#include <iostream> #include <queue> using namespace std; struct cords{ int x; int y; int dist=0; }; int main(){ int n,m,k; cin>>n>>m>>k; string matrix[n]; bool done[n][m]; for(int i=0; i<n; i++){ for(int j=0; j<m; j++){ done[i][j] = false; } } for(int i=0; i<n; i++){ cin>>matrix[i]; } queue<cords> q; cords tmp; tmp.x = 0; tmp.y = 0; tmp.dist = 0; q.push(tmp); done[0][0] = true; int dist = 0; while(!q.empty()){ dist++; cords tmp = q.front(); q.pop(); int x = tmp.x; int y = tmp.y; if(x == m-1 && y==n-1){ dist = tmp.dist; break; } done[y][x]=true; if(x > 0 && matrix[y][x-1] == '.' && done[y][x-1]==false){ cords tmp2; tmp2.x = x-1; tmp2.y = y; tmp2.dist = tmp.dist+1; q.push(tmp2); done[y][x-1]=true; } if(x+1 < m && matrix[y][x+1] == '.' && done[y][x+1]==false){ cords tmp2; tmp2.x = x+1; tmp2.y = y; tmp2.dist = tmp.dist+1; q.push(tmp2); done[y][x+1]=true; } if(y > 0 && matrix[y-1][x] == '.' && done[y-1][x]==false){ cords tmp2; tmp2.x = x; tmp2.y = y-1; tmp2.dist = tmp.dist+1; q.push(tmp2); done[y-1][x]=true; } if(y+1<n && matrix[y+1][x] == '.' && done[y+1][x]==false){ cords tmp2; tmp2.x = x; tmp2.y = y+1; tmp2.dist = tmp.dist+1; q.push(tmp2); done[y+1][x]=true; } } // for(int i=0; i<n; i++){ // for(int j=0; j<m; j++){ // cout<<((done[i][j]==true)?'#':'_'); // } // cout<<endl; // } // cout<<dist; int a = m+n-2,b=0; a += (dist-m-n+2)/2; b += (dist-m-n+2)/2; long long int min=-1; int counter = 0; for(int i=0; i<k; i++){ long long int ai, bi; cin>>ai>>bi; long long int time = ai*a + bi*b; if(time < min || min == -1){ counter = 1; min = time; } else if(time == min){ counter++; } } // cout<<a<<" "<<b<<endl; cout<<min<<" "<<counter<<endl; 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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | #include <iostream> #include <queue> using namespace std; struct cords{ int x; int y; int dist=0; }; int main(){ int n,m,k; cin>>n>>m>>k; string matrix[n]; bool done[n][m]; for(int i=0; i<n; i++){ for(int j=0; j<m; j++){ done[i][j] = false; } } for(int i=0; i<n; i++){ cin>>matrix[i]; } queue<cords> q; cords tmp; tmp.x = 0; tmp.y = 0; tmp.dist = 0; q.push(tmp); done[0][0] = true; int dist = 0; while(!q.empty()){ dist++; cords tmp = q.front(); q.pop(); int x = tmp.x; int y = tmp.y; if(x == m-1 && y==n-1){ dist = tmp.dist; break; } done[y][x]=true; if(x > 0 && matrix[y][x-1] == '.' && done[y][x-1]==false){ cords tmp2; tmp2.x = x-1; tmp2.y = y; tmp2.dist = tmp.dist+1; q.push(tmp2); done[y][x-1]=true; } if(x+1 < m && matrix[y][x+1] == '.' && done[y][x+1]==false){ cords tmp2; tmp2.x = x+1; tmp2.y = y; tmp2.dist = tmp.dist+1; q.push(tmp2); done[y][x+1]=true; } if(y > 0 && matrix[y-1][x] == '.' && done[y-1][x]==false){ cords tmp2; tmp2.x = x; tmp2.y = y-1; tmp2.dist = tmp.dist+1; q.push(tmp2); done[y-1][x]=true; } if(y+1<n && matrix[y+1][x] == '.' && done[y+1][x]==false){ cords tmp2; tmp2.x = x; tmp2.y = y+1; tmp2.dist = tmp.dist+1; q.push(tmp2); done[y+1][x]=true; } } // for(int i=0; i<n; i++){ // for(int j=0; j<m; j++){ // cout<<((done[i][j]==true)?'#':'_'); // } // cout<<endl; // } // cout<<dist; int a = m+n-2,b=0; a += (dist-m-n+2)/2; b += (dist-m-n+2)/2; long long int min=-1; int counter = 0; for(int i=0; i<k; i++){ long long int ai, bi; cin>>ai>>bi; long long int time = ai*a + bi*b; if(time < min || min == -1){ counter = 1; min = time; } else if(time == min){ counter++; } } // cout<<a<<" "<<b<<endl; cout<<min<<" "<<counter<<endl; return 0; } |