码迷,mamicode.com
首页 > 其他好文 > 详细

UVA 1589 Xiangqi(仔细的模拟)

时间:2015-08-29 11:13:18      阅读:184      评论:0      收藏:0      [点我收藏+]

标签:uva

题意:中国象棋大家都玩过,就是基本规则,只有将,帅,车,马,炮。

解题思路:

思路是把各个棋子能攻击到的位置在judge数组相应的位置上标记出来

首先考虑马蹩马腿的情况,这个比较好考虑,注意不要越界就行了。

车,不能穿过自己方的车,马,炮,帅。但范围可以穿过‘将’,因为‘将’下一步会移动。

炮,不可以用‘将’作为炮架,其余都可以,因为‘将’下一步会移动。

帅,情况很简单。一条线。

要注意的是,每个棋子的攻击范围,是必须到另一个棋子的位置的

考虑数据

3 1 5

R 1 1

R 2 5

G10 5

应该是YES。

注意到这些,剩下的就很简单了。

我没写太多函数,结果导致代码冗长。

题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=52102

Memory: 0 KB   Time: 0 MS
Language: C++ 4.8.2   Result: Accepted
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<cctype>
#include<list>
#include<iostream>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>

using namespace std;

#define MAX_N 20
const int INF = 0x3f3f3f3f;
int sx, sy;
int judge[MAX_N][MAX_N];
char board[MAX_N][MAX_N];

bool can_put(int i, int j)
{
    return board[i][j] != 'R' && board[i][j] != 'H' && board[i][j] != 'C' && board[i][j] != 'G';
}

