HDU 4121 - Xiangqi (模拟)

题意

判断黑方是不是完蛋了。

思路

处理出下一步黑方被搞死的地方,然后判断是不是将走不了了。

一开始车和炮直面将的地方处理得不好,WA了几次。

马的部分没有写错还是挺满意的。

代码

#include <stack>
#include <cstdio>
#include <list>
#include <cassert>
#include <set>
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <functional>
#include <cstring>
#include <algorithm>
#include <cctype>
#include <string>
#include <map>
#include <cmath>
using namespace std;
#define LL long long
#define ULL unsigned long long
#define SZ(x) (int)x.size()
#define Lowbit(x) ((x) & (-x))
#define MP(a, b) make_pair(a, b)
#define MS(arr, num) memset(arr, num, sizeof(arr))
#define PB push_back
#define X first
#define Y second
#define ROP freopen("input.txt", "r", stdin);
#define MID(a, b) (a + ((b - a) >> 1))
#define LC rt << 1, l, mid
#define RC rt << 1|1, mid + 1, r
#define LRT rt << 1
#define RRT rt << 1|1
const double PI = acos(-1.0);
const int INF = 0x3f3f3f3f;
const double eps = 1e-8;
const int MAXN = 30 + 10;
const int MOD = 9901;
const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
int cases = 0;
typedef pair<int, int> pii;
 
char mp[15][15];
int vis[15][15], HorseMove[][2] = { {-2, -1}, {-2, 1}, {2, 1}, {2, -1}, {1, -2}, {-1, -2}, {-1, 2}, {1, 2} };
int xxx, yyy;
 
void HandleGeneral(int x, int y)
{
    for (int i = x-1; i >= 1; i--)
    {
        vis[i][y] = 1;
        if (mp[i][y]) return;
    }
}
 
void HandleChariot(int x, int y)
{
    for (int i = 0; i < 4; i++)
    {
        bool obstacle = false;
        int xx = x, yy = y;
        while (xx >= 1 && xx <= 10 && yy >= 1 && yy <= 9)
        {
            xx = xx + dir[i][0], yy = yy + dir[i][1];
            vis[xx][yy] = 1;
            if (mp[xx][yy])
            {
                if (mp[xx][yy] != 'S') break;
            }
        }
    }
}
 
void HandleHorse(int x, int y)
{
    int use[5] = {0};
    for (int i = 0; i < 4; i++)
    {
        int xx = x + dir[i][0], yy = y + dir[i][1];
        if (mp[xx][yy]) use[i] = 1;
    }
    for (int i = 0; i < 4; i++) if (!use[i])
    {
        vis[x+HorseMove[i*2][0]][y+HorseMove[i*2][1]] = 1;
        vis[x+HorseMove[i*2+1][0]][y+HorseMove[i*2+1][1]] = 1;
    }
}
 
void HandleCannon(int x, int y)
{
    for (int i = 0; i < 4; i++)
    {
        bool obstacle = false;
        int xx = x, yy = y;
        while (true)
        {
            xx = xx + dir[i][0], yy = yy + dir[i][1];
            if (xx < 1 || xx > 10 || yy < 1 || yy > 9) break;
            if (mp[xx][yy])
            {
                if (obstacle)
                {
                    vis[xx][yy] = 1;
                    if (mp[xx][yy] != 'S') break;
                }
                else
                {
                    if (mp[xx][yy] == 'S') break;
                    obstacle = 1;
                }
            }
            else
            {
                if (obstacle) vis[xx][yy] = 1;
            }
        }
    }
}
 
bool FinalCheck()
{
    for (int i = 0; i < 4; i++)
    {
        int xx = xxx + dir[i][0], yy = yyy + dir[i][1];
        if (xx > 3 || xx < 1 || yy > 6 || yy < 4) continue;
        if (!vis[xx][yy]) return false;
    }
    return true;
}
 
int main()
{
    //ROP;
    int n;
    while (scanf("%d%d%d", &n, &xxx, &yyy), n+xxx+yyy)
    {
        MS(mp, 0); MS(vis, 0);
        mp[xxx][yyy] = 'S';
        char name[10];
        int xx, yy;
        for (int i = 0; i < n; i++)
        {
            scanf("%s%d%d", name, &xx, &yy);
            mp[xx][yy] = name[0];
        }
        for (int i = 1; i <= 10; i++)
        {
            for (int j = 1; j <= 9; j++)
            {
                if (mp[i][j] && mp[i][j] != 'S')
                {
                    if (mp[i][j] == 'G') HandleGeneral(i, j);
                    else if (mp[i][j] == 'C') HandleCannon(i, j);
                    else if (mp[i][j] == 'H') HandleHorse(i, j);
                    else HandleChariot(i, j);
                }
            }
        }
        printf("%s\n", FinalCheck() ? "YES" : "NO");
    }
    return 0;
}

Powered by Jekyll and Theme by solid