您的位置:首页 > 其它

Codeforces Gym100286B Blind Walk (dfs)

2015-08-08 19:08 896 查看

Blind Walk

Time Limit:2000MS

Memory Limit:262144KB

This is an interactive problem.

Your task is to write a program that controls a robot which lindly walks through a maze. The maze is n×m (1 ≤ n, m ≤ 30) rectangular grid that consists of square cells. Each cell is either empty or blocked.All cells on the border of the maze are blocked. The robot starts in an empty cell. It can move south,

west, north, or east to an adjacent empty cell. The robot is blind and has only bump sensors, so when it attempts to move it can either succeed or bump into blocked cell and fail.

The robot has to visit all empty cells in the maze. All cells are guaranteed to be reachable.The picture shows sample maze where blocked cells are, filled and initial robot’s location is designated with a circle.

The picture shows sample maze where blocked cells are, filled and initial robot’s location is designated with a circle.

Interaction protocol

The program must write to the standard output one line with robot’s action and wait for a line in the standard input with a response, then write next action and read next response, and so on until all empty cells in the maze had been visited. The program must exit only when all cells have been visited. Empty

cells may be visited multiple times. It is acceptable to move even after all cells had been visited.

Output

Each line of the standard output represents robot’s action. It is one of the following five strings: SOUTH, WEST, NORTH, EAST, or DONE. DONE must be printed when the robot has visited all empty cells. After printing DONE your program must exit. You must flush standard output after printing each action.

Input

Each line of the standard input represents response on robot’s action. It is either a string EMPTY if robot has successfully moved in the specified direction to an adjacent cell or a string BLOCKED if robot’s movement has failed because the orresponding adjacent cell was blocked.

题意

有一个迷宫,每次可以选择一个方向前进,然后从输入读取一个字符串,EMPTY表示可以前进,BLOCKED表示不能前进。

题解

建一个vis矩阵,模拟一下dfs的过程就好。需要注意的是返回上一层的时候要输出返回的方向并且丢弃下一个输入,还有记的每次输出之后用
fflush(stdout)
,否则会有迷之返回结果(不知道原理是啥。。。),AC代码如下

#include <cstdio>
#include <cstring>
#include <cstdlib>

using namespace std;

int M[65][65];
char d[10][10]={"NORTH","EAST","SOUTH","WEST"};
int dx[]={0,1,0,-1};
int dy[]={-1,0,1,0};
char ans[10];
int x,y;
void dfs(int s)
{
M[x][y]=1;
for(int i=0;i<4;i++)
{
if(i!=s&&M[x+dx[i]][y+dy[i]]==0)
{
printf("%s\n",d[i]);
fflush(stdout);
scanf("%s",ans);
if(ans[0]=='B')
M[x+dx[i]][y+dy[i]]=1;
else
{
x+=dx[i];
y+=dy[i];
dfs((i+2)%4);
}
}
}
if(s==-1)
return;
printf("%s\n",d[s]);
fflush(stdout);
x+=dx[s];
y+=dy[s];
scanf("%s",ans);
}
int main()
{
//freopen("in.txt","r",stdin);
memset(M,0,sizeof M);
x=y=32;
dfs(-1);
printf("DONE\n");
fflush(stdout);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  codeforces dfs