萌新求助,为何第二个样例过不了
查看原帖
萌新求助,为何第二个样例过不了
731239
RTX7070Ti楼主2022/10/5 10:40

代码如下:

#include <iostream>
#include <cstring>
using namespace std;

struct node{
	int x, y, step;
};
bool book[101][101];
struct node que[10001];
int nt[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 x1, y1, x2, y2, tx, ty;
int head = 0, tail = 1;
int bfs(int x, int y){
	que[1].x = x;
	que[1].y = y;
	que[1].step = 0;
	book[x][y] = 1;
	while (head < tail){
		head++;
		for (int i = 0; i < 12; i++){
			tx = que[head].x + nt[i][0];
			ty = que[head].y + nt[i][1];
			
			if (tx < 0 || tx > 50 || ty <= 0 || ty > 50) continue;
			if (book[tx][ty] == 0){
				book[tx][ty] = 1;
				tail++;
				que[tail].x = tx; 
				que[tail].y = ty; 
				que[tail].step = que[head].step + 1;
			}
			if (tx == 1 && ty == 1) {
				return que[tail].step;
			}
		}
	}
}
int main(){
	cin >> x1 >> y1 >> x2 >> y2;
	
	if(x1 == 1 && y1 == 1){
        cout << 0 << endl;
        return 0;
    }
    if(x2 == 1 && y2 == 1){
        cout << 0 << endl;
        return 0;
    }
	cout << bfs(x1, y1) << endl;
	memset(book, false, sizeof(book));
	cout << bfs(x2, y2);
	
	return 0;
}

输入: 12 16 18 10 输出: 8 8 求大佬回答!!!

2022/10/5 10:40
加载中...