#include <bits/stdc++.h> using namespace std; long long n, m, k; bool a[2005][2005]; long long x[1000005], y[1000006]; vector < long long > e[5000000]; bool used[5000000]; queue < long long > q; long long d[5000000]; int main(){ cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(false); cin >> n >> m >> k; for (long long i = 0; i < n; ++i) { for (long long j = 0; j < m; ++j) { char c; cin >> c; if(c == '.'){ a[i][j] = true; } //cout << a[n - 1 - i][j] << endl; } } //cout << "AA" << endl; for (long long i = 0; i < k; ++i) { cin >> x[i] >> y[i]; //cout << x[i] << " " << y } for (long long i = 0; i < n; ++i) { for (long long j = 0; j < m; ++j) { //cout << i << ":" << j << endl; if(i >= 1){ //cout << "f" << endl; if(a[i - 1][j] and a[i][j]){ e[j + m * i].push_back(j + m * (i - 1)); e[j + m * (i - 1)].push_back(j + m * i); } } if(j >= 1){ if(a[i][j - 1] and a[i][j]){ e[j - 1 + m * i].push_back(j + m * i); e[j + m * i].push_back(j - 1 + m * i); } } //cout << e[4].size() << endl; } } for (long long i = 0; i < n * m; ++i) { //cout << i << ": "; //for(long long v : e[i]){ // cout << v << " "; // } // cout << endl; } q.push(0); used[0] = true; while(!q.empty()){ long long vertex = q.front(); q.pop(); //cout << q.size() << endl; used[vertex] = true; for(long long v : e[vertex]){ if(!used[v]){ used[v] = true; q.push(v); d[v] = d[vertex] + 1; } } } for (long long i = 0; i < n; ++i) { for (long long j = 0; j < m; ++j) { //cout << d[j + m * i] << " (" << a[i][j] << ") "; } //cout << endl; } long long dist = d[n * m - 1]; //cout << "dist = " << dist << endl; long long powroty = (dist - (n + m - 2)) / 2; long long wejscia = dist - powroty; long long max = -1; long long max_ind = 0; for (long long i = 0; i < k; ++i) { //cout << "wej = " << wejscia << "; powr = " << powroty << endl; //cout << "x = " << x[i] << "; y = " << y[i] << endl; long long t = x[i] * wejscia + y[i] * powroty; //cout << t << endl; if(t == max){ max_ind++; } if(t < max or max == -1){ max = t; max_ind = 1; } } cout << max << " " << max_ind << endl; }
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 | #include <bits/stdc++.h> using namespace std; long long n, m, k; bool a[2005][2005]; long long x[1000005], y[1000006]; vector < long long > e[5000000]; bool used[5000000]; queue < long long > q; long long d[5000000]; int main(){ cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(false); cin >> n >> m >> k; for (long long i = 0; i < n; ++i) { for (long long j = 0; j < m; ++j) { char c; cin >> c; if(c == '.'){ a[i][j] = true; } //cout << a[n - 1 - i][j] << endl; } } //cout << "AA" << endl; for (long long i = 0; i < k; ++i) { cin >> x[i] >> y[i]; //cout << x[i] << " " << y } for (long long i = 0; i < n; ++i) { for (long long j = 0; j < m; ++j) { //cout << i << ":" << j << endl; if(i >= 1){ //cout << "f" << endl; if(a[i - 1][j] and a[i][j]){ e[j + m * i].push_back(j + m * (i - 1)); e[j + m * (i - 1)].push_back(j + m * i); } } if(j >= 1){ if(a[i][j - 1] and a[i][j]){ e[j - 1 + m * i].push_back(j + m * i); e[j + m * i].push_back(j - 1 + m * i); } } //cout << e[4].size() << endl; } } for (long long i = 0; i < n * m; ++i) { //cout << i << ": "; //for(long long v : e[i]){ // cout << v << " "; // } // cout << endl; } q.push(0); used[0] = true; while(!q.empty()){ long long vertex = q.front(); q.pop(); //cout << q.size() << endl; used[vertex] = true; for(long long v : e[vertex]){ if(!used[v]){ used[v] = true; q.push(v); d[v] = d[vertex] + 1; } } } for (long long i = 0; i < n; ++i) { for (long long j = 0; j < m; ++j) { //cout << d[j + m * i] << " (" << a[i][j] << ") "; } //cout << endl; } long long dist = d[n * m - 1]; //cout << "dist = " << dist << endl; long long powroty = (dist - (n + m - 2)) / 2; long long wejscia = dist - powroty; long long max = -1; long long max_ind = 0; for (long long i = 0; i < k; ++i) { //cout << "wej = " << wejscia << "; powr = " << powroty << endl; //cout << "x = " << x[i] << "; y = " << y[i] << endl; long long t = x[i] * wejscia + y[i] * powroty; //cout << t << endl; if(t == max){ max_ind++; } if(t < max or max == -1){ max = t; max_ind = 1; } } cout << max << " " << max_ind << endl; } |