解题思路:
暴力,照着题做就是,关键是只要路过终点,就可以直接结束当前步骤了。
然后代码看着长,好多都是复制粘贴的,但是也可以再优化。
参考代码:
#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分
1 人评分
C语言网提供由在职研发工程师或ACM蓝桥杯竞赛优秀选手录制的视频教程,并配有习题和答疑,点击了解:
一点编程也不会写的:零基础C语言学练课程
解决困扰你多年的C语言疑难杂症特性的C语言进阶课程
从零到写出一个爬虫的Python编程课程
只会语法写不出代码?手把手带你写100个编程真题的编程百练课程
信息学奥赛或C++选手的 必学C++课程
蓝桥杯ACM、信息学奥赛的必学课程:算法竞赛课入门课程
手把手讲解近五年真题的蓝桥杯辅导课程
发表评论 取消回复