蒟蒻不懂,求助大佬
查看原帖
蒟蒻不懂,求助大佬
261262
WaltVBAlston楼主2021/12/8 18:22

RT,蒟蒻写的是三队列广搜,不知道为什么在自己造的小数据店里面挂了(实际80pts),但是检查了一下算法并没有什么问题啊,求助!

input:

4 7
1 1 0
1 2 0
1 4 0
2 1 0
2 3 1
3 4 1
4 4 1

output:

5

我的output:

4

code:

#include<bits/stdc++.h>
using namespace std;
int n,m,chess[105][105],cost[105][105];
struct node{
	int nowx,nowy,last_col;
	bool used;
};
queue <node> q0,q1,q2;
int dx[4]={1,-1,0,0},dy[4]={0,0,1,-1};
pair <int,int> pre[105][105];
node get_head(){
	node ans;
	if(q0.size()){
		ans=q0.front();
		q0.pop();
	}
	else if(q1.size()){
		ans=q1.front();
		q1.pop();
	}
	else{
		ans=q2.front();
		q2.pop();
	}
	return ans;
}
int get_cost(node from,int tox,int toy){
	if(from.last_col==chess[tox][toy])
		return 0;
	else if(from.last_col!=chess[tox][toy]&&chess[tox][toy]!=-1)
		return 1;
	else if(chess[tox][toy]==-1)
		return 2;
}
int main(){
	memset(chess,-1,sizeof(chess));
	memset(cost,0x3f,sizeof(cost));
	cin>>n>>m;
	while(m--){
		int temp_x,temp_y,temp_col;
		cin>>temp_x>>temp_y>>temp_col;
		chess[temp_x][temp_y]=temp_col;
	}
	cout<<endl<<endl;
	q0.push((node){1,1,chess[1][1],false});
	cost[1][1]=0;
	while(q0.size()||q1.size()||q2.size()){
		node t=get_head();
		if((t.nowx==n&&t.nowy==n)||cost[t.nowx][t.nowy]>=cost[n][n])
			continue;
		//cout<<t.nowx<<" "<<t.nowy<<" "<<t.last_col<<" "<<cost[t.nowx][t.nowy]<<" "<<t.used<<endl;
		for(int i=0;i<4;i++){
			int tx=t.nowx+dx[i],ty=t.nowy+dy[i];
			if((chess[tx][ty]==-1&&t.used==true)||tx<1||tx>n||ty<1||ty>n)
				continue;
			int w=get_cost(t,tx,ty);
			int temp=cost[tx][ty];
			if(cost[tx][ty]<=cost[t.nowx][t.nowy]+w)
				continue;
			pre[tx][ty]=make_pair(t.nowx,t.nowy);
			cost[tx][ty]=cost[t.nowx][t.nowy]+w;
			if(w==0)
				q0.push((node){tx,ty,chess[tx][ty],false});
			else if(w==1)
				q1.push((node){tx,ty,chess[tx][ty],false});
			else{
				q2.push((node){tx,ty,chess[t.nowx][t.nowy],true});
				cout<<chess[t.nowx][t.nowy]<<" "<<tx<<" "<<ty<<endl;
			}
		}
	}
	if(cost[n][n]==1061109567)
		cout<<-1;
	else
		cout<<cost[n][n];
	cout<<endl;
	pair <int,int> before=pre[n][n],now=make_pair(n,n);
	while(now!=make_pair(1,1)){
		cout<<now.first<<" "<<now.second<<" "<<cost[now.first][now.second]<<endl;
		now=before;
		before=pre[before.first][before.second]; 
	}
	return 0;
}
2021/12/8 18:22
加载中...