main.cc 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #include <cstdio>
  2. #include <cstring>
  3. #include <queue>
  4. using namespace std;
  5. const int L = 200;
  6. const int T = 10;
  7. const int dx[4] = {1, 0, -1, 0};
  8. const int dy[4] = {0, 1, 0, -1};
  9. char map[L][L + 1];
  10. bool flag[L][L][T];
  11. struct Pos {
  12. int x;
  13. int y;
  14. int t;
  15. int r;
  16. Pos(int x = 0, int y = 0, int t = 0, int r = 0) : x(x), y(y), t(t), r(r) {}
  17. bool operator==(const Pos &that) const {
  18. return this->x == that.x && this->y == that.y;
  19. }
  20. };
  21. int m, n;
  22. int is_expandable(int x, int y, int t) {
  23. if (x < 0 || m <= x || y < 0 || n <= y) return false;
  24. if (map[x][y] == '#') return t != 0 && !flag[x][y][t - 1];
  25. if (flag[x][y][t]) return false;
  26. return true;
  27. }
  28. int main() {
  29. int t;
  30. scanf("%d %d %d", &m, &n, &t);
  31. Pos naruto, sasuke;
  32. for (int i = 0; i < m; i++) {
  33. scanf("%s", map[i]);
  34. for (int j = 0; j < n; j++) {
  35. if (map[i][j] == '@') {
  36. naruto.x = i;
  37. naruto.y = j;
  38. naruto.t = t;
  39. } else if (map[i][j] == '+') {
  40. sasuke.x = i;
  41. sasuke.y = j;
  42. sasuke.r = -1;
  43. }
  44. }
  45. }
  46. queue<Pos> q;
  47. q.push(naruto);
  48. memset(flag, 0, sizeof(flag));
  49. flag[naruto.x][naruto.y][naruto.t] = true;
  50. while (!q.empty()) {
  51. Pos &cur = q.front();
  52. q.pop();
  53. if (cur == sasuke) {
  54. sasuke.r = cur.r;
  55. break;
  56. }
  57. for (int i = 0; i < 4; i++) {
  58. int nx = cur.x + dx[i];
  59. int ny = cur.y + dy[i];
  60. if (!is_expandable(nx, ny, cur.t)) continue;
  61. if (map[nx][ny] == '#') {
  62. q.emplace(nx, ny, cur.t - 1, cur.r + 1);
  63. flag[nx][ny][cur.t - 1] = true;
  64. } else {
  65. q.emplace(nx, ny, cur.t, cur.r + 1);
  66. flag[nx][ny][cur.t] = true;
  67. }
  68. }
  69. }
  70. printf("%d\n", sasuke.r);
  71. return 0;
  72. }