UVA 1589:Xiangqi (模拟 Grade D)

题目:

象棋,黑棋只有将,红棋有帅车马炮。问是否死将。

思路:

对方将四个方向走一步,看看会不会被吃。

代码:

很难看……WA了很多发,还越界等等。

#include <cstdio>
#include <cstring>
#include <cstdlib>

char graph[13][13];
int go[4][2] = {{1,0},{0,1},{-1,0},{0,-1}};

bool inBlackPalace(int x, int y) {
    return 4 <= y && y <= 6 && 1 <= x && x <= 3;
}

bool inChess(int x, int y) {
    return 1 <= x && x <= 10 && 1 <= y && y <= 9;
}

bool goG(int x, int y) {
    for (int i = x-1; i >= 1; i--) {
        if (graph[i][y]) {
            return graph[i][y] == ‘B‘;
        }
    }
    return false;
}

bool goR(int x, int y) {
    for (int i = x-1; i >= 1; i--) {
        if (graph[i][y]) {
            if (graph[i][y] == ‘B‘) return true;
            break;
        }
    }
    for (int i = x+1; i <= 10; i++) {
        if (graph[i][y]) {
            if (graph[i][y] == ‘B‘) return true;
            break;
        }
    }
    for (int j = y-1; j >= 1; j--) {
        if (graph[x][j]) {
            if (graph[x][j] == ‘B‘) return true;
            break;
        }
    }
    for (int j = y+1; j <= 9; j++) {
        if (graph[x][j]) {
            if (graph[x][j] == ‘B‘) return true;
            break;
        }
    }
    return false;
}

int can_move(int x,int y,int w)
{
    if(w==1) if(x<1||x>3||y<4||y>6) return 0;
    if(w==2) if(x<1||x>10||y<1||y>9) return 0;
    return 1;
}

bool goH(int x, int y) {
    for (int i = 0; i < 4; i++) {
        int nx = x + go[i][0];
        int ny = y + go[i][1];
        if (1 <= nx && nx <= 10 && 1 <= ny && ny <= 9) {
            if (graph[nx][ny] == 0) {
                if (inChess(nx+go[i][0]+go[i][1],ny+go[i][1]+go[i][0]))
                if (graph[nx+go[i][0]+go[i][1]][ny+go[i][1]+go[i][0]] == ‘B‘) return true;

                if (inChess(nx+go[i][0]-go[i][1],ny+go[i][1]-go[i][0]))
                if (graph[nx+go[i][0]-go[i][1]][ny+go[i][1]-go[i][0]] == ‘B‘) return true;
            }
        }
    }
    return false;
}

bool goC(int x, int y) {
    bool first = true;
    for (int i = x-1; i >= 1; i--) {
        if (graph[i][y]) {
            if (first) {
                first = false;
            } else {
                if (graph[i][y] == ‘B‘) return true;
                break;
            }
        }
    }
    first = true;
    for (int i = x+1; i <= 10; i++) {
        if (graph[i][y]) {
            if (first) {
                first = false;
            } else {
                if (graph[i][y] == ‘B‘) return true;
                break;
            }
        }
    }

    first = true;
    for (int j = y-1; j >= 1; j--) {
        if (graph[x][j]) {
            if (first) {
                first = false;
            } else {
                if (graph[x][j] == ‘B‘) return true;
                break;
            }
        }
    }
    first = true;
    for (int j = y+1; j <= 9; j++) {
        if (graph[x][j]) {
            if (first) {
                first = false;
            } else {
                if (graph[x][j] == ‘B‘) return true;
                break;
            }
        }
    }
    return false;
}

bool goo(int x, int y) {
    switch(graph[x][y]) {
        case ‘G‘: return goG(x,y);
        case ‘R‘: return goR(x,y);
        case ‘H‘: return goH(x,y);
        case ‘C‘: return goC(x,y);
    }

    return false;
}

char ch[200];
int main() {
    int n;
    while (scanf("%d", &n) != EOF) {
        int bx, by;
        scanf("%d%d", &bx, &by);
        if (n == 0 && bx == 0 && by == 0) break;

        memset(graph, 0, sizeof(graph));

        for (int i = 0; i < n; i++) {
            int x, y;
            scanf("%s%d%d", ch, &x, &y);
            graph[x][y] = ch[0];
        }

        bool flag = false;
        for (int way = 0; way < 4; way++) {
            int nx = bx + go[way][0];
            int ny = by + go[way][1];
            if (!inBlackPalace(nx, ny)) continue;
            char tmp = graph[nx][ny];
            graph[nx][ny] = ‘B‘;
            bool ok = true;
            for (int i = 1; i <= 10 && ok; i++) {
                for (int j = 1; j <= 9 && ok; j++) {
                    if (graph[i][j]) {
                        if (goo(i,j)) ok = false;
                    }
                }
            }

            graph[nx][ny] = tmp;
            if (ok) {
                flag = true;
                break;
            }
        }

        puts(flag?"NO":"YES");
    }
    return 0;
}
时间: 2024-12-23 09:28:07

