#include <stdio.h> #include <queue> using namespace std; struct Map { int down, up; bool free, visited; Map() { down = 0; up = 0; visited = false; } }; int main() { char line[2001]; int n, m, k; scanf("%d %d %d", &n, &m, &k); Map** map = new Map * [n]; for (int i = 0; i < n; i++) { map[i] = new Map[m]; scanf("%s", line); for (int j = 0; j < m; j++) { map[i][j].visited = false; map[i][j].free = line[j] == '.'; } } map[0][0].visited = true; queue<int> qi, qj; qi.push(0); qj.push(0); while (!qi.empty()) { int i = qi.front(); int j = qj.front(); qi.pop(); qj.pop(); if (i > 0 && map[i - 1][j].free && !map[i - 1][j].visited) { map[i - 1][j].visited = true; map[i - 1][j].down = map[i][j].down + 1; map[i - 1][j].up = map[i][j].up; qi.push(i - 1); qj.push(j); } if (j > 0 && map[i][j - 1].free && !map[i][j - 1].visited) { map[i][j - 1].visited = true; map[i][j - 1].down = map[i][j].down + 1; map[i][j - 1].up = map[i][j].up; qi.push(i); qj.push(j - 1); } if (i < n - 1 && map[i + 1][j].free && !map[i + 1][j].visited) { map[i + 1][j].visited = true; map[i + 1][j].down = map[i][j].down; map[i + 1][j].up = map[i][j].up + 1; qi.push(i + 1); qj.push(j); } if (j < m - 1 && map[i][j + 1].free && !map[i][j + 1].visited) { map[i][j + 1].visited = true; map[i][j + 1].down = map[i][j].down; map[i][j + 1].up = map[i][j].up + 1; qi.push(i); qj.push(j + 1); } } long long sum = 1000000000000000000LL, count = 0, a, b; Map last = map[n - 1][m - 1]; for (int i = 0; i < k; i++) { scanf("%lld %lld", &a, &b); long long c = a * last.up + b * last.down; if (sum > c) { sum = c; count = 0; } if (sum == c) { count++; } } printf("%lld %lld", sum, count); 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 | #include <stdio.h> #include <queue> using namespace std; struct Map { int down, up; bool free, visited; Map() { down = 0; up = 0; visited = false; } }; int main() { char line[2001]; int n, m, k; scanf("%d %d %d", &n, &m, &k); Map** map = new Map * [n]; for (int i = 0; i < n; i++) { map[i] = new Map[m]; scanf("%s", line); for (int j = 0; j < m; j++) { map[i][j].visited = false; map[i][j].free = line[j] == '.'; } } map[0][0].visited = true; queue<int> qi, qj; qi.push(0); qj.push(0); while (!qi.empty()) { int i = qi.front(); int j = qj.front(); qi.pop(); qj.pop(); if (i > 0 && map[i - 1][j].free && !map[i - 1][j].visited) { map[i - 1][j].visited = true; map[i - 1][j].down = map[i][j].down + 1; map[i - 1][j].up = map[i][j].up; qi.push(i - 1); qj.push(j); } if (j > 0 && map[i][j - 1].free && !map[i][j - 1].visited) { map[i][j - 1].visited = true; map[i][j - 1].down = map[i][j].down + 1; map[i][j - 1].up = map[i][j].up; qi.push(i); qj.push(j - 1); } if (i < n - 1 && map[i + 1][j].free && !map[i + 1][j].visited) { map[i + 1][j].visited = true; map[i + 1][j].down = map[i][j].down; map[i + 1][j].up = map[i][j].up + 1; qi.push(i + 1); qj.push(j); } if (j < m - 1 && map[i][j + 1].free && !map[i][j + 1].visited) { map[i][j + 1].visited = true; map[i][j + 1].down = map[i][j].down; map[i][j + 1].up = map[i][j].up + 1; qi.push(i); qj.push(j + 1); } } long long sum = 1000000000000000000LL, count = 0, a, b; Map last = map[n - 1][m - 1]; for (int i = 0; i < k; i++) { scanf("%lld %lld", &a, &b); long long c = a * last.up + b * last.down; if (sum > c) { sum = c; count = 0; } if (sum == c) { count++; } } printf("%lld %lld", sum, count); return 0; } |