题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=110868#problem/I
bfs水题。二维的图,已知起始点,问规定时间内是否能到达。
手生了,出了很多bug. 1,模拟去队列head>=tail 表示队列不空. 2.check函数和mp的判断重写了两次. check函数还忘写!vis.导致RE。 3.启动时光机的时候是不需要时间的, 判断的是如果下一个格子是时光机并可以穿越时间不变。应该是+1.导致WA一次。
这种水题,应该快速敲对。借鉴。
/*
数据范围小,深搜尝试。
*/
#include <stdio.h>
#include <string.h>
#include <iostream>
#define inf 1000000000
using namespace std;
char mp[4][20][20];
struct Node {
int c, x, y;
}que[4100], st, ed, temp, now, nxt;
int step[4][20][20];
bool vis[4][20][20];
int dir[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};
int n, m, t;
bool check(Node temp) {
if (temp.x >= 0 && temp.x < n && temp.y >= 0 && temp.y < m && !vis[temp.c][temp.x][temp.y]) {
return true;
}
return false;
}
void bfs(Node st) {
int head = -1, tail = 0;
que[++head] = st;
vis[st.c][st.x][st.y] = 1;
step[st.c][st.x][st.y] = 0;
while(head >= tail) {
now = que[tail++];
if (mp[now.c][now.x][now.y] == 'P') {
return;
}
for (int i=0; i<4; ++i) {
nxt.c = now.c;
nxt.x = now.x + dir[i][0];
nxt.y = now.y + dir[i][1];
if (check(nxt) && mp[nxt.c][nxt.x][nxt.y] != '*' && mp[nxt.c][nxt.x][nxt.y] != '#' ) {
que[++head] = nxt;
vis[nxt.c][nxt.x][nxt.y] = 1;
step[nxt.c][nxt.x][nxt.y] = step[now.c][now.x][now.y] + 1;
}
else if (check(nxt) && mp[nxt.c][nxt.x][nxt.y] == '#' && mp[nxt.c^1][nxt.x][nxt.y] != '#' && mp[nxt.c^1][nxt.x][nxt.y] != '*') { // same is 0 different is 1
nxt.c = now.c ^ 1;
que[++head] = nxt;
vis[nxt.c][nxt.x][nxt.y] = 1;
step[nxt.c][nxt.x][nxt.y] = step[now.c][now.x][now.y]+1;
}
}
}
return;
}
int main() {
int c;
cin >> c;
while(c--) {
memset(vis, 0, sizeof(vis));
// input
cin >> n >> m >> t;
for (int i=0; i<2; ++i) {
for (int j=0; j<n; ++j) {
for (int k=0; k<m; ++k) {
step[i][j][k] = inf;
}
}
}
for (int i=0; i<n; ++i) {
for (int j=0; j<m; ++j) {
cin >> mp[0][i][j];
if (mp[0][i][j] == 'S') {
st.c = 0;
st.x = i;
st.y = j;
}
else if (mp[0][i][j] == 'P') {
ed.c = 0;
ed.x = i;
ed.y = j;
}
}
}
for (int i=0; i<n; ++i) {
for (int j=0; j<m; ++j) {
cin >> mp[1][i][j];
if (mp[1][i][j] == 'S') {
st.c = 1;
st.x = i;
st.y = j;
}
else if (mp[1][i][j] == 'P') {
ed.c = 1;
ed.x = i;
ed.y = j;
}
}
}
bfs(st);
//cout << step[ed.c][ed.x][ed.y] << "====\n";
if (step[ed.c][ed.x][ed.y] <= t) {
cout << "YES\n";
}
else cout << "NO\n";
}
return 0;
}
浙公网安备 33010602011771号