#include <cstdio> #include <vector> #include <map> #include <set> using namespace std; #define FOR(i,a,b) for(int (i)=(int)(a); (i)!=(int)(b); ++(i)) int n, m, k; char M[2001][2001]; vector<int> A, B; int dist[2000][2000]; int main() { scanf("%d %d %d", &n, &m, &k); A.resize(k); B.resize(k); FOR(i,0,n) scanf("%s", M[i]); FOR(i,0,k) scanf("%d %d", &A[i], &B[i]); set<pair<int, int> > Q; FOR(y,0,n) FOR(x,0,m) dist[y][x] = -1; Q.insert(make_pair(0, 0)); int current_dist = 0; while (!Q.empty()) { set<pair<int, int> > newQ; for(set<pair<int, int> >::iterator it = Q.begin(); it != Q.end(); ++it) dist[it->first][it->second] = current_dist; for(set<pair<int, int> >::iterator it = Q.begin(); it != Q.end(); ++it) { int y = it->first, x = it->second; if (y>0 && M[y-1][x] == '.' && dist[y-1][x] == -1) newQ.insert(make_pair(y-1, x)); if (x>0 && M[y][x-1] == '.' && dist[y][x-1] == -1) newQ.insert(make_pair(y, x-1)); if (y<n-1 && M[y+1][x] == '.' && dist[y+1][x] == -1) newQ.insert(make_pair(y+1, x)); if (x<m-1 && M[y][x+1] == '.' && dist[y][x+1] == -1) newQ.insert(make_pair(y, x+1)); } ++current_dist; Q = newQ; } long long down = (dist[n-1][m-1] - n - m + 2) / 2; long long up = dist[n-1][m-1] - down; map<long long, int> RES; FOR(i,0,k) { long long w = up * (long long)(A[i]) + down * (long long)(B[i]); RES[w] += 1; } printf("%lld %d\n", RES.begin()->first, RES.begin()->second); }
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 | #include <cstdio> #include <vector> #include <map> #include <set> using namespace std; #define FOR(i,a,b) for(int (i)=(int)(a); (i)!=(int)(b); ++(i)) int n, m, k; char M[2001][2001]; vector<int> A, B; int dist[2000][2000]; int main() { scanf("%d %d %d", &n, &m, &k); A.resize(k); B.resize(k); FOR(i,0,n) scanf("%s", M[i]); FOR(i,0,k) scanf("%d %d", &A[i], &B[i]); set<pair<int, int> > Q; FOR(y,0,n) FOR(x,0,m) dist[y][x] = -1; Q.insert(make_pair(0, 0)); int current_dist = 0; while (!Q.empty()) { set<pair<int, int> > newQ; for(set<pair<int, int> >::iterator it = Q.begin(); it != Q.end(); ++it) dist[it->first][it->second] = current_dist; for(set<pair<int, int> >::iterator it = Q.begin(); it != Q.end(); ++it) { int y = it->first, x = it->second; if (y>0 && M[y-1][x] == '.' && dist[y-1][x] == -1) newQ.insert(make_pair(y-1, x)); if (x>0 && M[y][x-1] == '.' && dist[y][x-1] == -1) newQ.insert(make_pair(y, x-1)); if (y<n-1 && M[y+1][x] == '.' && dist[y+1][x] == -1) newQ.insert(make_pair(y+1, x)); if (x<m-1 && M[y][x+1] == '.' && dist[y][x+1] == -1) newQ.insert(make_pair(y, x+1)); } ++current_dist; Q = newQ; } long long down = (dist[n-1][m-1] - n - m + 2) / 2; long long up = dist[n-1][m-1] - down; map<long long, int> RES; FOR(i,0,k) { long long w = up * (long long)(A[i]) + down * (long long)(B[i]); RES[w] += 1; } printf("%lld %d\n", RES.begin()->first, RES.begin()->second); } |