UVA 1589:Xiangqi (模拟 Grade D)的相关文章

UVa 1589 Xiangqi(模拟 HDU4121)

题意  给你一个黑方被将军的象棋残局  判断红方是否已经把黑方将死 模拟题  注意细节就行了  看黑方的将是否四个方向都不能走 #include<cstdio> #include<cstring> using namespace std; const int N = 12; char brd[N][N]; int dx[] = { -1, 1, 0, 0}, dy[] = {0, 0, -1, 1}; int hx[] = { -2, -1, -2, -1, 1, 2, 1, 2}

UVA - 1589 Xiangqi (模拟)

Xiangqi Problem Description Xiangqi is one of the most popular two-player board games in China. The game represents a battle between two armies with the goal of capturing the enemy's "general" piece. In this problem, you are given a situation of

UVA 1589 Xiangqi(仔细的模拟)

题意:中国象棋大家都玩过,就是基本规则,只有将,帅,车,马,炮. 解题思路: 思路是把各个棋子能攻击到的位置在judge数组相应的位置上标记出来 首先考虑马蹩马腿的情况,这个比较好考虑,注意不要越界就行了. 车,不能穿过自己方的车,马,炮,帅.但范围可以穿过'将',因为'将'下一步会移动. 炮,不可以用'将'作为炮架,其余都可以,因为'将'下一步会移动. 帅,情况很简单.一条线. 要注意的是,每个棋子的攻击范围,是必须到另一个棋子的位置的 考虑数据 3 1 5 R 1 1 R 2 5 G10 5

UVA 1589 Xiangqi

Z1589 - Xiangqi Time limit: 3.000 seconds 做这题的时候WA了很多次. 解决思路是,枚举黑方将军可以移动的位置,接着判断这些位置是否被红方将军,如果所有位置会被红方吃掉,那么就是checkmate了. 要注意的情况可能就是双炮将军. 1 #include <cstdio> 2 #include <cstring> 3 using namespace std; 4 5 char board[12][12]; 6 int dr[4] = { 1,

UVA 712(二叉树模拟)

L - S-Trees Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Appoint description:  System Crawler  (2014-04-01) Description  S-Trees  A Strange Tree (S-tree) over the variable set  is a binary tree representing a B

UVA 1594:Ducci Sequence (模拟 Grade E)

题意: 对于一个n元组(a0,a1,...),一次变换后变成(|a0-a1|,|a1-a2|,...) 问1000次变换以内是否存在循环. 思路: 模拟,map判重 代码: #include <cstdio> #include <cstring> #include <map> #include <cmath> #include <algorithm> using namespace std; struct Node{ int a[16]; int

uva 177:Paper Folding(模拟 Grade D)

题目链接 题意:一张纸,每次从右往左对折.折好以后打开,让每个折痕都自然的呈90度.输出形状. 思路:模拟折……每次折想象成把一张纸分成了正面在下的一张和反面在上的一张.维护左边和方向,然后输出.细节有点多. 代码: #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> using namespace std; #define N (1<<13)+

UVA 1593: Alignment of Code(模拟 Grade D)

题意: 格式化代码.每个单词对齐,至少隔开一个空格. 思路: 模拟.求出每个单词最大长度,然后按行输出. 代码: #include <cstdio> #include <cstdlib> #include <cstring> char words[1200][190][90]; int maxLen[190]; char tmp[200]; typedef char * pchar; int readStr(pchar &str, char *out) { in

【UVA】1589 Xiangqi(挖坑待填)

题目 题目 ? ? 分析 无力了,noip考完心力憔悴,想随便切道题却码了250line,而且还是错的,知道自己哪里错了,但特殊情况判起来太烦了,唯一选择是重构,我却没有这勇气. 有空再写吧,最近真的快疯了. ? ? 代码 对拍 #include <bits/stdc++.h> int main() { for(int i=1;i<=100;i++){ system("rand.exe > in.txt"); system("1589.exe <