#include <bits/stdc++.h>
#define FOR(i, n) for(int i = 0; i < (n); ++i)
#define REP(i, a, b) for(int i = (a); i < (b); ++i)
#define TRAV(i, a) for(auto & i : (a))
#define SZ(x) ((int)(x).size())
#define PR std::pair
#define MP std::make_pair
#define X first
#define Y second
typedef long long ll;
typedef std::pair<int, int> PII;
typedef std::vector<int> VI;
int n, m;
int tab[2005][2005];
bool bounds(PII a){
return a.X >= 0 && a.X < n && a.Y >= 0 && a.Y < m;
}
std::vector<PII> dirs{MP(0, 1), MP(0, -1), MP(1, 0), MP(-1, 0)};
PII operator +(PII a, PII b){
return MP(a.X+b.X, a.Y+b.Y);
}
int main(){
std::ios_base::sync_with_stdio(false);
std::cin.tie(0);
int q;
std::cin >> n >> m >> q;
FOR(i, n){
FOR(j, m){
char a;
std::cin >> a;
tab[i][j] = (a == 'X');
}
}
std::queue<PII> que;
que.push(MP(0, 0));
std::map<PII, int> map;
map[MP(0, 0)] = 0;
while(SZ(que)){
auto v = que.front();
que.pop();
TRAV(d, dirs){
auto ch = d+v;
if(!bounds(ch)) continue;
if(tab[ch.X][ch.Y]) continue;
if(map.count(ch)) continue;
map[ch] = map[v]+1;
que.push(ch);
}
}
int co = map[MP(n-1, m-1)];
int a = co + n + m - 2;
assert(a%2 == 0);
a /= 2;
int b = co-a;
assert(b >= 0);
std::map<ll, int> wyn;
while(q--){
int x, y;
std::cin >> x >> y;
wyn[1ll*x*a + 1ll*y*b]++;
}
std::cout << wyn.begin()->X << " " << wyn.begin()->Y << "\n";
return 0;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | #include <bits/stdc++.h> #define FOR(i, n) for(int i = 0; i < (n); ++i) #define REP(i, a, b) for(int i = (a); i < (b); ++i) #define TRAV(i, a) for(auto & i : (a)) #define SZ(x) ((int)(x).size()) #define PR std::pair #define MP std::make_pair #define X first #define Y second typedef long long ll; typedef std::pair<int, int> PII; typedef std::vector<int> VI; int n, m; int tab[2005][2005]; bool bounds(PII a){ return a.X >= 0 && a.X < n && a.Y >= 0 && a.Y < m; } std::vector<PII> dirs{MP(0, 1), MP(0, -1), MP(1, 0), MP(-1, 0)}; PII operator +(PII a, PII b){ return MP(a.X+b.X, a.Y+b.Y); } int main(){ std::ios_base::sync_with_stdio(false); std::cin.tie(0); int q; std::cin >> n >> m >> q; FOR(i, n){ FOR(j, m){ char a; std::cin >> a; tab[i][j] = (a == 'X'); } } std::queue<PII> que; que.push(MP(0, 0)); std::map<PII, int> map; map[MP(0, 0)] = 0; while(SZ(que)){ auto v = que.front(); que.pop(); TRAV(d, dirs){ auto ch = d+v; if(!bounds(ch)) continue; if(tab[ch.X][ch.Y]) continue; if(map.count(ch)) continue; map[ch] = map[v]+1; que.push(ch); } } int co = map[MP(n-1, m-1)]; int a = co + n + m - 2; assert(a%2 == 0); a /= 2; int b = co-a; assert(b >= 0); std::map<ll, int> wyn; while(q--){ int x, y; std::cin >> x >> y; wyn[1ll*x*a + 1ll*y*b]++; } std::cout << wyn.begin()->X << " " << wyn.begin()->Y << "\n"; return 0; } |
English