14wa求助啊啊啊啊去啊
查看原帖
14wa求助啊啊啊啊去啊
980018
qqjddw楼主2024/3/4 18:28

93分,14wa,有无大佬帮忙看一下。

#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>

using namespace std;

typedef pair<int, int> PII; 

const int N = 310;

struct Node
{
	int x, y, t;
};
int m, res;
int t[N][N];
bool st[N][N];
int dx[5] = {0, -1, 1, 0, 0}, dy[5] = {0, 0, 0, -1, 1};

int bfs()
{
	queue<Node> q;
	st[0][0] = true;
	q.push({0, 0, 0});
	
	
	while(q.size())
	{
		auto tt = q.front();
		q.pop();
		int x = tt.x, y = tt.y, res = tt.t;
		
		if(t[x][y] == 1e9) return res;
		for(int i = 0; i < 5; i++)
		{
			int nx = x + dx[i], ny = y + dy[i];
			if(nx >= 0 && nx <= 300 && ny >= 0 && ny <= 300 && res + 1 < t[nx][ny] && !st[nx][ny])
			{
				st[nx][ny] = true;
				q.push({nx, ny, res + 1});	
			}	
		}
	}
	
	return -1;
}

int main()
{
	memset(t, 0x3f, sizeof t);
	scanf("%d", &m);
	for(int i = 1; i <= m; i++) 
	{
		int mx, my, mt;
		scanf("%d%d%d", &mx, &my, &mt);
		t[mx][my] = min(t[mx][my], mt);
		if(mx - 1 >= 0) t[mx - 1][my] = min(t[mx - 1][my], mt);
		t[mx + 1][my] = min(t[mx + 1][my], mt);
		if(my - 1 >= 0) t[mx][my - 1] = min(t[mx][my - 1], mt);
		t[mx][my + 1] = min(t[mx][my + 1], mt);	
	}
	
	cout << bfs() << endl;
		
	return 0;	
}
2024/3/4 18:28
加载中...