Robot Motion

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 8180    Accepted Submission(s): 3771

Problem Description




A robot has been programmed to follow the instructions in its path. Instructions for the next direction the robot is to move are laid down in a grid. The possible instructions are 



N north (up the page)

S south (down the page)

E east (to the right on the page)

W west (to the left on the page)



For example, suppose the robot starts on the north (top) side of Grid 1 and starts south (down). The path the robot follows is shown. The robot goes through 10 instructions in the grid before leaving the grid.



Compare what happens in Grid 2: the robot goes through 3 instructions only once, and then starts a loop through 8 instructions, and never exits.



You are to write a program that determines how long it takes a robot to get out of the grid or how the robot loops around.
 
Input
There will be one or more grids for robots to navigate. The data for each is in the following form. On the first line are three integers separated by blanks: the number of rows in the grid, the number of columns in the grid, and the number of the column in
which the robot enters from the north. The possible entry columns are numbered starting with one at the left. Then come the rows of the direction instructions. Each grid will have at least one and at most 10 rows and columns of instructions. The lines of instructions
contain only the characters N, S, E, or W with no blanks. The end of input is indicated by a row containing 0 0 0.
 
Output
For each grid in the input there is one line of output. Either the robot follows a certain number of instructions and exits the grid on any one the four sides or else the robot follows the instructions on a certain number of locations once, and then the instructions
on some number of locations repeatedly. The sample input below corresponds to the two grids above and illustrates the two forms of output. The word "step" is always immediately followed by "(s)" whether or not the number before it is 1.
 
Sample Input
  1. 3 6 5
  2. NEESWE
  3. WWWESS
  4. SNWWWW
  5. 4 5 1
  6. SESWE
  7. EESNW
  8. NWEEN
  9. EWSEN
  10. 0 0
 
Sample Output
  1. 10 step(s) to exit
  2. 3 step(s) before a loop of 8 step(s)
 
Source
 
Recommend
 

Statistic | Submit | Discuss | 

pid=1035" style="color:rgb(26,92,200); text-decoration:none">Note


前几天看到这道题 感觉看到英文就头大了。。就没做。。

o(︶︿︶)o 唉  迟早都要做了。。

刚就看了看 一个DFS而已、、

主要就推断环的地方。用一个vis数组标记一下 而且用vis数组存贮到当前位置须要的步数

  1. #include <stdio.h>
  2. #include <string.h>
  3. char map[15][15];
  4. int sum,m,n,flag,mark,mark_x,mark_y,vis[15][15];
  5. void bfs(int x,int y,int ant)
  6. {
  7. if(x<0||y<0||x==m||y==n)//假设出界 就证明可以出去了
  8. {
  9. sum=ant;
  10. return ;
  11. }
  12. if(vis[x][y])//自身成环 记录眼下的步数和坐标
  13. {
  14. flag=1;
  15. mark_x=x,mark_y=y;
  16. mark=ant;
  17. return ;
  18. }
  19. vis[x][y]=ant+1;
  20. if(map[x][y]=='W'&&!sum&&!flag)
  21. bfs(x,y-1,++ant);
  22. if(map[x][y]=='E'&&!sum&&!flag)
  23. bfs(x,y+1,++ant);
  24. if(map[x][y]=='N'&&!sum&&!flag)
  25. bfs(x-1,y,++ant);
  26. if(map[x][y]=='S'&&!sum&&!flag)
  27. bfs(x+1,y,++ant);
  28. }
  29. int main()
  30. {
  31. int s;
  32. while(scanf("%d %d %d",&m,&n,&s)!=EOF)
  33. {
  34. if(m==0&&n==0&&s==0)
  35. break;
  36. for(int i=0;i<m;i++)
  37. scanf("%s",map[i]);
  38. sum=flag=0;
  39. memset(vis,0,sizeof(vis));
  40. bfs(0,s-1,0);
  41. if(!flag)
  42. printf("%d step(s) to exit\n",sum);
  43. else
  44. printf("%d step(s) before a loop of %d step(s)\n",vis[mark_x][mark_y]-1,mark-vis[mark_x][mark_y]+1);
  45. }
  46. return 0;
  47. }

