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就好了,注意回溯就好了

代码:

  1. #include <cstdio>
  2. #include <cmath>
  3. #include <cstring>
  4. #include <ctime>
  5. #include <iostream>
  6. #include <algorithm>
  7. #include <set>
  8. #include <vector>
  9. #include <sstream>
  10. #include <queue>
  11. #include <typeinfo>
  12. #include <fstream>
  13. #include <map>
  14. #include <stack>
  15. typedef long long ll;
  16. using namespace std;
  17. //freopen("D.in","r",stdin);
  18. //freopen("D.out","w",stdout);
  19. #define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
  20. #define test freopen("test.txt","r",stdin)
  21. const int maxn=;
  22. #define mod 1000000007
  23. #define eps 1e-9
  24. const int inf=0x3f3f3f3f;
  25. const ll infll = 0x3f3f3f3f3f3f3f3fLL;
  26. inline ll read()
  27. {
  28. ll x=,f=;char ch=getchar();
  29. while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
  30. while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
  31. return x*f;
  32. }
  33. //*************************************************************************************
  34.  
  35. int dx[]={,,,-};
  36. int dy[]={,,-,};
  37. int vis[][];
  38. char s[][]={"NORTH","EAST","WEST","SOUTH"};
  39. char s1[];
  40.  
  41. void dfs(int x,int y,int to)
  42. {
  43. // cout<<x<<" "<<y<<" "<<vis[x][y]<<endl;
  44. for(vis[x][y]++;vis[x][y]<;vis[x][y]++)
  45. {
  46. int i=vis[x][y];
  47. if(-i==to) continue;
  48. printf("%s\n",s[i]);
  49. fflush(stdout);
  50. scanf("%s",s1);
  51. // cout<<s1<<endl;
  52. int xx=x+dx[i],yy=y+dy[i];
  53. // cout<<xx<<" "<<yy<<" "<<s1<<endl;
  54. if(s1[]=='E')
  55. {
  56. dfs(xx,yy,i);
  57. }
  58. }
  59. printf("%s\n",s[-to]);
  60. fflush(stdout);
  61. scanf("%s",s1);
  62. // cout<<s1<<endl;
  63. return ;
  64. }
  65.  
  66. int main()
  67. {
  68. memset(vis,-,sizeof(vis));
  69. for(vis[][]++;vis[][]<;vis[][]++)
  70. {
  71. int i=vis[][];
  72. printf("%s\n",s[i]);
  73. fflush(stdout);
  74. scanf("%s",s1);
  75. if(s1[]=='E')dfs(+dx[i],+dy[i],i);
  76. }
  77. printf("DONE\n");
  78. fflush(stdout);
  79. return ;
  80. }

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. 【Unity3D】Unity自带组件—完成第一人称人物控制

    1.导入unity自带的Character Controllers包 2.可以看到First Person Controller组件的构成 Mouse Look() : 随鼠标的移动而使所属物体发生旋 ...

  2. handler.post 为什么要将thread对象post到handler中执行呢?

    转载网址:http://www.cnblogs.com/crazypebble/archive/2011/03/23/1991829.html在Android中使用Handler和Thread线程执行 ...

  3. Andorid-Fragment生命周期

    官网帮助文档链接: http://developer.android.com/guide/components/fragments.html Fragment的生命周期: Fragment与Activ ...

  4. HDU 5407 CRB and Candies

    题意:给一个正整数k,求lcm((k, 0), (k, 1), ..., (k, k)) 解法:在oeis上查了这个序列,得知答案即为lcm(1, 2, ..., k + 1) / (k + 1),而 ...

  5. delphi7在win7系统如何安装spcomm控件

    1.先准备好串口控件SPCOMM,例如把它放在F盘的工具安装文件夹下,等一下加载时需要用到. 2.打开delphi7软件. 3.按下上面的Component>Install  Component ...

  6. Easy Climb

    题意: 有n块石头,给定他们的高度,现保持第一和最后一块高度不变,其他可增加和减少高度,求通过变换使所有相邻石头的高度差的绝对值不大于d,所变化高度总和的最小值. 分析: 状态还可以想出来,dp[i] ...

  7. Qt使用Cookies对网站操作之Get和POST

    1.添加QNetwork模块: a.Qt Creator中打开.pro文件添加QT+=Network; b.VS_Qt中项目属性中Qt Project Settings中Qmodules中勾选”QNe ...

  8. 导入CSV文件之后出现换行符问题

    在用sqlldr导入数据之后,出现数据无法匹配的情况 在用plsql点击的时候,发现出现换行符的情况,从而使用下面的方法进行匹配 select q.comments from q where repl ...

  9. C语言char[]和char*比较

    先看看一个例子: #include <iostream> using namespace std; main() { char *c1 = "abc"; char c2 ...

  10. reverse the string word by word

    题目:Given an input string, reverse the string word by word. For example,Given s = "the sky is bl ...