社区讨论

0分求调,在线等!!

P2199最后的迷宫参与者 2已保存回复 3

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
3 条
当前快照
1 份
快照标识符
@lo1ttm1n
此快照首次捕获于
2023/10/23 02:52
2 年前
此快照最后确认于
2023/11/03 03:26
2 年前
查看原帖
CPP
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<cstring>
using namespace std;
struct node{
	int x, y;
}s, t;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int sdx[8] = {1, 0, -1, 0, 1, 1, -1, -1};
int sdy[8] = {0, 1, 0, -1, 1, -1, 1, -1};
char a[1005][1005];
int dis[1005][1005];
queue <node> q;
int main(){
	int n, m;
	scanf("%d%d", &n, &m);
	for(int i = 1; i <= n; i++)for(int j = 1; j <= m; j++)scanf("%c", &a[i][j]);
	while(cin >> s.x >> s.y >> t.x >> t.y && s.x)
	{
		for(int i = 1; i <= n; i++){
			for(int j = 1; j <= m; j++){
				dis[i][j] = -1;
				if(a[i][j] == 'S')a[i][j] = 'O';
			}
		}
		for(int i = 0; i < 8; i++)
		{
			int sx = s.x, sy = s.y;
			while(true)
			{
				a[sx][sy] = 'S';
				sx += sdx[i];
				sy += sdy[i];
				if(sx > n || sy > m || sx < 1 || sy < 1 || a[sx][sy] == 'X')
				{
					break;
				}
			}
		}
		int flag = false;
		q.push({t.x,t.y});
		dis[t.x][t.y] = 0;
		while(!q.empty() && !flag)
		{
			node now = q.front();q.pop();
			int x = now.x, y = now.y, d = dis[x][y];
			for(int i = 0; i < 4; i++)
			{
				int nx = x + dx[i], ny = y + dy[i];
				if(nx < 1 || nx > n || ny < 1 || ny > m)continue;
				if(a[nx][ny] == 0)continue;
				if(dis[nx][ny] != -1)continue;
				q.push({nx, ny});
				dis[nx][ny] = d + 1;
				if(a[nx][ny] == 'S')
				{
					flag = true;
					printf("%d\n", dis[nx][ny]);
					break;
				}
			}
		}
		if(dis[s.x][s.y] == -1)printf("Poor Harry\n");
	}
	return 0;
}

回复

3 条回复,欢迎继续交流。

正在加载回复...