Robot Motion

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 8180    Accepted Submission(s): 3771

Problem Description




A robot has been programmed to follow the instructions in its path. Instructions for the next direction the robot is to move are laid down in a grid. The possible instructions are 



N north (up the page)

S south (down the page)

E east (to the right on the page)

W west (to the left on the page)



For example, suppose the robot starts on the north (top) side of Grid 1 and starts south (down). The path the robot follows is shown. The robot goes through 10 instructions in the grid before leaving the grid.



Compare what happens in Grid 2: the robot goes through 3 instructions only once, and then starts a loop through 8 instructions, and never exits.



You are to write a program that determines how long it takes a robot to get out of the grid or how the robot loops around.
 
Input
There will be one or more grids for robots to navigate. The data for each is in the following form. On the first line are three integers separated by blanks: the number of rows in the grid, the number of columns in the grid, and the number of the column in
which the robot enters from the north. The possible entry columns are numbered starting with one at the left. Then come the rows of the direction instructions. Each grid will have at least one and at most 10 rows and columns of instructions. The lines of instructions
contain only the characters N, S, E, or W with no blanks. The end of input is indicated by a row containing 0 0 0.
 
Output
For each grid in the input there is one line of output. Either the robot follows a certain number of instructions and exits the grid on any one the four sides or else the robot follows the instructions on a certain number of locations once, and then the instructions
on some number of locations repeatedly. The sample input below corresponds to the two grids above and illustrates the two forms of output. The word "step" is always immediately followed by "(s)" whether or not the number before it is 1.
 
Sample Input
  1. 3 6 5
  2. NEESWE
  3. WWWESS
  4. SNWWWW
  5. 4 5 1
  6. SESWE
  7. EESNW
  8. NWEEN
  9. EWSEN
  10. 0 0
 
Sample Output
  1. 10 step(s) to exit
  2. 3 step(s) before a loop of 8 step(s)
 
Source
 
Recommend
 

Statistic | Submit | 

problemid=1035" style="color:rgb(26,92,200); text-decoration:none">Discuss | 

pid=1035" style="color:rgb(26,92,200); text-decoration:none">Note


前几天看到这道题 感觉看到英文就头大了。。

就没做。

o(︶︿︶)o 唉  迟早都要做了。

。刚就看了看 一个DFS而已、、

主要就推断环的地方。

用一个vis数组标记一下 而且用vis数组存贮到当前位置须要的步数

  1. #include <stdio.h>
  2. #include <string.h>
  3. char map[15][15];
  4. int sum,m,n,flag,mark,mark_x,mark_y,vis[15][15];
  5. void bfs(int x,int y,int ant)
  6. {
  7. if(x<0||y<0||x==m||y==n)//假设出界 就证明可以出去了
  8. {
  9. sum=ant;
  10. return ;
  11. }
  12. if(vis[x][y])//自身成环 记录眼下的步数和坐标
  13. {
  14. flag=1;
  15. mark_x=x,mark_y=y;
  16. mark=ant;
  17. return ;
  18. }
  19. vis[x][y]=ant+1;
  20. if(map[x][y]=='W'&&!sum&&!flag)
  21. bfs(x,y-1,++ant);
  22. if(map[x][y]=='E'&&!sum&&!flag)
  23. bfs(x,y+1,++ant);
  24. if(map[x][y]=='N'&&!sum&&!flag)
  25. bfs(x-1,y,++ant);
  26. if(map[x][y]=='S'&&!sum&&!flag)
  27. bfs(x+1,y,++ant);
  28. }
  29. int main()
  30. {
  31. int s;
  32. while(scanf("%d %d %d",&m,&n,&s)!=EOF)
  33. {
  34. if(m==0&&n==0&&s==0)
  35. break;
  36. for(int i=0;i<m;i++)
  37. scanf("%s",map[i]);
  38. sum=flag=0;
  39. memset(vis,0,sizeof(vis));
  40. bfs(0,s-1,0);
  41. if(!flag)
  42. printf("%d step(s) to exit\n",sum);
  43. else
  44. printf("%d step(s) before a loop of %d step(s)\n",vis[mark_x][mark_y]-1,mark-vis[mark_x][mark_y]+1);
  45. }
  46. return 0;
  47. }

