邓心一 6 éve
szülő
commit
27782431d1
2 módosított fájl, 159 hozzáadás és 3 törlés
  1. 80 0
      bailian/4115.naruto/main.cc
  2. 79 3
      bailian/4116.rescue-operations/main.cc

+ 80 - 0
bailian/4115.naruto/main.cc

@@ -0,0 +1,80 @@
+#include <cstdio>
+#include <cstring>
+#include <queue>
+
+using namespace std;
+
+const int L = 200;
+const int T = 10;
+
+const int dx[4] = {1, 0, -1, 0};
+const int dy[4] = {0, 1, 0, -1};
+
+char map[L][L + 1];
+bool flag[L][L][T];
+
+struct Pos {
+  int x;
+  int y;
+  int t;
+  int r;
+  Pos(int x = 0, int y = 0, int t = 0, int r = 0) : x(x), y(y), t(t), r(r) {}
+  bool operator==(const Pos &that) const {
+    return this->x == that.x && this->y == that.y;
+  }
+};
+
+int m, n;
+
+int is_expandable(int x, int y, int t) {
+  if (x < 0 || m <= x || y < 0 || n <= y) return false;
+  if (map[x][y] == '#') return t != 0 && !flag[x][y][t - 1];
+  if (flag[x][y][t]) return false;
+  return true;
+}
+
+int main() {
+  int t;
+  scanf("%d %d %d", &m, &n, &t);
+  Pos naruto, sasuke;
+  for (int i = 0; i < m; i++) {
+    scanf("%s", map[i]);
+    for (int j = 0; j < n; j++) {
+      if (map[i][j] == '@') {
+        naruto.x = i;
+        naruto.y = j;
+        naruto.t = t;
+      } else if (map[i][j] == '+') {
+        sasuke.x = i;
+        sasuke.y = j;
+        sasuke.r = -1;
+      }
+    }
+  }
+  queue<Pos> q;
+  q.push(naruto);
+  memset(flag, 0, sizeof(flag));
+  flag[naruto.x][naruto.y][naruto.t] = true;
+  while (!q.empty()) {
+    Pos &cur = q.front();
+    q.pop();
+    if (cur == sasuke) {
+      sasuke.r = cur.r;
+      break;
+    }
+    for (int i = 0; i < 4; i++) {
+      int nx = cur.x + dx[i];
+      int ny = cur.y + dy[i];
+      if (!is_expandable(nx, ny, cur.t)) continue;
+      if (map[nx][ny] == '#') {
+        q.emplace(nx, ny, cur.t - 1, cur.r + 1);
+        flag[nx][ny][cur.t - 1] = true;
+      } else {
+        q.emplace(nx, ny, cur.t, cur.r + 1);
+        flag[nx][ny][cur.t] = true;
+      }
+    }
+  }
+  printf("%d\n", sasuke.r);
+  return 0;
+}

+ 79 - 3
bailian/4116.rescue-operations/main.cc

@@ -1,18 +1,94 @@
 #include <cstdio>
+#include <cstring>
+#include <queue>
 
 using namespace std;
 
 const int L = 200;
 
+const int dx[4] = {1, 0, -1, 0};
+const int dy[4] = {0, 1, 0, -1};
+
+int s, n, m;
+
+int flag[L][L];
+
 char map[L][L + 1];
 
+struct Pos {
+  int r;
+  int c;
+  bool k;
+  int t;
+  Pos(int r = 0, int c = 0, bool k = false, int t = 0)
+      : r(r), c(c), k(k), t(t){};
+  bool operator==(const Pos that) const {
+    return this->r == that.r && this->c == that.c;
+  }
+};
+
+bool is_expandable(int x, int y) {
+  if (x < 0 || n <= x || y < 0 || m <= y) return false;
+  if (map[x][y] == '#' || (flag[x][y] & 1) != 0) return false;
+  return true;
+}
+
 int main() {
-  int s, n, m;
   scanf("%d", &s);
   while (s--) {
     scanf("%d %d", &n, &m);
-    for (int i = 0; i < n; i++) scanf("%s", map[i]);
-    
+    Pos r, a;
+    for (int i = 0; i < n; i++) {
+      scanf("%s", map[i]);
+      for (int j = 0; j < m; j++) {
+        if (map[i][j] == 'a') {
+          a.r = i;
+          a.c = j;
+          a.t = -1;
+        } else if (map[i][j] == 'r') {
+          r.r = i;
+          r.c = j;
+        }
+      }
+    }
+    queue<Pos> q;
+    q.push(r);
+    memset(flag, 0, sizeof(flag));
+    flag[r.r][r.c] |= 1;
+    while (!q.empty()) {
+      Pos &cur = q.front();
+      q.pop();
+      if (cur == a) {
+        a.t = cur.t;
+        break;
+      }
+      if (cur.k == true) {
+        for (int i = 0; i < 4; i++) {
+          int nx = cur.r + dx[i];
+          int ny = cur.c + dy[i];
+          if (!is_expandable(nx, ny)) continue;
+          q.push(Pos(nx, ny, false, cur.t + 1));
+          flag[nx][ny] |= 1;
+        }
+      } else if (map[cur.r][cur.c] == 'x') {
+        if ((flag[cur.r][cur.c] & 2) == 0) {
+          q.push(Pos(cur.r, cur.c, true, cur.t + 1));
+          flag[cur.r][cur.c] |= 2;
+        }
+      } else {
+        for (int i = 0; i < 4; i++) {
+          int nx = cur.r + dx[i];
+          int ny = cur.c + dy[i];
+          if (!is_expandable(nx, ny)) continue;
+          q.push(Pos(nx, ny, false, cur.t + 1));
+          flag[nx][ny] |= 1;
+        }
+      }
+    }
+    if (a.t == -1)
+      printf("Impossible\n");
+    else
+      printf("%d\n", a.t);
   }
   return 0;
 }