有坑勿踩
查看原帖
有坑勿踩
801978
sane1981楼主2022/10/21 14:36

注意啦!
有一个特别恶心的特判!!
我就被坑了!

#include<bits/stdc++.h>
using namespace std;
const int dir[4][2]={{-1,0},{0,1},{1,0},{0,-1}};
int a[105][105];
int n,m,w1,w2,c;
struct node{
	int nowx,nowy,step,color;
	bool use;
	node(){};
	node(int u,int v,int x,int y,bool z){
		nowx=u;nowy=v;step=x;color=y;use=z;
	}
	friend bool operator <(node x,node y){
		return x.step>y.step;
	}
};
priority_queue<node> q;
bool vis[105][105],is_break;
void BFS(int x,int y){
//	cout<<"----------\n";
	vis[x][y]=true;
	q.push(node(x,y,0,a[x][y],false));
	int xx,yy,ss;
	node t;
	while(!q.empty()){
		for(int i=0;i<4;i++){
			t=q.top();
			xx=t.nowx+dir[i][0];
			yy=t.nowy+dir[i][1];
			if(xx<1||xx>n||yy<1||yy>n||vis[xx][yy]||t.use&&a[xx][yy]==0)
				continue;
			ss=t.step;
			if(a[xx][yy]==0){
				ss+=2;
				q.push(node(xx,yy,ss,t.color,true));
			}else{
				if(t.color!=a[xx][yy]) ss++;
				q.push(node(xx,yy,ss,a[xx][yy],false));
			}
			if(xx==n&&yy==n){
				cout<<ss<<endl;
				is_break=true;
				break;
			}
//			cout<<xx<<" "<<yy<<" "<<ss<<" "<<t.use<<endl;
			vis[xx][yy]=true;
		}
		q.pop();
		if(is_break) break;
	}
	if(!is_break) cout<<"-1\n";
}
int main(){
	cin>>n>>m;
	for(int i=1;i<=m;i++){
		cin>>w1>>w2>>c;
		a[w1][w2]=c+1;
	}
	if(n>1) BFS(1,1);
	else cout<<"0\n";//就是这个
	return 0;
}

当n=m=1时,起点就是终点,直接输出0。
否则就会WA掉第一个点

2022/10/21 14:36
加载中...