样例没过玄关求条
查看原帖
样例没过玄关求条
1181602
Luowj楼主2024/9/17 22:16
#include<bits/stdc++.h>
using namespace std;
struct node {
	int x, y, t;
}a[500005];
int f[410][410], m, ans, b[410][410];
int dx[5] = {0, 1, -1, 0, 0};
int dy[5] = {0, 0, 0, 1, -1};
bool cmp(node a, node b) {
	return a.t < b.t;
}
void dfs(int x, int y, int cnt) {
	//cout << x << " " << y <<" " << cnt << " " << f[x][y]<< endl;
	if(f[x][y] == 1e9) {
		cout << cnt << endl;
		exit(0);
	}
	for(int i = 1;i <= 4;i ++) {
		int xx = x + dx[i];
		int yy = y + dy[i];
		if(f[xx][yy] > cnt + 1 && xx < 400 && yy < 400 && xx >= 0 && yy >= 0 && b[xx][yy] == 0) {
			b[xx][yy] = 1;
			dfs(xx, yy, cnt + 1);
		}
	}
	cout << -1 << endl;
	exit(0);
}
signed main() {
	for(int i = 0;i < 400;i ++) {
		for(int j = 0;j < 400;j ++) {
			f[i][j] = 1e9;
		}
	}
	cin >> m;
	for(int i = 1;i <= m;i ++) {
		cin >> a[i].x >> a[i].y >> a[i].t;
	}
	sort(a + 1, a + m + 1, cmp);
	for(int i = 1;i <= m;i ++) {
		int x = a[i].x, y = a[i].y, t = a[i].t;
		f[x][y] = t;
		f[x - 1][y] = t;
		f[x + 1][y] = t;
		f[x][y - 1] = t;
		f[x][y + 1] = t;
	}
	dfs(0, 0, 0);
}
2024/9/17 22:16
加载中...