60WA了 找不到哪里错了 求dalao看看!
查看原帖
60WA了 找不到哪里错了 求dalao看看!
744453
wetwertes楼主2022/8/8 15:43
#include<iostream>
#include<queue>
#include<utility>
#include<cstring>
using namespace std;

const int dx[]={1, 1, 2, 2, 2, 2, -1, -1, -2, -2, -2, -2};
const int dy[]={-2, 2, -2, -1, 1, 2, -2, 2, -1, 1, -2, 2};
const int N = 1010;
int a[N][N];
int b[N][N];
int x1, y5, x2, y2;
typedef pair<int,int> pii;
queue <pii> t;

int bfs(int x, int y)
{
	t.push({x,y});
	a[x][y] = 1;
	while(t.size())
	{
		int xx = t.front().first;
		int yy = t.front().second;
		t.pop();
		if(xx==1&&yy==1) return b[1][1];
		for(int i = 0; i < 12; i ++)
		{
			int nx = xx + dx[i];
			int ny = yy + dy[i];
			
			if(nx<1||ny<1||nx>20||ny>20) continue;
			if(a[nx][ny]) continue;
			
			a[nx][ny] = 1;
			t.push({nx,ny});
			b[nx][ny] = b[xx][yy]+1;
		}
	}
}

int main()
{
	cin >> x1 >> y5;
	cin >> x2 >> y2;
	cout << bfs(x1,y5) << endl;
	memset(a, 0, sizeof a);
	memset(b, 0, sizeof b);
	cout << bfs(x2,y2);
	
	return 0;
}
2022/8/8 15:43
加载中...