求助一道广搜题
  • 板块学术版
  • 楼主Dangerise
  • 当前回复3
  • 已保存回复3
  • 发布时间2022/12/22 08:43
  • 上次更新2023/10/24 06:59:13
查看原帖
求助一道广搜题
371409
Dangerise楼主2022/12/22 08:43

一道简单的广搜题

P1747

我才用的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;
}
2022/12/22 08:43
加载中...