P1443 马的遍历

  一开始没仔细审题被对齐卡了 = = 。。

#include <bits/stdc++.h>
#define MP make_pair
#define PB push_back
#define st first
#define nd second
#define rd third
#define rg register
#define FOR(i, a, b) for(int i =(a); i <=(b); ++i)
#define RE(i, n) FOR(i, 1, n)
#define FORD(i, a, b) for(int i = (a); i >= (b); --i)
#define REP(i, n) for(int i = 0;i <(n); ++i)
#define VAR(v, i) __typeof(i) v=(i)
#define FORE(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i)
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
using namespace std;

const int N = 712;
int chess[N][N];
void bfs(int n, int m, int x, int y)
{
    queue<pair<int, int>> q;
    q.push(pair<int, int>(x, y));
    while (!q.empty())
    {
        pair<int, int> node = q.front(); q.pop();
        chess[node.first][node.second] += 1;
        if (node.first + 2 < n && node.second + 1 < m && chess[node.first + 2][node.second + 1] < 0)
        {
            chess[node.first + 2][node.second + 1] = chess[node.first][node.second];
            q.push(pair<int, int>(node.first + 2, node.second + 1));
        }
        if (node.first + 1 < n && node.second + 2 < m && chess[node.first + 1][node.second + 2] < 0)
        {
            chess[node.first + 1][node.second + 2] = chess[node.first][node.second];
            q.push(pair<int, int>(node.first + 1, node.second + 2));
        }
        if (node.first + 2 < n && node.second - 1 >= 0 && chess[node.first + 2][node.second - 1] < 0)
        {
            chess[node.first + 2][node.second - 1] = chess[node.first][node.second];
            q.push(pair<int, int>(node.first + 2, node.second - 1));
        }
        if (node.first + 1 < n && node.second - 2 >= 0 && chess[node.first + 1][node.second - 2] < 0)
        {
            chess[node.first + 1][node.second - 2] = chess[node.first][node.second];
            q.push(pair<int, int>(node.first + 1, node.second - 2));
        }
        if (node.first - 2 >= 0 && node.second + 1 < m && chess[node.first - 2][node.second + 1] < 0)
        {
            chess[node.first - 2][node.second + 1] = chess[node.first][node.second];
            q.push(pair<int, int>(node.first - 2, node.second + 1));
        }
        if (node.first - 1 >= 0 && node.second + 2 < m && chess[node.first - 1][node.second + 2] < 0)
        {
            chess[node.first - 1][node.second + 2] = chess[node.first][node.second];
            q.push(pair<int, int>(node.first - 1, node.second + 2));
        }
        if (node.first - 2 >= 0 && node.second - 1 >= 0 && chess[node.first - 2][node.second - 1] < 0)
        {
            chess[node.first - 2][node.second - 1] = chess[node.first][node.second];
            q.push(pair<int, int>(node.first - 2, node.second - 1));
        }
        if (node.first - 1 >= 0 && node.second - 2 >= 0 && chess[node.first - 1][node.second - 2] < 0)
        {
            chess[node.first - 1][node.second - 2] = chess[node.first][node.second];
            q.push(pair<int, int>(node.first - 1, node.second - 2));
        }
    }
}
int main()
{
    int n, m, x, y;
    cin >> n >> m >> x >> y;
    REP(i, n) REP(j, m) chess[i][j] = -1;
    bfs(n, m, x - 1, y - 1);
    REP(i, n) {
        REP(j, m)
            printf("%-4d ", chess[i][j]);
        cout << endl;
    }
    return 0;
}

  

转载于:https://www.cnblogs.com/darkchii/p/9678508.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值