NOJ [1002] 将军问题

来源:互联网 发布:oracle数据库安全机制 编辑:程序博客网 时间:2024/04/30 05:26
  • 问题描述
  • 关于中国象棋,想必大家都很熟悉吧。我们知道,在走棋的时候,被对方將军的这种情形是很容易被人察觉的(不然,你也太粗心了)。但是我们的计算机是如何识别这种情形的呢?它显然没有人的这种“直觉”。这就是我们今天要解决的问题,你的任务就是写一段计算机代码,根据当前局面信息,判断是否存在一方正在被另一方將军的情形,并给出正确结果。

    ./1.png

    图片一

    如图一,象棋棋盘由九条竖线和十条横线交叉组成。棋盘上共有九十个交叉点,象棋子就摆放在和活动在这些交叉点上。棋盘中间没有画通直线的地方,叫做“九宫”。棋子共有三十二个,分为红、黑两组,每组共十六个,各分七种,其名称和数目如下:

    • 红棋子: 帅一个,车、马、炮、相、仕各两个,兵五个。

    • 黑棋子: 将一个,车、马、炮、象、士各两个,卒五个。

    各种棋子的走法如下:

    • 将(帅)每一步只许前进、后退、横走,但不能走出“九宫”。

    • 士(仕)每一步只许沿“九宫”斜线走一格,可进可退。

    • 象(相)不能越过“河界”,每一步斜走两格,可进可退,即俗称“象(相)走田字“。当田字中心有别的棋子时,俗称”塞象(相)眼“,则不许走过去。

    • 马每步一直(或一横)一斜,可进可退,即俗称”马走日字“。如果在要去的方向有别的棋子挡住,俗称”蹩马腿”,则不许走过去。具体可参考图二。

    ./2.png

    图片二

    • 车每一步可以直进、直退、横走,不限步数。

    • 炮在不吃子的时候,走法跟车一样。在吃子时必须隔一个棋子(无论是哪一方的)跳吃,即俗称“炮打隔子”。

    • 卒(兵)在没有过“河界”前,没步只许向前直走一格;过“河界”后,每步可向前直走或横走一格,但不能后退。

    另外,在一个局面中,如果一方棋子能够走到的位置有对方将(帅)的存在,那么该局面就称为將军局面,我们的任务就是找出这样的局面。根据上述规则,我们很容易就能推断出只有以下几种方式才会造成將军局面:

    1. 将(帅)照面。即将和帅在同一直线上。

    2. 马对将(帅)的攻击。(注意马有蹩脚)

    3. 车对将(帅)的攻击。

    4. 炮对将(帅)的攻击。(注意炮要隔一子)

    5. 过河兵对将(帅)的攻击。


  • 输入
  • 输入的第一行为一个正整数n(1<=n<=100)。表示有n个测试局面。
    接下来的n次测试,每次输入10行,每行输入9个特定正整数,用来表示一个局面(上黑下红)。其中数字0表示该处无棋子,其他数字具体表示如下:
    黑方:将(1)、士(2,3)、象(4,5)、马(6,7)、车(8,9)、炮(10,11)、卒(12,13,14,15,16)
    红方:帅(17)、仕(18,19)、相(20,21)、马(22,23)、车(24,25)、炮(26,27)、兵(28,29,30,31,32)
    提示:样例中的第一组数据表示的是初始局面,第二组数据表示的是图一的局面。
  • 输出
  • 如果存在将军局面,则输出"yes"。反之,输出"no"。
  • 样例输入
  • 28 6 4 2 1 3 5 7 90 0 0 0 0 0 0 0 00 10 0 0 0 0 0 11 012 0 13 0 14 0 15 0 160 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 028 0 29 0 30 0 31 0 320 26 0 0 0 0 0 27 00 0 0 0 0 0 0 0 0 24 22 20 18 17 19 21 23 258 6 4 2 1 3 5 0 90 0 0 0 0 0 0 0 00 10 0 0 0 0 7 11 012 0 13 0 14 0 15 0 160 0 0 0 0 0 0 0 0 0 0 0 0 27 0 0 0 028 0 29 0 30 0 31 0 320 26 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 24 22 20 18 17 19 21 23 25
  • 样例输出
  • noyes
  • 提示
  • 本题是一道比较复杂的模拟题,但总的来说也就只有那么几种情况
  • 将帅照面,过河兵的威胁,马的威胁,车的威胁以及炮的威胁,注意有蹩马脚
