首页 > ACM题库 > HDU-杭电 > Hdu 1680 Cheesy Chess[解题报告] C++
2013
12-21

Hdu 1680 Cheesy Chess[解题报告] C++

Cheesy Chess

问题描述 :

Any similarity of this problem to the game Chess is completely coincidental.

Cheesy Chess is a simple two-person game. It is played on an 8 × 8 board. Each player has one piece. The players take turns in moving their respective pieces.

The first player, say White, has a king. In one move, it can move one position in any of the eight directions, horizontally, vertically or diagonally, as long as it stays on the board. The second player, say Black, has a pawn. In one move, it can move exactly one position downwards. In fact, the pieces have to make such moves. They may not stay at their positions.

The White king is said to capture the Black pawn, if it moves onto the position currently occupied by the pawn. The aim of the White king is to do exactly this. The aim of the Black pawn is to reach the bottom line of the board safely. As we will see later, however, there are also other ways for White and Black to win.

The game is complicated by the presence of forbidden fields and dangerous fields. A forbidden field is a position on the board where neither the White king, nor the Black pawn may come. A dangerous field is a position where the Black pawn may come, but where the White king may not move onto.

In addition to the fixed dangerous fields, which are dangerous for the entire game, there are (at most) two other, floating dangerous fields, which depend on the position of the Black pawn. They are adjacent to the pawn’s position: the position to the bottom left and bottom right of the pawn, for as far as these positions exist within the boundaries of the board and are not forbidden. All other positions are called open fields, even if they are occupied by either of the pieces.

For example, we may have the following situation, where forbidden fields, dangerous fields and open fields are denoted by ‘F’, ‘D’ and ‘.’, respectively, the White king is denoted by ‘K’ and the Black pawn is denoted by ‘P’.

This illustration does not reveal whether the positions occupied by the White king and the Black pawn are dangerous or open, and whether the dangerous fields adjacent to the position of the pawn are fixed dangerous fields or not.

Due to a move of the Black pawn, the White king’s position may become dangerous. This is not a problem: in the next move, the White king has to move to another, open field anyway. The White king blocks the Black pawn, if Black is to move, but the position below the pawn is occupied by the White king. In this case, the pawn cannot move.

The game ends, when

the White king captures the Black pawn; in this case, White wins;
the White king is to move, but cannot move to an open field; in this case, Black wins;
the Black pawn is to move, but cannot move to an open field or a dangerous field; if the pawn is at the bottom line of the board, then Black wins, otherwise White wins.
You have to find out which player will win, given that White is the first player to move and given that White plays optimally.

输入:

The first line of the input file contains a single number: the number of test cases to follow. Each test case has the following format:

A description of the board, consisting of 8 lines, corresponding to the 8 lines of the board, from top to bottom. Each line contains a string of 8 characters from {‘F’, ‘D’, ‘.’}. Here, ‘F’ denotes a forbidden field, ‘D’ denotes a fixed dangerous field and ‘.’ (a period) denotes an open field.

Of course, an open field may become dangerous due to the position of the Black pawn.

One line with two integers xK and yK (1 ≤ xK, yK ≤ 8), separated by a single space, specifying the initial position of the White king. Here, xK denotes the column (counted from the left) and yK denotes the row (counted from below).

This initial position is neither a forbidden field, nor a fixed dangerous field.

One line with two integers xP and yP (1 <= xP, yP <= 8), separated by a single space, specifying the initial position of the Black pawn. Here, xP denotes the column (counted from the left) and yP denotes the row (counted from below).

This initial position is not a forbidden field, and is different from the initial position of the White king.

输出:

For every test case in the input file, the output should contain a single line containing the string "White" (if White wins) or "Black" (if Black wins).

样例输入:

2
........
.......D
........
.....F..
..DDD...
..DFDD..
..DDD...
........
7 6
3 7
........
........
........
........
........
........
........
........
3 1
6 3

样例输出:

Black
White

#include<iostream>

using namespace std;

#include<queue>

int xK,yK,xP,yP;

struct Node

{

     int K_x,K_y,P_x,P_y;

     int t;

}N,P;

char map[10][10];

int dir[][2]={-1,0, -1,1, 0,1, 1,1, 1,0, 1,-1, 0,-1, -1,-1};

int mark[8][8][10];

bool bfs()

{

     int i,tx,ty,flag;

     memset(mark,0,sizeof(mark));

     queue<Node>Q;

     N.K_x=xK;

     N.K_y=yK;

     N.P_x=xP;

     N.P_y=yP;

     N.t=0;

     mark[N.K_x][N.K_y][N.t]=1;

     Q.push(N);

     while(!Q.empty())

     {

         N=Q.front();

         Q.pop();

         for(i=0;i<8;i++)

         {

             tx=N.K_x;ty=N.K_y;     

             tx+=dir[i][0];

             ty+=dir[i][1];

             if(tx <0 || tx >8 || ty<0 || ty > 8 || map[tx][ty]!='.' || (tx == N.P_x+1&& ty== N.P_y-1) || (tx==N.P_x+1 && ty== N.P_y+1))

                 continue;

             P.K_x=tx;

             P.K_y=ty;

             P.P_x=N.P_x+1;

             P.P_y=N.P_y;

             P.t=N.t+1;

             if((P.P_x==P.K_x && P.P_y== P.K_y)||(P.K_x==N.P_x && P.K_y== N.P_y) || map[P.P_x][P.P_y]=='F')

             {

                 flag=1;

                 return 1;

             }

             if(P.P_x<=7 && !mark[P.K_x][P.K_y][P.t])

             {

                 Q.push(P);

                 mark[P.K_x][P.K_y][P.t]=1;

             }     

         }

     }

     return 0;

}

int main()

{

     int i,t;

     scanf("%d",&t);

     while(t--)

     {

         for(i=0;i<8;i++)

             scanf("%s",&map[i]);

         scanf("%d %d",&yK,&xK);yK--;xK=8-xK;

         scanf("%d %d",&yP,&xP);yP--;xP=8-xP;     

         if(bfs())

             printf("White\n");

         else printf("Black\n");

     }

}

 


  1. 5.1处,反了;“上一个操作符的优先级比操作符ch的优先级大,或栈是空的就入栈。”如代码所述,应为“上一个操作符的优先级比操作符ch的优先级小,或栈是空的就入栈。”

  2. 算法是程序的灵魂,算法分简单和复杂,如果不搞大数据类,程序员了解一下简单点的算法也是可以的,但是会算法的一定要会编程才行,程序员不一定要会算法,利于自己项目需要的可以简单了解。