#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <stack>
#include <climits>
#include <vector>
#include <cmath>
#include <queue>
using namespace std;
struct node {
int x, y, step;
};
int d_x[9] = {-1, -1, 0, 1, 1, 1, 0, -1, 0};
int d_y[9] = {0, 1, 1, 1, 0, -1, -1, -1, 0};
int n, start_x, start_y, end_x, end_y;
char map[10][10];
//bool flags[10][10] = {false};
bool flag;
bool judge(int x, int y) {
if (x < 1 || x > 8 || y < 1 || y > 8) return false;
if (map[x][y] == 'S') return false;
if (x > 1 && map[x - 1][y] == 'S') return false;
return true;
}
void refresh(int times) {
for (int i = 8; i > times; --i) {
for (int j = 1; j <= 8; ++j) {
map[i][j] = map[i - 1][j];
}
}
for (int k = 1; k <= 8; ++k) {
map[times][k] = '.';
}
}
void solve() {
queue<node> q;
q.push(node{start_x, start_y, 1});
int now_step = 1;
while (!q.empty()) {
auto t = q.front();
q.pop();
if (t.step == 8) {
flag = true;
return;
}
if (now_step != t.step) {
refresh(now_step);
now_step = t.step;
}
if (t.x == end_x && t.y == end_y) {
flag = true;
return;
}
for (int i = 0; i < 9; ++i) {
int new_x = t.x + d_x[i];
int new_y = t.y + d_y[i];
if (judge(new_x, new_y)) {
q.push(node{new_x, new_y, now_step + 1});
}
}
}
}
int main() {
while (scanf("%d", &n) != EOF) {
for (int i = 0; i < n; ++i) {
flag = false;
for (int j = 1; j <= 8; ++j) {
for (int k = 1; k <= 8; ++k) {
char temp;
scanf(" %c", &temp);
if (temp == 'U') {
start_x = j;
start_y = k;
temp = '.';
}
if (temp == 'A') {
end_x = j;
end_y = k;
temp = '.';
}
map[j][k] = temp;
}
}
solve();
if (flag) printf("Case #%d: Yes\n", i + 1);
else printf("Case #%d: No\n", i + 1);
}
}
return 0;
}
问题 B: DFS or BFS?
最新推荐文章于 2024-09-26 00:03:11 发布