这代码有什么问题?
查看原帖
这代码有什么问题?
1179906
hhy8399楼主2024/12/18 19:59
#include <bits/stdc++.h>
using namespace std;

const int MAXN = 1e3 + 10;

struct node {
	int x,y;
};

int x,y,step;
int dx[] = {2,-2,2,-2,2,-2,2,-2,1,1,-1,-1},dy[] = {1,-1,-1,1,2,2,-2,-2,1,-1,1,-1};
bool pos[MAXN][MAXN];

void bfs() {
	
	queue<node> que;
	cin >> x >> y;
	node k;
	k.x = x,k.y = y;
	que.push(k);
	while(que.size()) {
		k = que.front();
		que.pop();
		for(int i = 0;i < 12;i++) {
			int fx = k.x + dx[i],fy = k.y + dy[i];
			if(fx == 1 && fy == 1) {
				cout << ++step << endl;
				return ;
			}
			if(fx > 0 && fy > 0 && fx <= 20 && fy <= 20 && pos[fx][fy] == 0) {
		//		cout << fx << " " << fy << endl;
				pos[fx][fy] = 1;
				node v;
				v.x = fx,v.y = fy;
				que.push(v);
			}
		}
	//	cout << 2 << endl;
		step ++;
	//	system("pause");
	}
}

int main() {
	for(int i = 1;i <= 2;i++) {
		step = 0;
		memset(pos,0,sizeof pos);
		bfs();
	}
	return 0;
}
2024/12/18 19:59
加载中...