#include <bits/stdc++.h> using namespace std; const int N = 2005; const long long INF = 1e18 + 5; const int X[4] = {1, 0, -1, 0}, Y[4] = {0, 1, 0, -1}; bool grid[N][N][2]; int arr[N][N][2]; queue < pair < int, int > > Q; bool good(int x, int y){ return grid[x][y][0] && !grid[x][y][1]; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, m, k; cin >> n >> m >> k; for(int i = 1; i <= n; i++){ for(int j = 1; j <= m; j++){ char a; cin >> a; grid[i][j][0] = (a == '.' ? true : false); // moge 1, nie moge 0 } } Q.push({1, 1}); while(Q.size()){ int x = Q.front().first, y = Q.front().second; Q.pop(); for(int i = 0; i < 4; i++){ int nx = x + X[i], ny = y + Y[i]; if(good(nx, ny)){ arr[nx][ny][0] = arr[x][y][0]; arr[nx][ny][1] = arr[x][y][1]; if(i < 2) arr[nx][ny][0]++; else arr[nx][ny][1]++; grid[nx][ny][1] = 1; Q.push({nx, ny}); } } } long long x = arr[n][m][0], y = arr[n][m][1]; long long mini = INF, cnt = 0; for(int i = 0; i < k; i++){ long long a, b; cin >> a >> b; long long q = a * x + b * y; if(q == mini) cnt++; else if(q < mini) mini = q, cnt = 1; } cout << mini << " " << 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 | #include <bits/stdc++.h> using namespace std; const int N = 2005; const long long INF = 1e18 + 5; const int X[4] = {1, 0, -1, 0}, Y[4] = {0, 1, 0, -1}; bool grid[N][N][2]; int arr[N][N][2]; queue < pair < int, int > > Q; bool good(int x, int y){ return grid[x][y][0] && !grid[x][y][1]; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, m, k; cin >> n >> m >> k; for(int i = 1; i <= n; i++){ for(int j = 1; j <= m; j++){ char a; cin >> a; grid[i][j][0] = (a == '.' ? true : false); // moge 1, nie moge 0 } } Q.push({1, 1}); while(Q.size()){ int x = Q.front().first, y = Q.front().second; Q.pop(); for(int i = 0; i < 4; i++){ int nx = x + X[i], ny = y + Y[i]; if(good(nx, ny)){ arr[nx][ny][0] = arr[x][y][0]; arr[nx][ny][1] = arr[x][y][1]; if(i < 2) arr[nx][ny][0]++; else arr[nx][ny][1]++; grid[nx][ny][1] = 1; Q.push({nx, ny}); } } } long long x = arr[n][m][0], y = arr[n][m][1]; long long mini = INF, cnt = 0; for(int i = 0; i < k; i++){ long long a, b; cin >> a >> b; long long q = a * x + b * y; if(q == mini) cnt++; else if(q < mini) mini = q, cnt = 1; } cout << mini << " " << cnt << "\n"; } |