[蓝桥杯][算法提高VIP]我们的征途是星辰大海

题目

题目链接

题解

实现题。


这也很基础,写代码的时候细心点就行。

代码

#include<bits/stdc++.h>
using namespace std;
const int N = 55;int T, Q, n, sx, sy, tx, ty;
string mp[N], op;int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, -1, 1};int main()
{cin>>T;while(T--) {cin>>n;for(int i = 0;i < n;i ++) {cin>>mp[i];for(int j = 0;j < n;j ++) if(mp[i][j] == 'S') sx = i, sy = j;else if(mp[i][j] == 'T') tx = i, ty = j;}cin>>Q;while(Q--) {cin>>op;int x = sx, y = sy, flag = 0;for(int i = 0;i < op.size();i ++) {int k;if(op[i] == 'U') k = 0;else if(op[i] == 'D') k = 1;else if(op[i] == 'L') k = 2;else if(op[i] == 'R') k = 3;int xx = x+dx[k], yy = y+dy[k];if(xx < 0 || yy < 0 || xx >= n || yy >= n) {flag = 1; puts("I am out!"); break;}if(mp[xx][yy] == '#') {flag = 1; puts("I am dizzy!"); break;}if(xx == tx && yy == ty) {flag = 1; puts("I get there!"); break;}x = xx, y = yy;}if(!flag) puts("I have no idea!");}} return 0;
}

本文链接:https://my.lmcjl.com/post/11036.html

展开阅读全文

4 评论

留下您的评论.