解题思路:
暴力,照着题做就是,关键是只要路过终点,就可以直接结束当前步骤了。
然后代码看着长,好多都是复制粘贴的,但是也可以再优化。
参考代码:
#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 人评分