//逻辑有点烦 #include <iostream> #include <fstream> #include <cmath> #include <string> #include <iomanip> #include <algorithm> #include <stdio.h> //using namespace std; std::ifstream cin("in.txt"); std::ofstream cout("out.txt"); int maze[60][60]; int n,m; int robot_row,rotbot_column; int orientation; void solve(char cmd) { int tmp = 0; if (cmd == 'R') { orientation =(orientation+3)%12; if (orientation==0) { orientation = 12; } } else if (cmd == 'L') { orientation =(orientation-3)%12; if (orientation==0) { orientation = 12; } } else { if (orientation == 12) { tmp = robot_row-1; if (tmp==-1) { tmp =0; } if (maze[tmp][rotbot_column]) { robot_row = tmp; } } else if (orientation == 3) { tmp = rotbot_column+1; if (tmp==m) { tmp =m; } if (tmp<=m&&maze[robot_row][tmp]) { rotbot_column = tmp; } } else if (orientation == 6) { tmp = robot_row+1; if (tmp==n) { tmp =n; } if (tmp<=n&&maze[tmp][rotbot_column]) { robot_row = tmp; } } else if (orientation == 9) { tmp = rotbot_column-1; if (tmp==-1) { tmp =0; } if (maze[robot_row][tmp]) { rotbot_column = tmp; } } } } void output() { cout<<robot_row+1<<" "<<rotbot_column+1<<" "; switch(orientation) { case 12:cout<<"N"; break; case 3:cout<<"E"; break; case 6:cout<<"S"; break; case 9:cout<<"W"; break; default: cout<<"/n"; break; } } int main() { while (cin>>n>>m) { std::memset(maze,0,sizeof(maze)); std::string maze_str; getline(cin,maze_str); for (int i = 0; i<n ; i++) { getline(cin,maze_str); for (int j = 0; j<maze_str.length(); j++) { char c = maze_str[j]; if (c==' ') { maze[i][j] = 1; } else { maze[i][j] = 0; } } } cin>>robot_row>>rotbot_column; robot_row--; rotbot_column--; orientation = 12; bool flag_maze = true; std::string str; while (getline(cin,str)) { bool flag = false; for (int i = 0; i<str.length(); i++) { if (str[i] == 'Q') { flag = true; break; } if (str[i] == 'R'||str[i]=='L'||str[i]=='F') { solve(str[i]); } } if (flag) { break; } } output(); cout<<"/n"; } } 别人简单的代码: #include <stdio.h> #include <string.h> int r,c,nowr,nowc,dir; char maze[60][65]; char command[5000],*s; int diry[4]={-1,0,1,0}; int dirx[4]={0,1,0,-1}; char d[4]={'N','E','S','W'}; int main() { while(scanf("%d %d",&r,&c) == 2) { gets(command); for(int i=0;i<r;i++) gets(maze[i]); gets(command); sscanf(command,"%d %d",&nowr,&nowc); nowr--;nowc--;dir=0; while(true) { gets(command); if(strcmp(command,"")==0) break; s=command; while(*s) { if(*s=='Q') goto End; else if(*s=='R') dir=(dir+1)%4; else if(*s=='L') dir=(dir+3)%4; else if(*s=='F') { int tr=nowr+diry[dir]; int tc=nowc+dirx[dir]; if(maze[tr][tc]!='*'&&tr>=0&&tr<r&&tc>=0&&tc<c) { nowr=tr;nowc=tc; } } s++; } } End: printf("%d %d %c/n",nowr+1,nowc+1,d[dir]); } return 1; }