80分求助
查看原帖
80分求助
467443
A宋锦瑞A楼主2022/5/10 21:43
#include<bits/stdc++.h>

using namespace std;

struct pos {
	int x,y;
	int step;
} n1;

bool v[201][201];
int fx[12][2]= {{-1,-2},{-2,-2},{-2,-1},{-1,2},{-2,2},{-2,1},{1,2},{2,2},{2,1},{1,-2},{2,-2},{2,-1}};
queue<pos>q;

int bfs(pos x) {
	pos a,b;
	q.push(x);
	while(q.size()!=0) {
		a=q.front();
		q.pop();
		if(a.x==1&&a.y==1) {
			return a.step;
		}
		for(int i=0; i<12; i++) {
			b.x=a.x+fx[i][0];
			b.y=a.y+fx[i][1];
			if(b.x<0||b.y<0||v[b.x][b.y]==1)  {
				continue;
			}
			b.step=a.step+1;
			v[b.x][b.y]=1;
			q.push(b);
		}
	}
	return -1;
}

int main() {
	for(int i=0; i<2; i++) {
		cin>>n1.x>>n1.y;
		cout<<bfs(n1)<<endl;
		memset(v,0,sizeof(v));
		while(q.size()) {
			q.pop();
		}
	}

	return 0;
}

9,10测试点WA

2022/5/10 21:43
加载中...