思路 :
- 起点记录后改为普通点
- 给迷宫周围加一圈’#’
- 速度作用是循环,如果循环内碰撞,就return了
- 判断碰撞 :先是越界,还有’#’,然后就是斜向的判断了
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
#include <stack>
#include <unordered_set>
using namespace std;
const int N = 55;
int n, m, q;
char g[N][N];
int sx, sy;
int v, dir;
int dx[] = {
-1, -1, 0, 1, 1, 1, 0, -1};
int dy[] = {
0, 1, 1, 1, 0, -1, -1, -1};
bool move(char ch)
{
if (ch == 'L') dir = (dir + 7) % 8;
if (ch == 'R') dir = (dir + 1) % 8;
if (ch == 'U') v ++ ;
if (ch == 'D') v = max(v - 1, 0);
for (int i = 1; i <= v; i ++ )
{
int nx = sx + dx[dir], ny = sy + dy[dir];
if (nx < 1 || nx > n || ny < 1 || ny > m || g[nx][ny] == '#') return false;
if (dir == 1 && g[sx - 1][sy] == '#' && g[sx][sy + 1] == '#') return false;
if (dir == 3 && g[sx + 1][sy] == '#' && g[sx][sy + 1] == '#') return false;
if (dir == 5 && g[sx + 1][sy] == '#' && g[sx][sy - 1] == '#') return false;
if (dir == 7 && g[sx - 1][sy] == '#' && g[sx][sy - 1] == '#') return false;
sx = nx, sy = ny;
}
return true;
}
int main()
{
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; i ++ )
for (int j = 1; j <= m; j ++ )
{
cin >> g[i][j];
if (g[i][j] == '*') sx = i, sy = j, g[i][j] = '.';
}
cin >> q;
for (int i = 1; i <= q; i ++ )
{
char ch;
cin >> ch;
if (move(ch)) cout << sx << ' ' << sy << endl;
else
{
v = 0;
cout << "Crash! " << sx << ' ' << sy << endl;
}
}
return 0;
}