#include <bits/stdc++.h> using namespace std; string gory[2009]; bool czyste[2009][2009]; long long int i_pod, wys, dlug, wyn, i_best, a, b, pom, poprz; bool czy; int DFS (long long int x, long long int y, long long int czas, long long int a, long long int b, bool odwiedzone[2009][2009], long long int cel_x, long long int cel_y) { odwiedzone[y][x] = true; if ((not odwiedzone[y + 1][x]) and (gory[y + 1][x] == '.')) { long long int ple; ple = DFS (x, y + 1, czas + a, a, b, odwiedzone, cel_x, cel_y); czas = min (czas, ple); } if ((not odwiedzone[y][x + 1]) and (gory[y][x + 1] == '.')) { long long int ple; ple = DFS (x + 1, y, czas + a, a, b, odwiedzone, cel_x, cel_y); czas = min (czas, ple); } if ((not odwiedzone[y - 1][x]) and (gory[y - 1][x] == '.')) { long long int ple; ple = DFS (x, y - 1, czas + b, a, b, odwiedzone, cel_x, cel_y); czas = min (czas, ple); } if ((not odwiedzone[y][x - 1]) and (gory[y][x - 1] == '.')) { long long int ple; ple = DFS (x - 1, y, czas + b, a, b, odwiedzone, cel_x, cel_y); czas = min (czas, ple); } if ((x == cel_x) and (y == cel_y)) { if (czas < wyn){ wyn = czas; i_best = 0; czy = true; } if (czas == wyn){ czy = true; } } return czas; } int main () { string tym; gory[0] = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX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XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"; cin >> wys >> dlug >> i_pod; for (int x = 1; x <= wys; x++) { cin >> tym; tym = 'X' + tym + 'X'; gory[x] = tym; } i_best = 0; wyn = 4 * pow(10, 14); poprz = -1; for (int _ = 0; _ < i_pod; _++){ cin >> a >> b; czy = false; pom = DFS (1, 1, 0, a, b, czyste, dlug, wys); if (czy){ i_best += 1; } for (int f = 1; f <= dlug; f++){ for (int g = 1; g <= wys; g++){ czyste[g][f] = false; } } } cout << wyn << ' ' << i_best; return 0; } /* 5 7 1 ......X X.X..X. ..X.X.X .X.X... .....X. 2 1 2 5 4 .X... ...X. 2 1 2 2 1 7 2 1 6 5 1 ..... .XXX. ..XX. X.XX. ..XX. X.... 2 1 */
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 | #include <bits/stdc++.h> using namespace std; string gory[2009]; bool czyste[2009][2009]; long long int i_pod, wys, dlug, wyn, i_best, a, b, pom, poprz; bool czy; int DFS (long long int x, long long int y, long long int czas, long long int a, long long int b, bool odwiedzone[2009][2009], long long int cel_x, long long int cel_y) { odwiedzone[y][x] = true; if ((not odwiedzone[y + 1][x]) and (gory[y + 1][x] == '.')) { long long int ple; ple = DFS (x, y + 1, czas + a, a, b, odwiedzone, cel_x, cel_y); czas = min (czas, ple); } if ((not odwiedzone[y][x + 1]) and (gory[y][x + 1] == '.')) { long long int ple; ple = DFS (x + 1, y, czas + a, a, b, odwiedzone, cel_x, cel_y); czas = min (czas, ple); } if ((not odwiedzone[y - 1][x]) and (gory[y - 1][x] == '.')) { long long int ple; ple = DFS (x, y - 1, czas + b, a, b, odwiedzone, cel_x, cel_y); czas = min (czas, ple); } if ((not odwiedzone[y][x - 1]) and (gory[y][x - 1] == '.')) { long long int ple; ple = DFS (x - 1, y, czas + b, a, b, odwiedzone, cel_x, cel_y); czas = min (czas, ple); } if ((x == cel_x) and (y == cel_y)) { if (czas < wyn){ wyn = czas; i_best = 0; czy = true; } if (czas == wyn){ czy = true; } } return czas; } int main () { string tym; gory[0] = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX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XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"; cin >> wys >> dlug >> i_pod; for (int x = 1; x <= wys; x++) { cin >> tym; tym = 'X' + tym + 'X'; gory[x] = tym; } i_best = 0; wyn = 4 * pow(10, 14); poprz = -1; for (int _ = 0; _ < i_pod; _++){ cin >> a >> b; czy = false; pom = DFS (1, 1, 0, a, b, czyste, dlug, wys); if (czy){ i_best += 1; } for (int f = 1; f <= dlug; f++){ for (int g = 1; g <= wys; g++){ czyste[g][f] = false; } } } cout << wyn << ' ' << i_best; return 0; } /* 5 7 1 ......X X.X..X. ..X.X.X .X.X... .....X. 2 1 2 5 4 .X... ...X. 2 1 2 2 1 7 2 1 6 5 1 ..... .XXX. ..XX. X.XX. ..XX. X.... 2 1 */ |