难度:5
模拟
做这道题是因为它比较简单,只有两种操作,一个转向一个是前进,转向是比较简单的,麻烦一点的是前进操作,但是只是分类讨论,两种情况,
第一种情况,判断当前点在不在这个前进方向的边上,在的话不移动,跳过,
第二种情况,找后面连续的障碍数,先用while循环找到当前方向上,最后一个连续障碍的位置,此时,这个障碍的前进一格,要么是空地,要么是超出范围1格,但是不用分类讨论,因为输入的时候1到8的下标存储数据,所以周围是有一格的缓冲地带的,不会超出存储区域,如果是0到7存储数据就需要分类讨论了(一开始就是这么写的)然后就是循环就行了,把这些连续的障碍连同人都向前移动一格,因为前面要么是空地要么是超出范围一格,所以不需要特判,超出范围一格的东西也不需要输出,里面放的什么不用关心,它的作用就是容纳刚刚推出去的箱子,或者说是统一写法,就类似于链表初始化的时候直接设置头节点尾结点一样,
最后不要忘记,因为一开始把放人的地方改成了空地,只是用坐标来记录人的位置,输出前要改成相应的方向,再输出
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define mk make_pair
#define sz(x) ((int) (x).size())
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pa;
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};
char str[5] = "^>v<";
int main() {
char s[10][10];
int dir;
int x, y;
for (int i = 1; i <= 8; i++) {
for (int j = 1; j <= 8; j++) {
cin >> s[i][j];
if (s[i][j] == '^') dir = 0;
if (s[i][j] == '>') dir = 1;
if (s[i][j] == 'v') dir = 2;
if (s[i][j] == '<') dir = 3;
if (s[i][j] == '^' || s[i][j] == '>' || s[i][j] == 'v' || s[i][j] == '<') { x = i; y = j; s[i][j] = '.'; }
}
}
string order;
while (cin >> order && order != "#") {
if (order == "move") {
int step;
cin >> step;
while (step--) {
if (x == 1 && !dir || y == 8 && dir == 1 || x == 8 && dir == 2 || y == 1 && dir == 3) continue;
int r = x + dx[dir];
int c = y + dy[dir];
while (r > 0 && r < 9 && c > 0 && c < 9 && isalpha(s[r][c])) {
r += dx[dir];
c += dy[dir];
}
r -= dx[dir];
c -= dy[dir];
if (dir == 0) {
for (int i = r - 1; i < x; i++) s[i][y] = s[i + 1][y];
} else if (dir == 1) {
for (int i = c + 1; i > y; i--) s[x][i] = s[x][i - 1];
} else if (dir == 2) {
for (int i = r + 1; i > x; i--) s[i][y] = s[i - 1][y];
} else {
for (int i = c - 1; i < y; i++) s[x][i] = s[x][i + 1];
}
x += dx[dir];
y += dy[dir];
}
} else {
string str;
cin >> str;
if (str == "right") {
if (dir == 3) dir = 0;
else dir++;
} else if (str == "left") {
if (dir == 0) dir = 3;
else dir--;
} else {
if (dir == 0) dir = 2;
else if (dir == 2) dir = 0;
else if (dir == 1) dir = 3;
else dir = 1;
}
}
}
s[x][y] = str[dir];
for (int i = 1; i <= 8; i++) {
for (int j = 1; j <= 8; j++) cout << s[i][j] << (j == 8 ? "\n" : "");
}
return 0;
}