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};  //马将军
int hy[] = { -1, -2, 1, 2, -2, -1, 2, 1};
int tx[] = { -1, -1, -1, -1, 1, 1, 1, 1};  //马蹩脚
int ty[] = { -1, -1, 1, 1, -1, -1, 1, 1};
int cr[2], cc[2];  //记录炮的坐标

int check(int r, int c)
{
    int i, j, k, tr, tc, cnt;
    if(r < 1 || r > 3 || c < 4 || c > 6) return 1;

    for (j = c - 1; j > 0; --j)   //行被车
    {
        if(brd[r][j])
            if(brd[r][j] == 'R') return 1;
            else break;
    }
    for (j = c + 1; j <= 9; ++j)
    {
        if(brd[r][j])
            if(brd[r][j] == 'R') return 1;
            else break;
    }

    for (i = r - 1; i > 0; --i)  //列被车
    {
        if(brd[i][c])
            if(brd[i][c] == 'R') return 1;
            else break;
    }
    for (i = r + 1; i <= 10; ++i) //列被车或将
    {
        if(brd[i][c])
            if(brd[i][c] == 'R' || brd[i][c] == 'G') return 1;
            else break;
    }

    for(int k = 0; k < 2; ++k)    //被炮将军
    {
        if(cr[k] == r)   //行有炮
        {
            for(j = c - 1, cnt = 0; j > cc[k]; --j) if(brd[r][j]) ++cnt;
            if(cnt == 1) return 1;
            for(j = c + 1, cnt = 0; j < cc[k]; ++j) if(brd[r][j]) ++cnt;
            if(cnt == 1) return 1;
        }
        if(cc[k] == c)   //列有炮
        {
            for(i = r - 1, cnt = 0; i > cr[k]; --i) if(brd[i][c]) ++cnt;
            if(cnt == 1) return 1;
            for(i = r + 1, cnt = 0; i < cr[k]; ++i) if(brd[i][c]) ++cnt;
            if(cnt == 1) return 1;
        }
    }

    for(int k = 0; k < 8; ++k)   //被马将军
    {
        tr = r + hx[k], tc = c + hy[k];
        if(tr < 1 || tr > 10 || tc < 1 || tc > 9) continue;
        if(brd[tr][tc] == 'H' && (!brd[r + tx[k]][c + ty[k]]))
            return 1;
    }

    return 0;
}

int main()
{
    char s[5];
    int n, r, c, x, y;
    while(scanf("%d%d%d", &n, &r, &c), n || r || c)
    {
        memset(brd, 0, sizeof(brd));
        cr[0] = cc[0] = cr[1] = cc[1] = 0;

        while(n--)
        {
            scanf("%s%d%d", s, &x, &y);
            if(s[0] == 'C')
            {
                if(cr[0]) cr[1] = x, cc[1] = y;
                else cr[0] = x, cc[0] = y;
            }
            brd[x][y] = s[0];
        }

        int cnt = 0;
        for(int i = 0; i < 4; ++i)
            cnt += check(r + dx[i], c + dy[i]);
        if(cnt < 4) puts("NO");
        else puts("YES");
    }
    return 0;
}

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
later stage in the game. Besides, the red side has already “delivered a check”. Your work is to check whether the situation is “checkmate”.

Now we introduce some basic rules of Xiangqi. Xiangqi is played on a 10×9 board and the pieces are placed on the intersections (points). The top left point is (1,1) and the bottom right point is (10,9). There are two groups of pieces marked by black or red
Chinese characters, belonging to the two players separately. During the game, each player in turn moves one piece from the point it occupies to another point. No two pieces can occupy the same point at the same time. A piece can be moved onto a point occupied
by an enemy piece, in which case the enemy piece is "captured" and removed from the board. When the general is in danger of being captured by the enemy player on the enemy player’s next move, the enemy player is said to have "delivered a check".
If the general‘s player can make no move to prevent the general‘s capture by next enemy move, the situation is called “checkmate”.

We only use 4 kinds of pieces introducing as follows:

General: the generals can move and capture one point either vertically or horizontally and cannot leave the “palace” unless the situation called
flying general” (see the figure above). “Flying general” means that one general can “fly” across the board to capture the enemy general if they stand on the same line without intervening pieces.

Chariot: the chariots can move and capture vertically and horizontally by any distance, but may not jump over intervening pieces

Cannon: the cannons move like the chariots, horizontally and vertically, but capture by jumping exactly one piece (whether it is friendly or enemy) over to its
target.

Horse: the horses have 8 kinds of jumps to move and capture shown in the left figure. However, if there is any pieces lying on a point away from the horse horizontally or vertically
it cannot move or capture in that direction (see the figure below), which is called “hobbling the horse’s leg”.

Now you are given a situation only containing a black general, a red general and several red chariots, cannons and horses, and the red side has delivered a check. Now it turns to black side’s move. Your job is to determine that whether this situation is “checkmate”.

Input

The input contains no more than 40 test cases. For each test case, the first line contains three integers representing the number of red pieces N (2<=N<=7) and the position of the black general. The following n lines
contain details of N red pieces. For each line, there are a char and two integers representing the type and position of the piece (type char ‘G’ for general, ‘R’ for chariot, ‘H’ for horse and ‘C’ for cannon). We guarantee that the situation is legal and the
red side has delivered the check.

There is a blank line between two test cases. The input ends by 0 0 0.

Output

For each test case, if the situation is checkmate, output a single word ‘YES’, otherwise output the word ‘NO’.

Sample Input

2 1 4
G 10 5
R 6 4

3 1 5
H 4 5
G 10 5
C 7 5

0 0 0

Sample Output

YES
NO
时间: 2024-07-28 18:09:21

UVa 1589 Xiangqi(模拟 HDU4121)的相关文章

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 (模拟 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

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

hdu4121 poj4001 Xiangqi(模拟)

模拟题考验coding能力,一定要思路清晰,按照模块化思想,有哪些情况,需要哪些功能都要事先分析好了.高手的模拟题代码往往结构很清晰,功能模块写成函数,没有过多重复代码,让人一看便明. 方法选择的好坏会影响编程复杂度,这题老将最多只能往四个位置走,就枚举这四个位置,每个位置再枚举每个红子看是不是有子能吃了它.枚举时注意有可能老将这一步吃了一个红子. 有一种情况是一开始双方老将就见面,这其实不叫红棋在将军,实际中红棋不能这么将.但是毕竟是一道编程题,出题人可能也不是太懂棋...所以要考虑这种情况.

【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 <

Xiangqi UVA - 1589

https://vjudge.net/problem/UVA-1589 刘汝佳的第四章习题,思路没有难点,是用来练习函数化和自定而下的编程方法的. 首先分析输入输出,思考用什么容器存储数据,处理问题时会用到什么,然后写出大体框架. 可以简单的先写成接收输入,处理问题,按标准要求输出,然后把输入输出部分完善(因为本题这部分比较简单) 然后写处理部分,要判断当前情况下将死等于判断下一步所有能走的位置是不是都为死棋.(有一种特殊情况,直接可以飞将吃对方的帅来取胜?!!!) 再细化问题,把判断该位置是不

HDU 4121 Xiangqi 模拟

原题: http://acm.hdu.edu.cn/showproblem.php? pid=4121 题目: Xiangqi Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4809 Accepted Submission(s): 1134 Problem Description Xiangqi is one of the most pop