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=;
#define mod 1000000007
#define eps 1e-9
const int inf=0x3f3f3f3f;
const ll infll = 0x3f3f3f3f3f3f3f3fLL;
inline ll read()
{
ll x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
//************************************************************************************* int dx[]={,,,-};
int dy[]={,,-,};
int vis[][];
char s[][]={"NORTH","EAST","WEST","SOUTH"};
char s1[]; void dfs(int x,int y,int to)
{
// cout<<x<<" "<<y<<" "<<vis[x][y]<<endl;
for(vis[x][y]++;vis[x][y]<;vis[x][y]++)
{
int i=vis[x][y];
if(-i==to) continue;
printf("%s\n",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[]=='E')
{
dfs(xx,yy,i);
}
}
printf("%s\n",s[-to]);
fflush(stdout);
scanf("%s",s1);
// cout<<s1<<endl;
return ;
} int main()
{
memset(vis,-,sizeof(vis));
for(vis[][]++;vis[][]<;vis[][]++)
{
int i=vis[][];
printf("%s\n",s[i]);
fflush(stdout);
scanf("%s",s1);
if(s1[]=='E')dfs(+dx[i],+dy[i],i);
}
printf("DONE\n");
fflush(stdout);
return ;
}

Codeforces Gym 100286B Blind Walk DFS的更多相关文章

  1. codeforecs Gym 100286B Blind Walk

    交互式程序,要用到一个函数fflush,它的作用是对标准输出流的清理,对stdout来说是及时地打印数据到屏幕上,一个事实:标准输出是以『行』为单位进行的,也即碰到\n才打印数据到屏幕.这就可能造成延 ...

  2. Codeforces Gym 101252D&&floyd判圈算法学习笔记

    一句话题意:x0=1,xi+1=(Axi+xi%B)%C,如果x序列中存在最早的两个相同的元素,输出第二次出现的位置,若在2e7内无解则输出-1. 题解:都不到100天就AFO了才来学这floyd判圈 ...

  3. Codeforces Gym 101190M Mole Tunnels - 费用流

    题目传送门 传送门 题目大意 $m$只鼹鼠有$n$个巢穴,$n - 1$条长度为$1$的通道将它们连通且第$i(i > 1)$个巢穴与第$\left\lfloor \frac{i}{2}\rig ...

  4. Codeforces Gym 101623A - 动态规划

    题目传送门 传送门 题目大意 给定一个长度为$n$的序列,要求划分成最少的段数,然后将这些段排序使得新序列单调不减. 考虑将相邻的相等的数缩成一个数. 假设没有分成了$n$段,考虑最少能够减少多少划分 ...

  5. 【Codeforces Gym 100725K】Key Insertion

    Codeforces Gym 100725K 题意:给定一个初始全0的序列,然后给\(n\)个查询,每一次调用\(Insert(L_i,i)\),其中\(Insert(L,K)\)表示在第L位插入K, ...

  6. Codeforces gym 101343 J.Husam and the Broken Present 2【状压dp】

     2017 JUST Programming Contest 2.0 题目链接:Codeforces gym 101343 J.Husam and the Broken Present 2 J. Hu ...

  7. codeforces gym 100553I

    codeforces gym 100553I solution 令a[i]表示位置i的船的编号 研究可以发现,应是从中间开始,往两边跳.... 于是就是一个点往两边的最长下降子序列之和减一 魔改树状数 ...

  8. codeforces 615 B. Longtail Hedgehog (DFS + 剪枝)

    题目链接: codeforces 615 B. Longtail Hedgehog (DFS + 剪枝) 题目描述: 给定n个点m条无向边的图,设一条节点递增的链末尾节点为u,链上点的个数为P,则该链 ...

  9. CodeForces Gym 100213F Counterfeit Money

    CodeForces Gym题目页面传送门 有\(1\)个\(n1\times m1\)的字符矩阵\(a\)和\(1\)个\(n2\times m2\)的字符矩阵\(b\),求\(a,b\)的最大公共 ...

随机推荐

  1. 【DFS】NYOJ-325-zb的生日

    [题目链接:NYOJ-325] 一道以我名字命名的题目,难道要我生日的时候再A? 思路:依旧深搜,但这个问题应该有一个专有名词吧,看别的博客说是 “容量为 sum/2 的背包问题”,不懂... // ...

  2. window下版本控制工具Git 客户端安装

    安装使用 1.下载msysgit http://code.google.com/p/msysgit/ 2.下载tortoisegit客户端安装 http://code.google.com/p/tor ...

  3. [转载] python+Eclipse+pydev环境搭建

    转自:http://www.cnblogs.com/Bonker/p/3584707.html 编辑器:Python 自带的 IDLE 简单快捷, 学习Python或者编写小型软件的时候.非常有用. ...

  4. DevExpress控件XtraGrid的Master-Detail用法 z

    XtraGrid支持Master-Detail展示,在自带的Demo中展示了一个“公司——产品——订单”的例子.自己照着实现了一下,有几处关键地方补充一下. 示例: 部门信息(主1)——部门下用户(从 ...

  5. leveldb源码笔记

    关于KV数据库leveldb的介绍,网上已经太多了,这里只是自己再学习源码过程中,整理的笔记,磁盘存储和内存存储的结构用了伪代码表示出来了,首先是内存中存储结构,然后是log文件存储结构和磁盘数据ss ...

  6. IOCP模型

    IOCP http://blog.csdn.net/zhongguoren666/article/details/7386592 Winsock IO模型之IOCP模型 http://blog.csd ...

  7. 在word中显示漂亮的代码

    在word中粘贴或写代码时,通常得不到想要的格式,可用‘Notepad++’工具实现. 步骤: (1)安装Notepad++软件,把代码粘贴进去,选择菜单栏中的语言,然后选择相应代码语言,如P-> ...

  8. STM32F407 外扩SRAM

    字节控制功能.支持高/低字节控制. 看看实现 IS62WV51216 的访问,需要对 FSMC进行哪些配置. 这里就做一个概括性的讲解.步骤如下: 1)使能 FSMC 时钟,并配置 FSMC 相关的  ...

  9. bzoj 3207 花神的嘲讽计划Ⅰ(哈希法+主席树)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3207 [题意] 给定一个文本串,多次询问K长的模式串是否在文本[l,r]区间内出现. ...

  10. php里面为什么header之前有输出报错 源码分析

    众所周知,php 里面 header之前有输出的话,会报错,例如下面这样   就这个错误,我们开始查阅php源代码,到底是怎样做的,至于php源代码分析,安装,和调试时怎样配置的,我会专门写一篇文章去 ...