hdu1035 Robot Motion (DFS)的更多相关文章

  1. HDU-1035 Robot Motion

    http://acm.hdu.edu.cn/showproblem.php?pid=1035 Robot Motion Time Limit: 2000/1000 MS (Java/Others)   ...

  2. HDOJ(HDU).1035 Robot Motion (DFS)

    HDOJ(HDU).1035 Robot Motion [从零开始DFS(4)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架/双重DF ...

  3. (step 4.3.5)hdu 1035(Robot Motion——DFS)

    题目大意:输入三个整数n,m,k,分别表示在接下来有一个n行m列的地图.一个机器人从第一行的第k列进入.问机器人经过多少步才能出来.如果出现了循环 则输出循环的步数 解题思路:DFS 代码如下(有详细 ...

  4. poj1573&amp;&amp;hdu1035 Robot Motion(模拟)

    转载请注明出处:http://blog.csdn.net/u012860063? viewmode=contents 题目链接: HDU:pid=1035">http://acm.hd ...

  5. HDU1035 Robot Motion

    Problem Description A robot has been programmed to follow the instructions in its path. Instructions ...

  6. hdu 1035 Robot Motion(dfs)

    虽然做出来了,还是很失望的!!! 加油!!!还是慢慢来吧!!! >>>>>>>>>>>>>>>>> ...

  7. HDU-1035 Robot Motion 模拟问题(水题)

    题目链接:https://cn.vjudge.net/problem/HDU-1035 水题 代码 #include <cstdio> #include <map> int h ...

  8. [ACM] hdu 1035 Robot Motion (模拟或DFS)

    Robot Motion Problem Description A robot has been programmed to follow the instructions in its path. ...

  9. poj1573 Robot Motion

    Robot Motion Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12507   Accepted: 6070 Des ...

随机推荐

  1. 342 Power of Four 4的幂

    给定一个整数 (32位有符整数型),请写出一个函数来检验它是否是4的幂.示例:当 num = 16 时 ,返回 true . 当 num = 5时,返回 false.问题进阶:你能不使用循环/递归来解 ...

  2. Android ScrollView里嵌套RecyclerView时,在RecyclerView上滑动时出现卡顿(冲突)的现象

    最近在项目中遇到一个现象,一个界面有一个RecyclerView(GridView型的),外面套了一层ScrollView,通过ScrollView上下滚动,但是在滑动的时候如果是在RecyclerV ...

  3. [转帖]c++ 面试整理

    1. 继承方式 public    父类的访问级别不变 protected    父类的public成员在派生类编程protected,其余的不变 private        父类的所有成员变成pr ...

  4. [Android]异常5-throwable:java.lang.OutOfMemoryError: pthread_create

    背景:线程初始化耗时任务 异常原因: 可能一>多个new Thread()嵌套 解决办法有: 解决一>使用Handler分离new Thread()嵌套 注: 06-30 09:12:26 ...

  5. 《Java编程的逻辑》第四部分 文件

  6. C# 窗体 切换、重复显示等遗留问题解决(第五天)

    一.解决同一窗体多次点击重复显示BUG (1)点击弹出学校窗体 #region 弹出学校窗体 /// <summary> /// 弹出学校窗体 /// </summary> / ...

  7. RTL Compiler之synthesis flow

    1 generic RTL Compiler work flow 2 invoking RTL compiler RTL Compiler is invoked from the operating ...

  8. 我的 Windows 10 的基本配置

    Windows 10 的基本配置 功能性 开启 .Net Framework 3.5(包括 .NET 2.0 和 3.0) 旧版本 Windows 10 默认只安装了 .Net Framework 4 ...

  9. Centos永久路由添加教程

    Centos 永久路由添加,一张图看懂全部 blog地址:http://www.cnblogs.com/caoguo

  10. 解决richfaces自带的jquery

    项目里有个有史以来让人头疼的问题,就是前端的richfaces框架自带有jquery插件,而且好像总是在最后才加载,导致前面自己加载好的jquery版本的框架以及应用到jquery的其他前端框架生成的 ...