蒟蒻求助 代码求调 BFS代码
  • 板块学术版
  • 楼主homoteam_ikun
  • 当前回复2
  • 已保存回复2
  • 发布时间2023/7/11 23:11
  • 上次更新2023/11/3 10:25:41
查看原帖
蒟蒻求助 代码求调 BFS代码
896912
homoteam_ikun楼主2023/7/11 23:11

rt 代码如下

#include <iostream> 
using namespace std; 
struct node {
int x; int y; int step;
};
int book[101][101];
struct node que[10001];
int next[12][2] ={{-2,1},{-1,2},{1,2},{2,1},{2,-1},{1,-2},{-1,-2},{-2,-1},{-2,2},{2,2},{2,-2},{-2,-2}};
int p1,q1,p2,q2,tx,ty,flag1=0,flag2=0; 
int s1,s2;
int head=1,tail=1; 
void bfs() {
	que[tail].x=1;que[tail].y=1;
	que[tail].step =0;tail++;book[1][1]=1; while(head < tail){
		for(int i = 0;i <12;i++) {
			tx=que[head].x+next[i][0]; ty=que[head].y+next[i][1];
			if(tx<1||tx>100||ty<1||ty>100)
				continue;
			if(book[tx][ty]==0){
				book[tx][ty]=1;
				que[tail].x=tx; que[tail].y=ty;
				que[tail].step=que[head].step+1;tail++;
				if(flag1==0&&tx==p1&&ty==q1){
					flag1=1;
					s1=que[tail-1].step;
					if(flag2==0&&tx==p2&&ty==q2){
						flag2=1;s2=que[tail-1].step;
					}
				}
				if(flag1==1&&flag2==1)
					break;
				head++;
			}
		}
	}
}

妈妈生的.GIF

2023/7/11 23:11
加载中...