本人踩得坑,荒废了半个多钟头
  • 板块P1443 马的遍历
  • 楼主JK01WYX
  • 当前回复0
  • 已保存回复0
  • 发布时间2023/11/13 22:15
  • 上次更新2023/11/14 10:45:01
查看原帖
本人踩得坑,荒废了半个多钟头
798434
JK01WYX楼主2023/11/13 22:15

1.打印部分。 我cin那里解绑了,printf和cout混用会导致5个样例不过QAQ(我* 了)

2.bfs不熟。 进入下一个队列后,标记要立马改,不然别的也会再跑进一次。。(我一开始图方便是在下一次才改的)

#define _CRT_SECURE_NO_WARNINGS 1
//#include <bits/stdc++.h>
#include <stdio.h>
#include <iostream>
using namespace std;
#include <unordered_map>
#include <vector>
#include <math.h> 
#include <list> 
#include <map> 
#include <stack>
#include <queue>
#include <set>
#include <unordered_set>
#include <string>
#include <algorithm>//sort
#include <stdlib.h>
#include <iomanip>
#include <cstring>//memset
#include <limits.h>
/*ios::sync_with_stdio(false);
cin.tie(0);*/

//long long a, b, p;//a^b mod p=s
//long long fp(int c)
//{
//    if (c == 1)return a % p;
//    long long tmp = fp(c / 2);
//    if (c % 2 == 0)
//        return tmp * tmp % p;
//    else
//        return tmp * tmp % p * a % p;
//}

//int fx[] = { 1,2,1,2,-1,-2,-1,-2 };
//int fy[] = { 2,1,-2,-1,2,1,-2,-1 };
int fx[] = { -1,-2,-2,-1,1,2,2,1 };
int fy[] = { 2,1,-1,-2,2,1,-1,-2 };
int arr[409][409] = { 0 };
int n, m, x, y;
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> n >> m >> x >> y;
    memset(arr, -1, sizeof(arr));
    queue<pair<int, int>>q1;
    queue<pair<int, int>>q2;
    q1.push({ x,y });
    arr[x][y] = 0;
    int depth = 1;
    while (1)
    {
        int bx = q1.front().first, by = q1.front().second;
        q1.pop();
        for (int i = 0; i < 8; i++)
        {
            int aimx = bx + fx[i], aimy = by + fy[i];
            if (aimx >= 1 && aimx <= n
                && aimy >= 1 && aimy <= m
                && arr[aimx][aimy] == -1)
            {
                arr[aimx][aimy] = arr[bx][by]+1;
                q2.push({ aimx,aimy });
            }
        }
        if (q1.empty())
        {
            if (q2.empty())break;
            depth++;
            swap(q1, q2);
        }
    }
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
            printf("%-5d", arr[i][j]);
        //cout << endl;这里
        printf("\n");
    }
    return 0;
}
2023/11/13 22:15
加载中...