Codeforces Gym 100286B Blind Walk DFS

Problem B. Blind Walk
Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

http://acm.hust.edu.cn/vjudge/contest/view.action?cid=86821#problem/B

Description

This is an interactive problem. Your task is to write a program that controls a robot which blindly 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 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.

Input

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.

Output

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 corresponding adjacent cell was blocked.

Sample Input

NORTH
EAST
SOUTH
EAST
SOUTH
WEST
SOUTH
WEST
NORTH
WEST
WEST
NORTH
EAST
NORTH
DONE

Sample Output

BLOCKED
BLOCKED
EMPTY
BLOCKED
BLOCKED
EMPTY
BLOCKED
BLOCKED
EMPTY
EMPTY
BLOCKED
BLOCKED
EMPTY
BLOCKED

HINT

题意

交互题

给你一个图,然后让你遍历这个图,你每进行一个动作,题目都会给你反馈

题解

dfs就好了,注意回溯就好了

代码:

#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define test freopen("test.txt","r",stdin)
const int maxn=202501;
#define mod 1000000007
#define eps 1e-9
const int inf=0x3f3f3f3f;
const ll infll = 0x3f3f3f3f3f3f3f3fLL;
inline ll read()
{
    ll x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
//*************************************************************************************

int dx[4]={1,0,0,-1};
int dy[4]={0,1,-1,0};
int vis[2000][2000];
char s[4][10]={"NORTH","EAST","WEST","SOUTH"};
char s1[10];

void dfs(int x,int y,int to)
{
  //  cout<<x<<" "<<y<<" "<<vis[x][y]<<endl;
    for(vis[x][y]++;vis[x][y]<4;vis[x][y]++)
    {
        int i=vis[x][y];
        if(3-i==to) continue;
        printf("%s
",s[i]);
        fflush(stdout);
        scanf("%s",s1);
      //  cout<<s1<<endl;
        int xx=x+dx[i],yy=y+dy[i];
     //   cout<<xx<<" "<<yy<<" "<<s1<<endl;
        if(s1[0]=='E')
        {
            dfs(xx,yy,i);
        }
    }
    printf("%s
",s[3-to]);
    fflush(stdout);
    scanf("%s",s1);
  //  cout<<s1<<endl;
    return ;
}

int main()
{
    memset(vis,-1,sizeof(vis));
    for(vis[1000][1000]++;vis[1000][1000]<4;vis[1000][1000]++)
    {
        int i=vis[1000][1000];
        printf("%s
",s[i]);
        fflush(stdout);
        scanf("%s",s1);
        if(s1[0]=='E')dfs(1000+dx[i],1000+dy[i],i);
    }
    printf("DONE
");
    fflush(stdout);
    return 0;
}
原文地址:https://www.cnblogs.com/qscqesze/p/4713729.html