Robot Motion
2016-03-22 20:03
337 查看
Description
![](http://7xjob4.com1.z0.glb.clouddn.com/af1fa5f57be25a1e9c85a7db5f80940e)
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
Sample Output
大道至简,能不用类,就别用类,用类会使问题复杂化。
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
3 6 5 NEESWE WWWESS SNWWWW 4 5 1 SESWE EESNW NWEEN EWSEN 0 0
Sample Output
10 step(s) to exit 3 step(s) before a loop of 8 step(s)
大道至简,能不用类,就别用类,用类会使问题复杂化。
#include<iostream> #include<cstdio> using namespace std; int main() { int row,column,begin; while(scanf("%d%d",&row,&column)!=EOF) { char map[100][100]={'\0'}; int mark[100][100]={0}; /* bool flag[100][100]={false};*/ if(row==0&&column==0) return 0; cin>>begin; if(row==0&&begin==0&&column==0) return 0; for(int i=0;i<row;i++) { cin>>map[i]; } int steps=0; int x=begin-1,y=0; bool beg=false; while(1) { if(x>=column||x<0||y<0||y>=row) { cout<<steps<<" step(s) to exit"<<endl; break; } if(mark[x][y]!=0) { cout<<mark[x][y]<<" step(s) before a loop of "<<steps-mark[x][y]<<" step(s)"<<endl; break; } if(x==begin-1&&y==0&&beg==true) { cout<<0<<" step(s) before a loop of "<<steps<<" step(s)"<<endl; break; } if(map[y][x]=='W') { mark[x][y]=steps; /* flag[x][y]==true;*/ steps++; x--; beg==true; continue; } if(map[y][x]=='E') { mark[x][y]=steps; steps++; x++; beg=true; continue; } if(map[y][x]=='N') { mark[x][y]=steps; steps++; y--; beg=true; continue; } if(map[y][x]=='S') { mark[x][y]=steps; steps++; y++; beg=true; continue; } } } } /* 3 6 5 NEESWE WWWESS SNWWWW 4 5 1 SESWE EESNW NWEEN EWSEN */
相关文章推荐
- 启动apache Fail
- 笔记1handler/广播/布局/AIDL
- 第四周【项目3-随机数函数应用于游戏】
- libcurl使用演示样例
- 实验一、命令解释程序的编写
- cocos2dx遇到的坑1
- 前端路由
- 随笔1
- D2RQ平台之本体操作
- Trick (八)——list 模拟 stack(Python)
- Java POI 读取Office excel (2003,2007)及相关jar包
- 新学到list的常用方法,做下记录
- 队列原理详解
- 本月有几天?
- 世界是数字的读书笔记
- DIV横向排列_CSS如何让多个div盒子并排同行显示
- Number Game(ZOJ3908)
- 大数据平台服务器重启流程
- MySQL--Merge存储引擎
- 项目进阶 之 集群环境搭建(三)多管理节点MySQL集群