解题思路:
参考代码:
#include<bits/stdc++.h> #define hh ios::sync_with_stdio(false),cin.tie(0),cout.tie(0) using namespace std; const int maxn=105; int t,n,q; int x,y,pos_x,pos_y; char mmp[maxn][maxn]; string cmd; int main() { //hh; cin>>t; while(t--) { memset(mmp,0,sizeof(mmp)); cin>>n; for(int i=1; i<=n; i++) { for(int j=1; j<=n; j++) { cin>>mmp[i][j]; if(mmp[i][j]=='S') { x=i,y=j; pos_x=i,pos_y=j; } } } cin>>q; while(q--) { cin>>cmd; x=pos_x,y=pos_y; for(int i=0; i<cmd.length(); i++) { if(cmd[i]=='L') { y--; } else if(cmd[i]=='R') { y++; } else if(cmd[i]=='U') { x--; } else if(cmd[i]=='D') { x++; } if(mmp[x][y]=='T') { cout<<"I get there!"<<endl; break; } else if(mmp[x][y]!='T'&&i==cmd.length()-1&&(x>=1&&x<=n&&y>=1&&y<=n)) { cout<<"I have no idea!"<<endl; break; } else if(mmp[x][y]=='#') { cout<<"I am dizzy!"<<endl; break; } else if(x<1||x>n||y<1||y>n) { cout<<"I am out!"<<endl; break; } } cmd.clear(); } } return 0; } //这份代码错误50%
然后我看了一下别人的题解,进行了一下修改,吧else if换成了if进行判断,就对了
#include<bits/stdc++.h> #define hh ios::sync_with_stdio(false),cin.tie(0),cout.tie(0) using namespace std; const int maxn=105; int t,n,q; int x,y,pos_x,pos_y; char mmp[maxn][maxn]; int main() { //hh; cin>>t; while(t--) { memset(mmp,0,sizeof(mmp)); cin>>n; for(int i=1; i<=n; i++) { for(int j=1; j<=n; j++) { cin>>mmp[i][j]; if(mmp[i][j]=='S') { x=i,y=j; pos_x=i,pos_y=j; } } } cin>>q; while(q--) { string cmd; cin>>cmd; x=pos_x,y=pos_y; for(int i=0; i<cmd.length(); i++) { if(cmd[i]=='L') y--; if(cmd[i]=='R') y++; if(cmd[i]=='U') x--; if(cmd[i]=='D') x++; if(mmp[x][y]=='T') { cout<<"I get there!"<<endl; break; } else if(mmp[x][y]=='#') { cout<<"I am dizzy!"<<endl; break; } else if(mmp[x][y]==0) { cout<<"I am out!"<<endl; break; } if(mmp[x][y]!='T'&&i==cmd.length()-1&&mmp[x][y]!=0) { cout<<"I have no idea!"<<endl; break; } } } } return 0; }
AC
0.0分
0 人评分
A+B for Input-Output Practice (III) (C++代码)浏览:864 |
C语言程序设计教程(第三版)课后习题8.9 (C++代码)浏览:853 |
点我有惊喜!你懂得!浏览:1229 |
妹子杀手的故事 (C语言代码)浏览:691 |
C语言考试练习题_排列 (C语言代码)浏览:1326 |
C语言程序设计教程(第三版)课后习题8.1 (C语言代码)浏览:531 |
字符逆序 (C语言代码)浏览:460 |
C语言程序设计教程(第三版)课后习题5.6 (C语言代码)浏览:564 |
DNA (C语言代码)浏览:750 |
1071题解浏览:493 |