我的AC代码(仅供参考,希望大家不要直接贴)

    #include<stdio.h>
    #include<string.h>

    int chess[11][10];
    bool soldier(int x,int y) //过河士兵对将军的威胁 ,x,y是将军的坐标
    {
    if(chess[x][y]==1) //对于黑将
    {
    if(x+1<=10 && chess[x+1][y]>=28 && chess[x+1][y]<=32)//下面的
    return true;
    if(y-1>=1 && chess[x][y-1]>=28 && chess[x][y-1]<=32)//左面
    return true;
    if(y+1<=9 && chess[x][y+1]>=28 && chess[x][y+1]<=32)//右面
    return true;
    }
    else if(chess[x][y]==17)//对于红将
    {
    if(x-1>=1 && chess[x-1][y]>=12 && chess[x-1][y]<=16)
    return true;
    if(y-1>=1 && chess[x][y-1]>=12 && chess[x][y-1]<=16)
    return true;
    if(y+1<=9 && chess[x][y+1]>=12 && chess[x][y+1]<=16)
    return true;
    }
    return false;
    }

    bool cannon(int x,int y,int cx,int cy)//炮对将军的威胁
    {
    int other=0;//记录炮和将军的线路上旗子的个数
    if(cx==x)//炮和将军在同一个水平线上
    {
    if(cy>y) //炮在将军的右边列
    {
    for(int i=y+1;i<cy;i++)
    if(chess[x][i])
    other++;
    }
    else if(cy<y)//左边列
    {
    for(int i=cy+1;i<y;i++)
    if(chess[x][i])
    other++;
    }
    if(other==1)//路上正好一个旗子
    return true;
    else
    return false;
    }
    else if(cy==y)//炮和将军在同一垂线上
    {
    if(cx>x)//炮在将军的下方
    {
    for(int i=x+1;i<cx;i++)
    if(chess[i][y])
    other++;
    }
    else if(cx<x)
    {
    for(int i=cx+1;i<x;i++)
    if(chess[i][y])
    other++;
    }
    if(other==1)//路上正好一个旗子
    return true;
    else
    return false;
    }
    return false;
    }

    bool car(int x,int y,int cx,int cy)//车对将军的威胁
    {
    int other=0;
    if(cx==x)//车和将军在同一个水平线上
    {
    if(cy>y) //车在将军的右边
    {
    for(int i=y+1;i<cy;i++)
    if(chess[x][i])
    other++;
    }
    else if(cy<y) //左边
    {
    for(int i=cy+1;i<y;i++)
    if(chess[x][i])
    other++;
    }
    if(other==0)//路上无旗子
    return true;
    else
    return false;
    }
    else if(cy==y)//车和将军在同一垂线上
    {
    if(cx>x)//车在将军的下方
    {
    for(int i=x+1;i<cx;i++)
    if(chess[i][y])
    other++;
    }
    else if(cx<x)
    {
    for(int i=cx+1;i<x;i++)
    if(chess[i][y])
    other++;
    }
    if(other==0)//路上无旗子
    return true;
    else
    return false;
    }
    return false;
    }

    bool meet(int x1,int y1,int x2,int y2)//x1是将军,x2是元帅
    {
    int other=0;
    if(y1==y2)//处于同一垂线上才可能发生将军照面
    {
    for(int i=x1+1;i<x2;i++)
    if(chess[i][y1]!=0)
    return false;
    return true;
    }
    return false;
    }

    bool horse(const int x,const int y,int hx,int hy)
    {
    if(hx==x-2 && hy==y+1 && chess[x-1][y+1]==0)//马在(x-2,y+1)上,且没有蹩马脚
    return true;
    else if(hx==x-1 && hy==y+2 && chess[x-1][y+1]==0)//同上
    return true;
    else if(hx==x+1 && hy==y+2 && chess[x+1][y+1]==0)
    return true;
    else if(hx==x+2 && hy==y+1 && chess[x+1][y+1]==0)
    return true;
    else if(hx==x+2 && hy==y-1 && chess[x+1][y-1]==0)
    return true;
    else if(hx==x+1 && hy==y-2 && chess[x+1][y-1]==0)
    return true;
    else if(hx==x-1 && hy==y-2 && chess[x-1][y-1]==0)
    return true;
    else if(hx==x-2 && hy==y-1 && chess[x-1][y-1]==0)
    return true;
    else
    return false;
    }

    int main()
    {
    int n,i,j;
    int red_bossx=0,red_bossy=0,black_bossx=0,black_bossy=0;
    scanf("%d",&n);
    while(n--)
    {
    red_bossx=0;
    red_bossy=0;
    black_bossx=0;
    black_bossy=0;
    for(i=1;i<=10;i++)
    for(j=1;j<=9;j++)
    {
    scanf("%d",&chess[i][j]);
    if(chess[i][j]==1)
    {
    black_bossx=i;
    black_bossy=j;
    }
    else if(chess[i][j]==17)
    {
    red_bossx=i;
    red_bossy=j;
    }
    }
    bool p;
    for(i=1;i<=10;i++)
    {
    p=false;
    for(j=1;j<=9;j++)
    {
    if(chess[i][j]==6 || chess[i][j]==7) //黑马
    {
    p=horse(red_bossx,red_bossy,i,j);
    if(p==true)
    break;
    }
    else if(chess[i][j]==8 || chess[i][j]==9)//黑车
    {
    p=car(red_bossx,red_bossy,i,j);
    if(p==true)
    break;
    }
    else if(chess[i][j]==10 || chess[i][j]==11)//黑炮
    {
    p=cannon(red_bossx,red_bossy,i,j);
    if(p==true)
    break;
    }
    else if(chess[i][j]==22 || chess[i][j]==23)//红马
    {
    p=horse(black_bossx,black_bossy,i,j);
    if(p==true)
    break;
    }
    else if(chess[i][j]==24 || chess[i][j]==25)//红车
    {
    p=car(black_bossx,black_bossy,i,j);
    if(p==true)
    break;
    }
    else if(chess[i][j]==26 || chess[i][j]==27)//红炮
    {
    p=cannon(black_bossx,black_bossy,i,j);
    if(p==true)
    break;
    }
    }
    if(p)
    break;
    }
    if(p==true)
    printf("yes\n");
    else
    {
    bool q=(meet(black_bossx,black_bossy,red_bossx,red_bossy) || soldier(black_bossx,black_bossy) || soldier(red_bossx,red_bossy));
    if(q)
    printf("yes\n");
    else
    printf("no\n");
    }
    }
    return 0;
    }


0 0