朕今日甚喜,写广搜一题,众爱卿若能调吾代码着,受重赏
查看原帖
朕今日甚喜,写广搜一题,众爱卿若能调吾代码着,受重赏
544069
chienyu楼主2025/8/5 17:08
#include<bits/stdc++.h>
using namespace std;
const int N = 105;
int m, x[N], y[N], t[N];
const int dx[] = {0, 0, 0, 1, -1};
const int dy[] = {0, 1, -1, 0, 0};
struct node
{
	int x, y, t;
};
bool vis[105][105];
int star[105][105];
int bfs(int sx, int sy)
{
	queue<node> q;
	memset(vis, 0, sizeof(vis));
	vis[sx][sy] = 1;
	q.push((node){sx, sy, 0});
	while(!q.empty())
	{
		node cur = q.front();
		q.pop();
		if(star[cur.x][cur.y] == 0x3f3f3f3f)
			return cur.t;
		for(int i = 1; i <= 4; i ++)
		{
			int nx = cur.x + dx[i];
			int ny = cur.y + dy[i];
			if(nx >= 1 && ny >= 1 && vis[nx][ny] == 0 && star[nx][ny] > cur.t + 1)
			{
				vis[nx][ny] = 1;
				q.push((node){nx, ny, cur.t + 1});
			}
		}
	}
	return -1;
}
int main()
{
	cin >> m;
	memset(star, 0x3f3f3f3f, sizeof(star));
	for(int i = 1; i <= m; i ++)
	{
		cin >> x[i] >> y[i] >> t[i];
		for(int j = 0; j <= 4; j ++)
		{
			int nx = x[i] + dx[j], ny = y[i] + dy[j];
			star[nx][ny] = min(star[nx][ny], t[i]);	
		}	
	}
	if(star[0][0] == 0)	cout << -1;
	else	cout << bfs(0, 0);	
	return 0;
}
2025/8/5 17:08
加载中...