#include<bits/stdc++.h> using namespace std; using ll = long long; const int N = 2e3+1, INF = 1e9+1; int n, m, k, dist[N][N]; bool vis[N][N]; vector<pair<int, int>> directions = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; bool check(int x, int y) { if(x<n && y<m && x>=0 && y>=0 && !vis[x][y]) return true; return false; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin>>n>>m>>k; for(int i=0; i<n; ++i) { for(int j=0; j<m; ++j) { char c; cin>>c; if(c=='X') vis[i][j] = 1; } } queue<pair<int, int>> que; que.push({0, 0}); for(int i=0; i<n; ++i) { for(int j=0; j<m; ++j) { dist[i][j] = INF; } } dist[0][0] = 0; while(que.size()) { auto [curx, cury] = que.front(); que.pop(); for(auto [dx, dy]: directions) { auto [nx, ny] = make_pair(curx+dx, cury+dy); if(check(nx, ny)) { dist[nx][ny] = dist[curx][cury]+1; que.push(make_pair(nx, ny)); vis[nx][ny]=1; } } } ll ans = 1e18+1; int cnt = 0; int d = dist[n-1][m-1]; for(int i=0; i<k; ++i) { ll a, b; cin>>a>>b; ll cost = (ll)(n+m-2)*a+(ll)(d-(n+m-2))*(a+b)/2; if(cost<ans) { ans = cost; cnt = 0; } if(cost==ans) { cnt++; } } cout<<ans<<' '<<cnt<<'\n'; }
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 | #include<bits/stdc++.h> using namespace std; using ll = long long; const int N = 2e3+1, INF = 1e9+1; int n, m, k, dist[N][N]; bool vis[N][N]; vector<pair<int, int>> directions = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; bool check(int x, int y) { if(x<n && y<m && x>=0 && y>=0 && !vis[x][y]) return true; return false; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin>>n>>m>>k; for(int i=0; i<n; ++i) { for(int j=0; j<m; ++j) { char c; cin>>c; if(c=='X') vis[i][j] = 1; } } queue<pair<int, int>> que; que.push({0, 0}); for(int i=0; i<n; ++i) { for(int j=0; j<m; ++j) { dist[i][j] = INF; } } dist[0][0] = 0; while(que.size()) { auto [curx, cury] = que.front(); que.pop(); for(auto [dx, dy]: directions) { auto [nx, ny] = make_pair(curx+dx, cury+dy); if(check(nx, ny)) { dist[nx][ny] = dist[curx][cury]+1; que.push(make_pair(nx, ny)); vis[nx][ny]=1; } } } ll ans = 1e18+1; int cnt = 0; int d = dist[n-1][m-1]; for(int i=0; i<k; ++i) { ll a, b; cin>>a>>b; ll cost = (ll)(n+m-2)*a+(ll)(d-(n+m-2))*(a+b)/2; if(cost<ans) { ans = cost; cnt = 0; } if(cost==ans) { cnt++; } } cout<<ans<<' '<<cnt<<'\n'; } |