藍橋杯 ADV-201 演算法提高 我們的征途是星辰大海
最新的火星探測機器人curiosity被困在了一個二維迷宮裡,迷宮由一個個方格組成。
共有四種方格:
‘.’ 代表空地,curiosity可以穿過它
‘#’ 代表障礙物,不可穿越,不可停留
‘S’ 代表curiosity的起始位置
‘T’ 代表curiosity的目的地
輸入格式
第一行是一個整數T,代表有幾個測試樣例
輸出格式
對於每個詢問輸出單獨的一行:
“I get there!”:執行給出的命令後curiosity最終到達了終點。
“I have no idea!”:執行給出的命令後curiosity未能到達終點。
“I am dizzy!”:curiosity在執行命令的過程中撞到了障礙物。
“I am out!”:代表curiosity在執行命令的過程中走出了迷宮的邊界。
Sample Input
2
2
S.
#T
2
RD
DR
3
S.#
.#.
.T#
3
RL
DDD
DDRR
Sample Output
I get there!
I am dizzy!
I have no idea!
I am out!
I get there!
#include <iostream> #include <cstring> using namespace std; int main() { int k; cin >> k; while (k--) { int n, m, sx, sy, ex, ey; char cc[100][100]; memset(cc, '0', sizeof(cc)); cin >> n; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { scanf(" %c ", &cc[i][j]); if (cc[i][j] == 'S') { sx = i; sy = j; } if (cc[i][j] == 'T') { ex = i; ey = j; } } } cin >> m; while (m--) { int nowx = sx, nowy = sy, flag = 0; string s; cin >> s; for (int i = 0; i < s.length(); i++) { if (s[i] == 'R') nowy++; if (s[i] == 'L') nowy--; if (s[i] == 'U') nowx--; if (s[i] == 'D') nowx++; if (cc[nowx][nowy] == '#') { flag = 1; cout << "I am dizzy!\n"; break; } else if (cc[nowx][nowy] == '0') { flag = 1; cout << "I am out!\n"; break; } else if (cc[nowx][nowy] == 'T') { flag = 1; cout << "I get there!\n"; break; } } if (flag == 0) cout << "I have no idea!\n"; } } return 0; }ofollow,noindex" target="_blank">❤❤點選這裡 -> 訂閱PAT、藍橋杯、GPLT天梯賽、LeetCode題解離線版❤❤