int main()
{
    int n;
    int xx, yy;
    while(scanf("%d%d%d",&n, &xx, &yy) && (xx + yy+ n))
    {
        memset(board, 0, sizeof(board));
        memset(judge, 0, sizeof(judge));
        board[xx][yy] = 'J';
        char kind[3];
        int x, y;
        for(int i = 0 ; i < n ; i++)
        {
            scanf("%s%d%d", &kind, &x, &y);
            board[x][y] = kind[0];
        }
        for(int i = 1 ; i <= 10 ; i++)
        {
            for(int j = 1 ; j <= 9 ; j++)
            {
                if(board[i][j] == 'H')
                {
                    if(i - 2 >= 1 && board[i-1][j] != 'J' && can_put(i - 1, j))
                    {
                        if(j - 1 >= 1 )
                        {
                            judge[i - 2][j - 1] = 1;
                        }
                        if(j + 1 <= 9 )
                        {
                            judge[i - 2][j + 1] = 1;
                        }
                    }
                    if(i + 2 <= 10 && board[i+1][j] != 'J' && can_put(i + 1, j))
                    {
                        if(j - 1 >= 1 )
                        {
                            judge[i + 2][j - 1] = 1;
                        }
                        if(j + 1 <= 9 )
                        {
                            judge[i + 2][j + 1] = 1;
                        }
                    }
                    if(j - 2 >= 1 && board[i][j-1] != 'J' && can_put(i, j - 1))
                    {
                        if(i - 1 >= 1 )
                        {
                            judge[i - 1][j - 2] = 1;
                        }
                        if(i + 1 <= 5 )
                        {
                            judge[i + 1][j - 2] = 1;
                        }
                    }
                    if(j + 2 <= 10 && board[i][j+1] != 'J' && can_put(i, j + 1))
                    {
                        if(i - 1 >= 1 )
                        {
                            judge[i - 1][j + 2] = 1;
                        }
                        if(i + 1 <= 5)
                        {
                            judge[i + 1][j + 2] = 1;
                        }
                    }
                }
                else if(board[i][j] == 'R')
                {
                    for(int k = i - 1 ; k >= 1 ; k--)
                    {
                        if( can_put(k, j))
                        {
                            judge[k][j] = 1;
                        }
                        else
                        {
                            judge[k][j] = 1;
                            break;
                        }
                    }
                    for(int k = i + 1 ; k <= 10 ; k++)
                    {
                        if(can_put(k, j))
                        {
                            judge[k][j] = 1;
                        }
                        else
                        {
                            judge[k][j] = 1;
                            break;
                        }
                    }
                    for(int k = j + 1 ; k <= 9 ; k++)
                    {
                        if(can_put(i, k))
                        {
                            judge[i][k] = 1;
                        }
                        else
                        {
                            judge[i][k] = 1;
                            break;
                        }
                    }
                    for(int k = j - 1 ; k >= 1 ; k--)
                    {
                        if(can_put(i, k))
                        {
                            judge[i][k] = 1;
                        }
                        else
                        {
                            judge[i][k] = 1;
                            break;
                        }
                    }
                }
                else if(board[i][j] == 'C')
                {
                    int flag1 = 0;
                    for(int k = j - 1 ; k >= 1 ; k--)
                    {
                        if(board[i][k] == 'R' || board[i][k] == 'H' || board[i][k] == 'C' || board[i][k] == 'G')
                        {
                            for(int l = k - 1; l >= 1 ; l--)
                            {
                                if(can_put(i, l))
                                {
                                    judge[i][l] = 1;
                                }
                                else
                                {
                                    judge[i][l] = 1;
                                    flag1 = 1;
                                    break;
                                }
                            }
                            if(flag1)
                            {
                                break;
                            }
                        }
                    }
                    int flag2 = 0;
                    for(int k = j + 1; k <= 9 ; k++)
                    {
                        if(board[i][k] == 'R' || board[i][k] == 'H' || board[i][k] == 'C' || board[i][k] == 'G')
                        {
                            for(int l = k + 1 ; l <= 1 ; l++)
                            {
                                if(can_put(i, l))
                                {
                                    judge[i][l] = 1;
                                }
                                else
                                {
                                    judge[i][l] = 1;
                                    flag2 = 1;
                                    break;
                                }
                            }
                            if(flag2)
                            {
                                break;
                            }
                        }
                    }
                    int flag3 = 0;
                    for(int k = i - 1; k >= 1 ; k--)
                    {
                        if(board[k][j] == 'R' || board[k][j] == 'H' || board[k][j] == 'C' || board[k][j] == 'G')
                        {
                            for(int l = k - 1 ; l >= 1 ; l--)
                            {
                                if(can_put(l, j))
                                {
                                    judge[l][j] = 1;
                                }
                                else
                                {
                                    judge[l][j] = 1;
                                    flag3 = 1;
                                    break;
                                }
                            }
                            if(flag3)
                            {
                                break;
                            }
                        }
                    }
                    int flag4 = 0;
                    for(int k = i + 1; k <= 10 ; k++)
                    {
                        if(board[k][j] == 'R' || board[k][j] == 'H' || board[k][j] == 'C' || board[k][j] == 'G')
                        {
                            for(int l = k + 1; l <= 10 ; l++)
                            {
                                if(can_put(l, j))
                                {
                                    judge[l][j] = 1;
                                }
                                else
                                {
                                    judge[l][j] = 1;
                                    flag4 = 1;
                                    break;
                                }
                            }
                            if(flag4)
                            {
                                break;
                            }
                        }
                    }
                }
                else if(board[i][j] == 'G')
                {
                    for(int k = i - 1 ; k >= 1 ; k--)
                    {
                        if(can_put(k, j))
                        {
                            judge[k][j] = 1;
                        }
                        else
                        {
                            judge[k][j] = 1;
                            break;
                        }
                    }
                }
                else if(board[i][j] == 'J')
                {
                    sx = i;
                    sy = j;
                }
            }
        }
        int ok = 0;
        if(sx + 1 <= 3 && !judge[sx + 1][sy])
        {
            ok = 1;
        }
        else if(sx - 1 >= 1 && !judge[sx - 1][sy])
        {
            ok = 1;
        }
        else if(sy + 1 <= 6 && !judge[sx][sy + 1])
        {
            ok = 1;
        }
        else if(sy - 1 >= 4 && !judge[sx][sy - 1])
        {
            ok = 1;
        }
        if(!ok) printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}




版权声明:本文为博主原创文章,未经博主允许不得转载。

UVA 1589 Xiangqi(仔细的模拟)

标签:uva

原文地址:http://blog.csdn.net/qwe585p/article/details/48084981

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!