#include <iostream> using namespace std; class Item { public: short y; short x; int ret; Item *next; Item(short yy, short xx, int rr) : y(yy), x(xx), ret(rr), next(0) {} }; char field[2000][2000]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, m, k; cin >> n >> m >> k; string empty; getline(cin, empty); for (int i = 0; i < n; i++) { string line; getline(cin, line); for (int j = 0; j < m; j++) { char c = line.at(j); field[i][j] = c; } } Item *head = new Item(0, 0, 0); Item *tail = head; field[0][0] = '#'; for (;;) { short y = head->y; short x = head->x; if (y == n - 1 && x == m - 1) { break; } if (y > 0 && field[y - 1][x] == '.') { Item *item = new Item(y - 1, x, head->ret + 1); tail->next = item; tail = item; field[y - 1][x] = '#'; } if (x > 0 && field[y][x - 1] == '.') { Item *item = new Item(y, x - 1, head->ret + 1); tail->next = item; tail = item; field[y][x - 1] = '#'; } if (y < n - 1 && field[y + 1][x] == '.') { Item *item = new Item(y + 1, x, head->ret); tail->next = item; tail = item; field[y + 1][x] = '#'; } if (x < m - 1 && field[y][x + 1] == '.') { Item *item = new Item(y, x + 1, head->ret); tail->next = item; tail = item; field[y][x + 1] = '#'; } Item *del = head; head = head->next; delete del; } int ret = head->ret; long long lowest = -1; int count = 0; for (int i = 0; i < k; i++) { long long a; long long b; cin >> a >> b; long long res = a * (m + n - 2) + (a + b) * ret; if (lowest < 0 || res < lowest) { lowest = res; count = 1; } else if (res == lowest) { count++; } } cout << lowest << " " << count << endl; 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 | #include <iostream> using namespace std; class Item { public: short y; short x; int ret; Item *next; Item(short yy, short xx, int rr) : y(yy), x(xx), ret(rr), next(0) {} }; char field[2000][2000]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, m, k; cin >> n >> m >> k; string empty; getline(cin, empty); for (int i = 0; i < n; i++) { string line; getline(cin, line); for (int j = 0; j < m; j++) { char c = line.at(j); field[i][j] = c; } } Item *head = new Item(0, 0, 0); Item *tail = head; field[0][0] = '#'; for (;;) { short y = head->y; short x = head->x; if (y == n - 1 && x == m - 1) { break; } if (y > 0 && field[y - 1][x] == '.') { Item *item = new Item(y - 1, x, head->ret + 1); tail->next = item; tail = item; field[y - 1][x] = '#'; } if (x > 0 && field[y][x - 1] == '.') { Item *item = new Item(y, x - 1, head->ret + 1); tail->next = item; tail = item; field[y][x - 1] = '#'; } if (y < n - 1 && field[y + 1][x] == '.') { Item *item = new Item(y + 1, x, head->ret); tail->next = item; tail = item; field[y + 1][x] = '#'; } if (x < m - 1 && field[y][x + 1] == '.') { Item *item = new Item(y, x + 1, head->ret); tail->next = item; tail = item; field[y][x + 1] = '#'; } Item *del = head; head = head->next; delete del; } int ret = head->ret; long long lowest = -1; int count = 0; for (int i = 0; i < k; i++) { long long a; long long b; cin >> a >> b; long long res = a * (m + n - 2) + (a + b) * ret; if (lowest < 0 || res < lowest) { lowest = res; count = 1; } else if (res == lowest) { count++; } } cout << lowest << " " << count << endl; return 0; } |