Poj OpenJudge 百练 1573 Robot Motion
来源:互联网 发布:淘宝steam激活码哪来的 编辑:程序博客网 时间:2024/05/29 17:06
1.Link:
http://poj.org/problem?id=1573
http://bailian.openjudge.cn/practice/1573/
2.Content:
Robot MotionTime Limit: 1000MS Memory Limit: 10000KTotal Submissions: 10856 Accepted: 5260Description
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 0Sample Output
10 step(s) to exit3 step(s) before a loop of 8 step(s)Source
Mid-Central USA 1999
3.Method:
模拟题,使用arr_mark保存行走路径,走到重复的路则为loop,出边界则为exit
特别注意 0 step(s) before a loop of 4 step(s) 的情况
如:
2 2 1
SW
EN
4.Code:
1 #include <iostream> 2 #include <cstring> 3 4 using namespace std; 5 6 //N,E,S,W 7 const int idx_x[] = {0,1,0,-1}; 8 const int idx_y[] = {-1,0,1,0}; 9 const char idx_ch[] = {'N','E','S','W'};10 const int num_d = 4;11 12 int main()13 {14 //freopen("D://input.txt","r",stdin);15 16 int y,x;17 int i;18 19 int w,h,s;20 cin >> h >> w >> s;21 22 while(h != 0 || w != 0 || s != 0)23 {24 int **arr_d = new int*[h];25 for(y = 0; y < h; ++y) arr_d[y] = new int[w];26 27 char ch;28 for(y = 0; y < h; ++y)29 {30 for(x = 0; x < w; ++x)31 {32 cin >> ch;33 for(i = 0; i < num_d; ++i) if(idx_ch[i] == ch) break;34 arr_d[y][x] = i;35 }36 }37 38 //for(y = 0; y < h; ++y)39 //{40 // for(x = 0; x < w; ++x)41 // {42 // cout << arr_d[y][x] << " ";43 // }44 // cout << endl;45 //}46 47 int **arr_mark = new int*[h];48 for(y = 0; y < h; ++y)49 {50 arr_mark[y] = new int[w];51 memset(arr_mark[y],0,sizeof(int) * w);52 }53 54 y = 0;55 x = s - 1;56 int path = 0;57 int nx,ny;58 while(!arr_mark[y][x])//loop59 {60 nx = x;61 ny = y;62 arr_mark[y][x] = ++path;63 64 x = nx + idx_x[arr_d[ny][nx]];65 y = ny + idx_y[arr_d[ny][nx]];66 67 if(y < 0 || y >= h || x < 0 || x >= w) break;//exit68 }69 70 if(y < 0 || y >= h || x < 0 || x >=w)71 {72 cout << path << " step(s) to exit" << endl;73 }74 else75 {76 cout << (arr_mark[y][x] - 1) << " step(s) before a loop of " << (arr_mark[ny][nx] - arr_mark[y][x] + 1) << " step(s)" << endl;77 }78 79 //for(y = 0; y < h; ++y)80 //{81 // for(x = 0; x < w; ++x) cout << arr_mark[y][x] << " ";82 // cout << endl;83 //}84 85 for(y = 0; y < h; ++y) delete [] arr_mark[y];86 delete [] arr_mark;87 88 for(y = 0; y < h; ++y) delete [] arr_d[y];89 delete [] arr_d;90 91 cin >> h >> w >> s;92 }93 94 //fclose(stdin);95 96 return 0;97 }
5.Reference:
http://poj.org/showmessage?message_id=123463
0 0
- Poj OpenJudge 百练 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
- POJ 1573 Robot Motion
- Poj 3117 World Cup
- Poj 3278 Catch That Cow
- Poj 3126 Prime Path
- xcode的快捷键
- Poj OpenJudge 百练 2632 Crashing Robots
- Poj OpenJudge 百练 1573 Robot Motion
- OpenCV学习(14) 细化算法(2)(转)
- List
- OpenCV学习(15) 细化算法(3)(转)
- Poj 2996 Help Me with the Game
- Poj 2993 Emag eht htiw Em Pleh
- 第15周项目3-指针的基本操作(new-delete)-输出最值
- 2014华图ios开发工程师笔试题及答案
- Poj 3087 Shuffle'm Up