我才用的STL库进行广搜,代码也比较标准就(应该),但不知道是不是我没有注意什么细节,导致我只有80分
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#define SIZE 30
using namespace std;
int dx[12] = {1, 1, 2, 2, 2, 2, -1, -1, -2, -2, -2, -2};
int dy[12] = {-2, 2, -2, -1, 1, 2, -2, 2, -1, 1, -2, 2};
bool vis[SIZE][SIZE];
int ans[SIZE][SIZE];
void bfs(int x, int y)
{
queue<pair<int, int>> que;
que.push(make_pair(x, y));
int nx, ny;
while (!que.empty())
{
x = que.front().first;
y = que.front().second;
que.pop();
for (int i = 0; i < 12; i++)
{
nx = x + dx[i];
ny = y + dy[i];
if (nx >= 0 && nx < SIZE && ny >= 0 && ny < SIZE)
{
if (!vis[nx][ny])
{
vis[nx][ny] = 1;
ans[nx][ny] = ans[x][y] + 1;
que.push(make_pair(nx, ny));
if (nx == 1 && ny == 1)
{
return;
}
}
}
}
}
}
int main()
{
int t = 2;
while (t--)
{
memset(ans, 127, sizeof(ans));
memset(vis, 0, sizeof(vis));
int x, y;
cin >> x >> y;
vis[x][y] = 1;
ans[x][y] = 0;
bfs(x, y);
cout << ans[1][1] << endl;
}
return 0;
}