#include <iostream> #include <string> using namespace std; struct field { public: bool visited; int cost; field() : visited(true), cost(0) {} field(bool v, int c) : visited(v), cost(c) {} }tab[2000][2000]; struct coords { short x, y; int cost = 0; coords(){} coords(short x, short y) : x(x), y(y) {} bool operator < (const coords& a) { return cost < a.cost; } bool operator > (const coords& a) { return cost > a.cost; } } h[4000000]; int hl=0; short n, m; int k; // HEAP void heapParent(int index) { if (index == 0) return; int parent = (index - 1) / 2; if (h[parent] > h[index]) { swap(h[parent], h[index]); heapParent(parent); } } void heapChildren(int index) { int child = 1 + index * 2; if (child >= hl) return; if (child + 1 < hl) if (h[child + 1] < h[child]) child = child + 1; if (h[index] > h[child]) { swap(h[index], h[child]); heapChildren(child); } } void addHeap(short x, short y, int cost) { h[hl].x = x; h[hl].y = y; h[hl].cost = cost; heapParent(hl++); } coords popMax() { swap(h[0], h[--hl]); heapChildren(0); return h[hl]; } // HEAP END int main() { ios_base::sync_with_stdio(0); int i, j, bcost = 0; char c; cin >> n >> m >> k; for (i = 0; i < n; i++) { for (j = 0; j < m; j++) { cin >> c; tab[i][j].visited = c == 'X'; } } addHeap(0, 0, 0); coords curr; while (hl > 0) { curr = popMax(); i = curr.x; j = curr.y; if (tab[i][j].visited) continue; tab[i][j].visited = true; if (i + 1 < n) { // can down if (i + 2 == n && j + 1 == m) { bcost = tab[i][j].cost; break; } if (!tab[i + 1][j].visited) { tab[i + 1][j].cost = tab[i][j].cost; addHeap(i + 1, j, tab[i + 1][j].cost); } } if (j + 1 < m) { // can right if (i + 1 == n && j + 2 == m) { bcost = tab[i][j].cost; break; } if (!tab[i][j + 1].visited) { tab[i][j + 1].cost = tab[i][j].cost; addHeap(i, j + 1, tab[i][j + 1].cost); } } if (i - 1 >= 0) { // can up if (!tab[i - 1][j].visited) { tab[i - 1][j].cost = tab[i][j].cost + 1; addHeap(i - 1, j, tab[i - 1][j].cost); } } if (j - 1 >= 0) { // can left if (!tab[i][j - 1].visited) { tab[i][j - 1].cost = tab[i][j].cost + 1; addHeap(i , j - 1, tab[i][j - 1].cost); } } } int cost = m + n - 2, crnt; int min = INT32_MAX, mincnt = 0; while (k--) { cin >> i >> j; crnt = (cost + bcost) * i + bcost * j; if (crnt < min) { min = crnt; mincnt = 1; } else if (crnt == min) mincnt++; } cout << min << " " << mincnt << "\n"; 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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 | #include <iostream> #include <string> using namespace std; struct field { public: bool visited; int cost; field() : visited(true), cost(0) {} field(bool v, int c) : visited(v), cost(c) {} }tab[2000][2000]; struct coords { short x, y; int cost = 0; coords(){} coords(short x, short y) : x(x), y(y) {} bool operator < (const coords& a) { return cost < a.cost; } bool operator > (const coords& a) { return cost > a.cost; } } h[4000000]; int hl=0; short n, m; int k; // HEAP void heapParent(int index) { if (index == 0) return; int parent = (index - 1) / 2; if (h[parent] > h[index]) { swap(h[parent], h[index]); heapParent(parent); } } void heapChildren(int index) { int child = 1 + index * 2; if (child >= hl) return; if (child + 1 < hl) if (h[child + 1] < h[child]) child = child + 1; if (h[index] > h[child]) { swap(h[index], h[child]); heapChildren(child); } } void addHeap(short x, short y, int cost) { h[hl].x = x; h[hl].y = y; h[hl].cost = cost; heapParent(hl++); } coords popMax() { swap(h[0], h[--hl]); heapChildren(0); return h[hl]; } // HEAP END int main() { ios_base::sync_with_stdio(0); int i, j, bcost = 0; char c; cin >> n >> m >> k; for (i = 0; i < n; i++) { for (j = 0; j < m; j++) { cin >> c; tab[i][j].visited = c == 'X'; } } addHeap(0, 0, 0); coords curr; while (hl > 0) { curr = popMax(); i = curr.x; j = curr.y; if (tab[i][j].visited) continue; tab[i][j].visited = true; if (i + 1 < n) { // can down if (i + 2 == n && j + 1 == m) { bcost = tab[i][j].cost; break; } if (!tab[i + 1][j].visited) { tab[i + 1][j].cost = tab[i][j].cost; addHeap(i + 1, j, tab[i + 1][j].cost); } } if (j + 1 < m) { // can right if (i + 1 == n && j + 2 == m) { bcost = tab[i][j].cost; break; } if (!tab[i][j + 1].visited) { tab[i][j + 1].cost = tab[i][j].cost; addHeap(i, j + 1, tab[i][j + 1].cost); } } if (i - 1 >= 0) { // can up if (!tab[i - 1][j].visited) { tab[i - 1][j].cost = tab[i][j].cost + 1; addHeap(i - 1, j, tab[i - 1][j].cost); } } if (j - 1 >= 0) { // can left if (!tab[i][j - 1].visited) { tab[i][j - 1].cost = tab[i][j].cost + 1; addHeap(i , j - 1, tab[i][j - 1].cost); } } } int cost = m + n - 2, crnt; int min = INT32_MAX, mincnt = 0; while (k--) { cin >> i >> j; crnt = (cost + bcost) * i + bcost * j; if (crnt < min) { min = crnt; mincnt = 1; } else if (crnt == min) mincnt++; } cout << min << " " << mincnt << "\n"; return 0; } |