POJ 1573:Robot Motion
来源:互联网 发布:中国广电网络投诉电话 编辑:程序博客网 时间:2024/06/13 06:41
Robot Motion
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 11324 Accepted: 5512
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
3 6 5NEESWEWWWESSSNWWWW4 5 1SESWEEESNWNWEENEWSEN0 0 0
Sample Output
10 step(s) to exit3 step(s) before a loop of 8 step(s)
突然爱上了模拟题,要是所有题都像这样的题目只需模拟一下走的方向,而不考虑算法的该有多好。。。哈哈
从头开始走,记录第几步走在了哪一个格子上,如果要走的格子大于0了,说明走重复了。这题比较水。
代码:
#include <iostream>#include <algorithm>#include <cmath>#include <vector>#include <string>#include <cstring>#pragma warning(disable:4996)using namespace std;enum{ E,N,W,S }; int map_f[20][20];char call[20][20];int move_x[5]={0,-1,0,1}; int move_y[5]={1,0,-1,0}; int X,Y,start,i,cur_x,cur_y,flag;void solve(){int temp;cur_x = 1;cur_y = start;map_f[cur_x][cur_y]=1;while(1){if(flag)break;if(call[cur_x][cur_y]=='E'){temp=map_f[cur_x][cur_y];cur_x = cur_x + move_x[E];cur_y = cur_y + move_y[E];if(cur_x<=0 || cur_y<=0 || cur_x>X || cur_y>Y){flag=1;cout<<temp<<" step(s) to exit"<<endl;}else if(map_f[cur_x][cur_y]){flag=1;cout<<map_f[cur_x][cur_y]-1<<" step(s) before a loop of "<<temp-map_f[cur_x][cur_y]+1<<" step(s)"<<endl;}else{map_f[cur_x][cur_y]=temp+1;}}else if(call[cur_x][cur_y]=='N'){temp=map_f[cur_x][cur_y];cur_x = cur_x + move_x[N];cur_y = cur_y + move_y[N];if(cur_x<=0 || cur_y<=0 || cur_x>X || cur_y>Y){flag=1;cout<<temp<<" step(s) to exit"<<endl;}else if(map_f[cur_x][cur_y]){flag=1;cout<<map_f[cur_x][cur_y]-1<<" step(s) before a loop of "<<temp-map_f[cur_x][cur_y]+1<<" step(s)"<<endl;}else{map_f[cur_x][cur_y]=temp+1;}}else if(call[cur_x][cur_y]=='W'){temp=map_f[cur_x][cur_y];cur_x = cur_x + move_x[W];cur_y = cur_y + move_y[W];if(cur_x<=0 || cur_y<=0 || cur_x>X || cur_y>Y){flag=1;cout<<temp<<" step(s) to exit"<<endl;}else if(map_f[cur_x][cur_y]){flag=1;cout<<map_f[cur_x][cur_y]-1<<" step(s) before a loop of "<<temp-map_f[cur_x][cur_y]+1<<" step(s)"<<endl;}else{map_f[cur_x][cur_y]=temp+1;}}else if(call[cur_x][cur_y]=='S'){temp=map_f[cur_x][cur_y];cur_x = cur_x + move_x[S];cur_y = cur_y + move_y[S];if(cur_x<=0 || cur_y<=0 || cur_x>X || cur_y>Y){flag=1;cout<<temp<<" step(s) to exit"<<endl;}else if(map_f[cur_x][cur_y]){flag=1;cout<<map_f[cur_x][cur_y]-1<<" step(s) before a loop of "<<temp-map_f[cur_x][cur_y]+1<<" step(s)"<<endl;}else{map_f[cur_x][cur_y]=temp+1;}}}}int main(){while(cin>>X>>Y>>start){if(!(X+Y+start))break;memset(map_f,0,sizeof(map_f));flag=0;for(i=1;i<=X;i++){cin>>call[i]+1;}solve();}return 0;}
0 0
- poj 1573 Robot Motion
- Poj 1573 Robot Motion
- poj 1573 Robot Motion
- POJ 1573 - Robot Motion
- poj-1573-Robot Motion
- POJ 1573 Robot Motion
- POJ 1573 Robot Motion
- POJ-1573-Robot Motion
- poj 1573 Robot Motion
- POJ 1573 Robot Motion
- POJ 1573 Robot Motion.
- poj 1573 Robot Motion
- POJ -1573 Robot Motion
- POJ 1573:Robot Motion
- poj 1573 Robot Motion
- poj 1573 Robot Motion
- POJ 1573 Robot Motion
- Robot Motion POJ 1573
- 简单谈谈对Object C的理解
- 快速排序 归并排序
- hdu 3511 圆扫描线
- 【读书笔记】iOS-GCD-block
- Python GUI编程(Tkinter)
- POJ 1573:Robot Motion
- FLAG_ACTIVITY_CLEAR_TOP和FLAG_ACTIVITY_REORDER_TO_FRONT用法
- sql笔记
- MFC中CWnd类及其派生类对话框、消息处理、窗口操作
- arrlist
- [LeetCode]Merge Sorted Array
- Android 之 GridView初探
- 文件上传中submit与ajax的问题与思考
- Redis 集群