35分求调
查看原帖
35分求调
136827
Jack2007Q楼主2025/8/31 11:22

知道这个方法不好,后面好多mle的,但是想知道为什么有一些测试点会WA 其中一个 in:

23
2 5 10
1 3 5
5 3 12
3 3 9
1 8 7
8 4 15
2 3 7
0 0 2
6 7 10
4 4 10
3 7 7
8 5 13
0 4 9
2 6 8
0 2 4
6 4 12
0 6 7
4 2 10
1 4 7
4 6 10
5 5 12
6 5 14
2 1 2

out:13

#include <iostream>
#include <queue>
using namespace std;
struct pos{
	int x,y,cnt;
}temp,temp2;
bool v[400][400];
int ans = -1;
int x[1001][50001],y[1001][50001];
queue<pos> q;
int m;
int tx,ty,tt;
int cnt[1001];
int maxx,maxy,maxt;
bool check[301][301];
int dirx[4]={0,0,-1,1};
int diry[4] = {1,-1,0,0};
void bfs(){
	for(int i=0;i<=maxt;i++){
		//cout << 'i' << i<<endl;
			for(int j =0; j<cnt[i]; j++){	
				v[x[i][j]][y[i][j]] = 1;
				if(x[i][j] + 1 <= 300){
					
					v[x[i][j] + 1][y[i][j]] = 1;
				}
				if(x[i][j] - 1 >=0){
									
					v[x[i][j] -1][y[i][j]] = 1;
								
				}
				if(y[i][j] +1 <= 300){
					
					v[x[i][j]][y[i][j] +1 ] = 1;
				}
				if(y[i][j] -1>=0){
					
					v[x[i][j]][y[i][j] -1] = 1;
				}
			}
			/*
			for(int k =0;k <=10; k++){
							for (int j =0; j<=10;j++){
								cout << v[k][j] << ' ';
							}
							cout << endl;
						}*/
		//TODO
		while(1){
				//TODO
			
			temp = q.front();
			//cout << temp.x<<' '<< temp.y <<' '<< temp.cnt << i<<endl;
			if(temp.cnt != i or q.empty()){
				//cout<<'f' << temp.x<<' '<< temp.y<<endl;
				break;
			}
			q.pop();
			if(!check[temp.x][temp.y]){
				//cout << "d"<<endl;
				ans = temp.cnt;
				break;
			}
			
			for (int j=0;j<4;j++){
				temp2.x = temp.x + dirx[j];
				temp2.y= temp.y + diry[j];
				temp2.cnt = temp.cnt + 1;
				if(!v[temp2.x][temp2.y] and temp2.x >=0 and temp2.x <= 300 and temp2.y >= 0 and temp2.y <= 300){
					q.push(temp2);
					v[temp2.x][temp2.y] = 1;
					//cout<<'y' << temp2.x<<' '<< temp2.y << ' '<<temp.cnt<<endl;
				} 
			}
		}
	}
}
void checkfunc(){
	for(int i = 0; i <= maxt; i++){
		for(int j =0; j<cnt[i]; j++){
			
			check[x[i][j]][y[i][j]] = 1;
			check[x[i][j] + 1][y[i][j]] = 1;
			check[x[i][j] -1][y[i][j]] = 1;
			check[x[i][j]][y[i][j] -1 ] = 1;
			check[x[i][j]][y[i][j] +1] = 1;
		}
	}
}
int main(){
	cin >> m;
	for(int i = 0; i<m;i++){
		cin >> tx>>ty>> tt;
		maxt = max(maxt , tt);
		maxx = max(maxx, tx);
		maxy= max(maxy,ty);
		x[tt][cnt[tt]] = tx;
		y[tt][cnt[tt]] = ty;
		cnt[tt]++;
	}
	checkfunc();
	temp.x=0;
	temp.y=0;
	temp.cnt=0;
	v[0][0]=1;
	q.push(temp);
	/*
	cout << "maxt:"<<maxt<<endl;
	for(int i =0; i <= maxt; i++){
			cout<<'d' << cnt[i]<<endl;
		}*/
	bfs();
	
	cout << ans << endl;
	return 0;
}


2025/8/31 11:22
加载中...