解题思路:
暴力,照着题做就是,关键是只要路过终点,就可以直接结束当前步骤了。
然后代码看着长,好多都是复制粘贴的,但是也可以再优化。
参考代码:
#include<bits/stdc++.h> using namespace std; char mapp[51][51]; int main() { int to; cin >> to; while (to--) { int n,s=0,t=0,s2=0,t2=0; cin >> n; for (int i = 0; i < n; i++) { scanf("%s",mapp[i]); } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (mapp[i][j] == 'S') { s = i; t = j; } else if (mapp[i][j] == 'T') { s2 = i; t2 = j; } } } int num; cin >> num; while (num--) { char ins[1001], * q; int s1 = s, t1 = t; scanf("%s",ins); q = ins; int count = 0; while (*q) { if (*q == 'L') { t1--; if (s1 == s2 && t1 == t2) { cout << "I get there!" << endl; count = 1; break; } if (t1<0 || t1>n - 1) { cout << "I am out!" << endl; count = 1; break; } if (mapp[s1][t1]=='#') { cout << "I am dizzy!" << endl; count = 1; break; } } else if (*q == 'R') { t1++; if (s1 == s2 && t1 == t2) { cout << "I get there!" << endl; count = 1; break; } if (t1<0 || t1>n - 1) { cout << "I am out!" << endl; count = 1; break; } if (mapp[s1][t1] == '#') { cout << "I am dizzy!" << endl; count = 1; break; } } else if (*q == 'U') { s1--; if (s1 == s2 && t1 == t2) { cout << "I get there!" << endl; count = 1; break; } if (s1<0 || s1>n - 1) { cout << "I am out!" << endl; count = 1; break; } if (mapp[s1][t1] == '#') { cout << "I am dizzy!" << endl; count = 1; break; } } else if (*q == 'D') { s1++; if (s1 == s2 && t1 == t2) { cout << "I get there!" << endl; count = 1; break; } if (s1<0 || s1>n - 1) { cout << "I am out!" << endl; count = 1; break; } if (mapp[s1][t1] == '#') { cout << "I am dizzy!" << endl; count = 1; break; } } q++; } if (count == 0) { if (s1 == s2 && t1 == t2) cout << "I get there!" << endl; else cout << "I have no idea!"<<endl; } } } }
0.0分
4 人评分
C语言程序设计教程(第三版)课后习题8.4 (C语言代码)浏览:575 |
C语言考试练习题_保留字母 (C语言代码)浏览:638 |
震宇大神的杀毒软件 (C语言代码)浏览:1348 |
C语言训练-排序问题<1> (C++代码)浏览:632 |
C语言训练-阶乘和数* (C语言代码)-------- 呆板写法浏览:1396 |
C语言训练-大、小写问题 (C语言代码)浏览:792 |
C语言程序设计教程(第三版)课后习题6.11 (C语言代码)浏览:565 |
C语言程序设计教程(第三版)课后习题8.7 (C语言代码)浏览:609 |
求圆的面积 (C语言代码)浏览:1756 |
C语言程序设计教程(第三版)课后习题9.2 (C语言代码)浏览:573 |