怎么处理无解的情况?
查看原帖
怎么处理无解的情况?
1071829
ssssbbbbbb楼主2024/6/26 09:38

论怎么处理无解!! 尝试了多次,还是不行,求调qwq %%%%

用的队列

#include<bits/stdc++.h>
using namespace std;
const int maxn=400+100;
int n,m,y,x,cnt[maxn][maxn];
int d[8][2]={{-1,-2},{-2,-1},{-2,1},{-1,2},{1,2},{2,1},{2,-1},{1,-2}};
struct node{
	int x,y,step;
	node (int xx,int yy,int st){
		x=xx;
		y=yy;
		step=st;
	}
};
queue<node> q;
void bfs(int x,int y){
	cnt[x][y]=1;
	q.push(node(x,y,0));
	while(!q.empty())
	{
		node no=q.front();q.pop();
		for(int i=0;i<8;i++)
		{
			int dx=no.x+d[i][0];
			int dy=no.y+d[i][1];
			//cout<<dx<<" "<<dy<<endl;
			if(dx>=0 && dx<=n && dy>=0 &&  dy<=m && cnt[dx][dy]==0)
			{
				cnt[dx][dy]=no.step+1;
				q.push(node(dx,dy,cnt[dx][dy]));
				//cout<<cnt[dx][dy]<<endl;
			}
		}
	}
}
int main(){
	cin>>n>>m>>x>>y;
	bfs(x,y);
	cnt[x][y]=0;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++) cout<<cnt[i][j]<<" ";
		cout<<endl;
	}
	return 0;
}
2024/6/26 09:38
加载中...