zoukankan      html  css  js  c++  java
  • 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;
    }
    

      

  • 相关阅读:
    文件File
    Vuex 模块化实现待办事项的状态管理
    浅谈jquery插件开发模式*****************************************************************************************
    git 详细的操作指南笔记
    Vue学习之路---No.5(分享心得,欢迎批评指正)
    Java 向Hbase表插入数据报(org.apache.hadoop.hbase.client.HTablePool$PooledHTable cannot be cast to org.apac)
    MongoDB -- 查询
    3 分钟学会调用 Apache Spark MLlib KMeans
    架构设计:负载均衡层设计方案(4)——LVS原理
    Linux内核:关于中断你须要知道的
  • 原文地址:https://www.cnblogs.com/darkchii/p/9678508.html
Copyright © 2011-2022 走看看