#include<iostream> #include<vector> #include<algorithm> #include<climits> #include<queue> using namespace std; #define ULL unsigned long long #define PII pair< int, int > #define PLI pair< long long, int > #define VPII vector< PII > #define VPLI vector< PLI > #define MP make_pair #define REP(i,n) for(int i=0;i<(n);++i) #define FOR(i,a,b) for (int i=(a); i<(b); ++i) #define FORD(i,a,b) for (int i=(a)-1; i>=(b); --i) #define EACH(a, x) for (auto& a : (x)) #define ALL(x) (x).begin(), (x).end() #define INF INT_MAX #define MAXN 2001 #define MAXK 1000001 int n, m, k; int visited[MAXN + 1][MAXN + 1]; int main() { ios_base::sync_with_stdio(0); cin >> n >> m >> k; string s; visited[n + 1][m + 1] = 1; REP(row, n + 1) { visited[row][0] = visited[row][m + 1] = 1; } REP(col, m + 1) { visited[0][col] = visited[n + 1][col] = 1; } REP(row, n) { cin >> s; REP(col, m) { visited[row + 1][col + 1] = s[col] == 'X'; } } int end_row = n, end_col = n; vector<int> mf = {0, 0, 1, -1}, ms = {1, -1, 0, 0}; queue< pair<int, int> > q; q.push(MP(1, 1)); visited[1][1] = 1; int v; while (!q.empty()) { PII top = q.front(); q.pop(); v = visited[top.first][top.second]; if (top.first == n && top.second == m) { break; // cout << "Found target after " << v << " steps" << endl; } REP(i, 4) { if (visited[top.first + mf[i]][top.second + ms[i]] == 0) { visited[top.first + mf[i]][top.second + ms[i]] = v + 1; // cout << "Pushing " << top.first + mf[i] << " " << top.second + ms[i] << endl; q.push(MP(top.first + mf[i], top.second + ms[i])); } else { // cout << "Skipping " << top.first + mf[i] << " " << top.second + ms[i] << endl; } } } ULL up_steps = m + n - 2; v -= m + n - 1; up_steps += v / 2; ULL down_steps = v / 2; ULL best = ULLONG_MAX; ULL a, b; int best_cnt = 0; REP(i, k) { cin >> a >> b; ULL val = a * up_steps + b * down_steps; if (val < best) { best = val; best_cnt = 1; } else if (val == best) { best_cnt++; } } cout << best << ' ' << best_cnt << 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 | #include<iostream> #include<vector> #include<algorithm> #include<climits> #include<queue> using namespace std; #define ULL unsigned long long #define PII pair< int, int > #define PLI pair< long long, int > #define VPII vector< PII > #define VPLI vector< PLI > #define MP make_pair #define REP(i,n) for(int i=0;i<(n);++i) #define FOR(i,a,b) for (int i=(a); i<(b); ++i) #define FORD(i,a,b) for (int i=(a)-1; i>=(b); --i) #define EACH(a, x) for (auto& a : (x)) #define ALL(x) (x).begin(), (x).end() #define INF INT_MAX #define MAXN 2001 #define MAXK 1000001 int n, m, k; int visited[MAXN + 1][MAXN + 1]; int main() { ios_base::sync_with_stdio(0); cin >> n >> m >> k; string s; visited[n + 1][m + 1] = 1; REP(row, n + 1) { visited[row][0] = visited[row][m + 1] = 1; } REP(col, m + 1) { visited[0][col] = visited[n + 1][col] = 1; } REP(row, n) { cin >> s; REP(col, m) { visited[row + 1][col + 1] = s[col] == 'X'; } } int end_row = n, end_col = n; vector<int> mf = {0, 0, 1, -1}, ms = {1, -1, 0, 0}; queue< pair<int, int> > q; q.push(MP(1, 1)); visited[1][1] = 1; int v; while (!q.empty()) { PII top = q.front(); q.pop(); v = visited[top.first][top.second]; if (top.first == n && top.second == m) { break; // cout << "Found target after " << v << " steps" << endl; } REP(i, 4) { if (visited[top.first + mf[i]][top.second + ms[i]] == 0) { visited[top.first + mf[i]][top.second + ms[i]] = v + 1; // cout << "Pushing " << top.first + mf[i] << " " << top.second + ms[i] << endl; q.push(MP(top.first + mf[i], top.second + ms[i])); } else { // cout << "Skipping " << top.first + mf[i] << " " << top.second + ms[i] << endl; } } } ULL up_steps = m + n - 2; v -= m + n - 1; up_steps += v / 2; ULL down_steps = v / 2; ULL best = ULLONG_MAX; ULL a, b; int best_cnt = 0; REP(i, k) { cin >> a >> b; ULL val = a * up_steps + b * down_steps; if (val < best) { best = val; best_cnt = 1; } else if (val == best) { best_cnt++; } } cout << best << ' ' << best_cnt << endl; } |