W2,W4,W8,W10,BFS,求助
查看原帖
W2,W4,W8,W10,BFS,求助
1070264
TheRangerNick楼主2024/7/20 10:58
#include<bits/stdc++.h>
using namespace std;
const int M = 55;
const char* dirs = "NESW";
char mp[M][M];
int dir[M];
int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
int n, m, sx, sy;
int c;
bool vis[M][M][1010];
struct node {
	int x;
	int y;
	int step;
	node(int x, int y, int z): x(x), y(y), step(z) {};
};
int main(void) {
	scanf("%d %d", &n, &m);
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; ++j) {
			scanf("\n%c", &mp[i][j]);
			if (mp[i][j] == '*') {
				sx = i;
				sy = j;
				mp[i][j] = '.';
			}
		}
	}
	scanf("%d", &c);
	string a;
	for (int i = 1; i <= c; ++i) {
		cin >> a;
		dir[i] = strchr(dirs, a[0]) - dirs;
	}
	memset(vis, 0, sizeof(vis));
	queue<node>q;
	q.push(node(sx, sy, 0));
	vis[sx][sy][0] = 1;
	while (!q.empty()) {
		node u = q.front();
		q.pop();
		if (u.step == c) {
			mp[u.x][u.y] = '*';
			continue;
		}
		for (int i = 1;; ++i) {
			int steps = u.step + 1;
			int new_x = u.x + dx[dir[steps]] * i;
			int new_y = u.y + dy[dir[steps]] * i;
			if (new_x > n || new_x < 1 || mp[new_x][new_y] == 'X' || new_y > m || new_y < 1)
				break;
			if (vis[new_x][new_y][steps]) continue;
			q.push(node(new_x, new_y, steps));
			vis[new_x][new_y][steps] = 1;
		}
	}
	for (int i = 1; i <= n; ++i) {
		for (int j = 1; j <= m; ++j)
			printf("%c", mp[i][j]);
		puts("");
	}
	return 0;
}
2024/7/20 10:58
加载中...