解题思路:
参考代码:
#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 人评分
C语言训练-求s=a+aa+aaa+aaaa+aa...a的值 (C语言代码)浏览:613 |
C语言程序设计教程(第三版)课后习题1.5 (C语言代码)浏览:531 |
IP判断 (C语言代码)浏览:762 |
C语言程序设计教程(第三版)课后习题10.4 (C语言代码)浏览:879 |
a+b浏览:432 |
C语言程序设计教程(第三版)课后习题9.8 (C语言代码)浏览:664 |
DNA (C语言代码)浏览:746 |
Tom数 (C语言代码)浏览:492 |
C语言程序设计教程(第三版)课后习题11.1 (C语言代码)浏览:486 |
2003年秋浙江省计算机等级考试二级C 编程题(1) (C语言代码)浏览:511 |