Codeforces Gym100286B Blind Walk (dfs)

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值