1204. Maze Traversal
Input
For this problem, you will input the dimensions of a maze, as two integer values on the first line. Of the two numbers, the first is the number of rows and the second is the number of columns. Neither the number of rows nor columns will exceed 60.
Following these numbers will be a number of rows, as specified previously. In each row there will be a character for each column, with the tow terminated by the end of line. Blank spaces are corridors, asterisks are walls. There needn't be any exits from the maze.
Following the maze, will be an initial row and column specified as two integers on one line. This gives the initial position of the robot. Initially the robot will be facing North (toward the first row).
The remaining input will consist of commands to the robot, with any amount of interspersed white-space. The valid commands are:
R rotate the robot 90 degrees clockwise (to the right)
L rotate the robot
90 degrees counter-clockwise (to the left)
F move the robot forward unless a
wall prevents this in which case do nothing
Q quite the program, printing out
the current robot row, column and orientation.
Output
The final row and column must be integers separated by a space. The orientation must be one of N, W, S, E and separated from the column by a space.
Sample Input
7 8 ******** * * * ** * * * * * ** * * * * * * * ** ******** 2 4 RRFLFF FFR FF RFFQ
Sample Output
5 6 W
特别注意:
1、输入的行列号数跟数组内的下标数差一位,一定要注意转换。
2、读入字符串,特别是带有空白字符的字符串,一定还要注意回车符。
3、最后一定要注意输出最后一定要有一个回车符,不然会是PE错误,也就是输出格式错误。
printf("%d %d %c ",robot.row+1,robot.col+1,robot.ori);
1 #include <stdio.h> 2 #include <string.h> 3 4 int handle(char *ptr); 5 void move(char str); 6 7 8 struct R{ 9 int row;//行号 10 int col;//列号 11 char ori;//朝向 12 }robot; 13 char maze[60][60]; 14 15 int main(void) 16 { 17 int row,col,i,j; 18 char run[100]; 19 20 scanf("%d %d",&row,&col); 21 getchar(); 22 23 for(i=0;i<row;i++){ 24 for(j=0;j<col;j++) 25 maze[i][j]=getchar(); 26 getchar(); 27 } 28 29 scanf("%d %d",&robot.row,&robot.col); 30 getchar();//吸收回车 31 robot.ori = 'N'; 32 robot.row--; 33 robot.col--;//跟数组匹配。 34 35 while(1){ 36 gets(run); 37 if(handle(run) == 0) 38 break; 39 memset(run,0,sizeof(run)); 40 } 41 42 printf("%d %d %c ",robot.row+1,robot.col+1,robot.ori); 43 44 //system("PAUSE"); 45 return 0; 46 } 47 48 int handle(char *ptr) 49 { 50 char *next = ptr; 51 char c= *next; 52 while((c != '