求助大佬4个wa
查看原帖
求助大佬4个wa
382911
cwh1769楼主2021/2/14 20:45
#include<iostream>
#include<algorithm>
#include<iomanip>
#include<queue>
using namespace std;
int M, flag = 0;
const int INF = 1e9;
int dist[311][311];
int map[311][311];
int xx[4] = { 0,0,1,-1 };
int yy[4] = { 1,-1,0,0 };
void chang(int x, int y, int t) {//对地图进行时间覆盖(取最小的时间)
	map[x][y] = t;
	if (x - 1 >= 0)
		map[x - 1][y] = min(map[x - 1][y], t);
	map[x + 1][y] = min(map[x + 1][y], t);
	if (y - 1 >= 0)
		map[x][y - 1] = min(map[x][y - 1], t);
	map[x][y + 1] = min(map[x][y + 1], t);
}
struct node {
	int x;
	int y;
	int step;
};
queue<node>v;
void BFS() {
	node start;
	start.x = 0; start.y = 0; start.step = 0;
	dist[0][0] = 1;
	v.push(start);
	while (!v.empty()) {
		node now;
		now = v.front();
		v.pop();
		if (map[now.x][now.y] == INF) {
			cout << now.step;
			flag = 1;
			break;
		}
		for (int i = 0; i < 4; i++) {
			node next;
			next.x = now.x + xx[i];
			next.y = now.y + yy[i];
			next.step = now.step + 1;
			if (next.x < 0 || next.y < 0 || next.step >= map[next.x][next.y] || dist[next.x][next.y] == 1)continue;
			dist[next.x][next.y] = 1;
			v.push(next);
		}
	}
}
int main() {
	cin >> M;
	for (int i = 0; i <= 310; i++) {
		for (int j = 0; j <= 310; j++) {
			map[i][j] = INF;
		}
	}
	for (int i = 0; i < M; i++) {
		int x, y, z;
		cin >> x >> y >> z;
		//map[x][y] = z;
		chang(x, y, z);
	}
	BFS();
	if (flag == 0) {
		cout << "-1";
	}
	return 0;
}
2021/2/14 20:45
加载中...