zoukankan      html  css  js  c++  java
  • HDU4478 Where is the King 搜索

    题意:给定N*N的网格,从某一点出发,问经过T时间后能够出现的位置有多少个?

    解法:由于从一个点出去后可以原路返回,所以我们只需要记录某个点在T时刻内能否在奇时刻或是偶时刻到达。最后统计一下奇数和偶数时刻到达各点的情况。注意如果8个方向都没方法行走,那么就呆在原地。

    代码如下:

    #include <cstdlib>
    #include <cstring>
    #include <cstdio>
    #include <algorithm>
    #include <iostream>
    #include <queue>
    using namespace std;
    
    int N, T, sx, sy;
    char mp[105][105];
    char vis[105][105][2];
    
    struct Sta {
        int x, y, t;    
    };
    
    queue<Sta>q;
    
    int dir[8][2]={{-1,0},{-1,1},{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1}};
    
    inline bool judge(int x, int y) {
        if (x >= 0 && x < N && y >= 0 && y < N) return true;
        return false;
    }
    
    void cover(const Sta &cur) {
        Sta nxt;
        bool flag = false;
        for (int k = 0; k < 8; ++k) {
            nxt.x = cur.x + dir[k][0];
            nxt.y = cur.y + dir[k][1];
            nxt.t = cur.t + 1;
            if (judge(nxt.x, nxt.y) && mp[nxt.x][nxt.y] == '.') {
                flag = true;
                if (!vis[nxt.x][nxt.y][nxt.t&1]) {
                    vis[nxt.x][nxt.y][nxt.t&1] = 1;
                    if (nxt.t < T) {
                        q.push(nxt);
                    }
                }
            }
        }
        if (!flag) {
            nxt = cur;
            ++nxt.t;
            if (!vis[nxt.x][nxt.y][nxt.t&1]) {
                vis[nxt.x][nxt.y][nxt.t&1] = 1;
                if (nxt.t < T) {
                    q.push(nxt);
                }
            }
        }
    }
    
    void solve() {
        while (!q.empty()) q.pop();
        memset(vis, 0, sizeof (vis));
        Sta cur, nxt;
        nxt.x = sx, nxt.y = sy, nxt.t = 0;
        vis[sx][sy][0] = 1;
        q.push(nxt);
        while (!q.empty()) {
            cur = q.front(), q.pop();
            cover(cur);
        }
        int odd = 0, even = 0;
        for (int i = 0; i < N; ++i) {
            for (int j = 0; j < N; ++j) {
                if (vis[i][j][0]) ++even;
                if (vis[i][j][1]) ++odd;
            }
        }
        if (T & 1) printf("%d\n", odd);
        else printf("%d\n", even);
    }
    
    int main() {
        int C;
        scanf("%d", &C);
        while (C--) {
            scanf("%d %d %d %d", &N, &T, &sx, &sy);
            sx--, sy--;
            for (int i = 0; i < N; ++i) {
                scanf("%s", mp[i]);
            }
            solve();
        }
        return 0;    
    }
  • 相关阅读:
    剑指offer-整数中1出现的次数
    剑指offer-连续子数组的最大和
    剑指offer-最小的k个数
    剑指offer-数组中超过一半的数字
    剑指offer-二叉搜索树与双向链表
    剑指offer-复杂链表的复制
    剑指offer-二叉树中和为某一值的路径
    剑指offer-二叉搜索树的后序遍历
    Alpha 冲刺 (7/10)
    Alpha 冲刺 (6/10)
  • 原文地址:https://www.cnblogs.com/Lyush/p/3126765.html
Copyright © 2011-2022 走看看