解题思路:
暴力,照着题做就是,关键是只要路过终点,就可以直接结束当前步骤了。
然后代码看着长,好多都是复制粘贴的,但是也可以再优化。
参考代码:
#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 人评分
矩形面积交 (Java代码)浏览:1212 |
【亲和数】 (C语言代码)浏览:853 |
字符串比较 (C语言代码)答案错误????浏览:595 |
C语言程序设计教程(第三版)课后习题7.5 (C语言代码)浏览:519 |
WU-C语言程序设计教程(第三版)课后习题11.11 (C++代码)(想学链表的可以看看)浏览:1342 |
C语言程序设计教程(第三版)课后习题8.8 (C语言代码)浏览:1414 |
A+B for Input-Output Practice (VI) (C语言代码)浏览:552 |
C语言程序设计教程(第三版)课后习题9.4 (C语言代码)浏览:628 |
GC的苦恼 (C语言代码)浏览:618 |
钟神赛车 (C语言代码)浏览:590 |