代码不过样例,但思路很清晰,求助大佬
查看原帖
代码不过样例,但思路很清晰,求助大佬
233779
Dune_楼主2020/11/29 00:36
#include<stdio.h>
#include<queue>
#include<string.h>
#include<algorithm>
using namespace std;
int m;
int the_world[330][330];
int vis[330][330];
int ans[330][330];
int dx[4] = {0,0,1,-1};
int dy[4] = {1,-1,0,0};
int total = 0;
int main()
{
	int t;
	int x,y;
	scanf("%d",&m);
	memset(the_world,-1,sizeof(the_world));
	for(int i = 1;i <= m; ++i)
	{
		scanf("%d%d",&x,&y);
		scanf("%d",&t);
		the_world[x][y] = t;
		for(int j = 0;j < 4; j++)
		{
			int nx = x+dx[j];
			int ny = y+dy[j];
			if(nx >= 0&&ny >= 0)
			{
				if(the_world[nx][ny] > t||the_world[nx][ny] == -1)
				{
					the_world[nx][ny] = t;
				}
			}
		}
	}
	vis[0][0] = 1;
	queue<int> q[2];
	q[0].push(0);
	q[1].push(0);
	while(!q[0].empty())
	{
		int xx = q[0].front();
		int yy = q[1].front();
		q[0].pop();
		q[1].pop();
		total = ans[xx][yy] + 1;
		if(the_world[xx][yy] == -1)
		{
			printf("%d",total-1);
			return 0;
		}
		for(int i = 0;i < 4; ++i)
		{
			int nowx = xx + dx[i];
			int nowy = yy + dy[i];
			if(the_world[nowx][nowy] == -1)
			{
				printf("%d",total);
				return 0;
			}
			if(nowx >= 0&&nowy >= 0)
			{
				if(vis[nowx][nowy] == 0&&total < the_world[nowx][nowy])
				{
					q[0].push(nowx);
					q[1].push(nowy);
					vis[nowx][nowy] = 1;
					ans[nowx][nowy] = total;
				}
			}
		}
	}
	printf("-1");
	return 0;
}
2020/11/29 00:36
加载中...