[搜索]Cheesy Chess

来源:互联网 发布:shell list 添加数据 编辑:程序博客网 时间:2024/05/29 10:08
Cheesy Chess
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 391 Accepted: 148

Description

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.

Input

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 ≤ xKyK ≤ 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 ≤ xPyP ≤ 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.

Output

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).

Sample Input

2...............D.............F....DDD.....DFDD....DDD...........7 63 7................................................................3 16 3

Sample Output

BlackWhite

Source

BAPC 2006 Qualification

看起来好象是博弈论,但是注意到兵是没有决策的,因此只有单方,王的策略。
因此只要存在某一个决策序列,可以让王赢,那么王就必赢。

因此可以用搜索解决,搜索是否存在必胜态,存在一个即胜利。

变量较多,一定不能命名混乱。
过程繁琐的最好模块化。
题目一定要理解清楚,尤其要分析样例,这道题因为理解错坐标系,而WA了很多次。

#include <cstdio>#include <cstring>const int dx[8] = {-1,-1,-1,0,1,1,1,0};const int dy[8] = {-1,0,1,1,1,0,-1,-1};char map[20][20];struct node{    int x;    int y;};node que[500000][2];bool used[10][10][10];int vabs(int x){return x>0?x:-x;}bool attack(node& a,node& b){    if(b.x!=a.x+1)return false;    return vabs(b.y-a.y)==1;}bool ok(int x,int n){return x>=0&&x<n;}bool bfs(node& king,node& pawn){    int l = -1;    int r = 0;    int step = 0;    memset(used,0,sizeof used);    que[r][0] = king;    que[r][1] = pawn;    used[step][king.x][king.y] = true;    node tp,tk;    while (l != r)    {        int size = r-l;        step ++;        while (size --)        {            l ++;            king = que[l][0];            pawn = que[l][1];            if (pawn.x == 8) continue;            tp.x = pawn.x+1;            tp.y = pawn.y;            for (int i=0;i<8;i++)            {                tk.x = king.x + dx[i];                tk.y = king.y + dy[i];                if (!ok(tk.x,8)||!ok(tk.y,8)||map[tk.x][tk.y]=='F'||map[tk.x][tk.y]=='D') continue;                if (tk.x==pawn.x&&tk.y==pawn.y) return true;                if (attack(pawn,tk)) continue;                if (used[step][tk.x][tk.y]) continue;                if (ok(tp.x,8)&&ok(tp.y,8)&&map[tp.x][tp.y] == 'F') return true;                if (tk.x==tp.x&&tk.y==tp.y) return true;                used[step][tk.x][tk.y] = true;                r ++;                que[r][0] = tk;                que[r][1] = tp;            }        }    }    return false;}node GetPoint(){    node p;    scanf("%d%d",&p.y,&p.x);    p.y--;    p.x--;    p.x=8-1-p.x;    return p;}int main(){    freopen("3455.in","r",stdin);    freopen("3455.out","w",stdout);    int T;    scanf("%d",&T);    node pawn,king;    while (T--)    {        for (int i=0;i<8;i++)            scanf("%s",map[i]);        king = GetPoint();        pawn = GetPoint();        if (bfs(king,pawn))            printf("White\n");        else            printf("Black\n");    }    return 0;}


原创粉丝点击