当前位置: 代码迷 >> 综合 >> hdu 1680 Cheesy Chess(BFS)
  详细解决方案

hdu 1680 Cheesy Chess(BFS)

热度:61   发布时间:2024-01-13 20:20:24.0

1、http://acm.hdu.edu.cn/showproblem.php?pid=1680

2、题目大意:

二人对弈,白先黑后,棋盘固定8*8,白黑双方各执一子,白子曰白王(White King),黑子曰黑卒(Black Pawn)。棋盘上除了空格区域.外还有两种区域,一种是D(Dangerous),另一种是F(Forbidden),规定F区两种棋都不能进入,D区只有白棋不能进入。再来说一下走法,白棋可以往邻接的8个方向走,而黑棋只能往正下方一个方向走。求哪一方赢并输出,所以要说一下胜利标准,白棋的目标是要抓到黑棋,如果白棋走到黑棋占据的格子里,白棋就赢了;任意一方如果走投无路了(轮到它走但它没法走了)另一方就赢了,如果黑棋已经走到最底层又轮到它走了,黑棋就赢了。还有一个比较有意思的point,黑棋的左下和右下两个格子总有两块D(卒前侍卫),它们是跟着黑棋走的。

3、题目:

Cheesy Chess

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 135    Accepted Submission(s): 51


Problem 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 ≤ 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.


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 6 3 7 ........ ........ ........ ........ ........ ........ ........ ........ 3 1 6 3

Sample Output
  
   
Black White

Source
华东区大学生程序设计邀请赛_热身赛

Recommend
lcy
4、AC代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define N 100
char map[10][10];
int wx,wy,hx,hy;
int visit[10][10][100];
int dir[8][2]= {0,1,1,1,1,0,1,-1,0,-1,-1,-1,-1,0,-1,1};
struct node
{int x;int y;int step;
} a[N];
bool judge(node a)
{if(a.x==hx+a.step&&(a.y==hy+1||a.y==hy-1))return false;if(map[a.x][a.y]=='F'||map[a.x][a.y]=='D')return false;return a.x>=1&&a.y>=1&&a.x<=8&&a.y<=8&&!visit[a.x][a.y][a.step];
}
int bfs()
{memset(visit,0,sizeof(visit));int front=0;int rear=1;node cur;a[0].x=wx;a[0].y=wy;a[0].step=0;visit[wx][wy][0]=1;int flag=1;int mm=0;while(front<rear){node cur;cur.x=a[front].x;cur.y=a[front].y;cur.step=a[front].step;//黑被挡住了,白winif(map[hx+cur.step][hy]=='F'){return 1;}//黑到底了,黑winif(hx+cur.step>8){return 0;}for(int i=0; i<8; i++){int tx=cur.x+dir[i][0];int ty=cur.y+dir[i][1];int tstep=cur.step+1;node b;b.step=tstep;b.x=tx;b.y=ty;if((tx>=1 && tx<=8) && (ty>=1 && ty<=8)  && map[tx][ty]!='F' && map[tx][ty]!='D'  && visit[tx][ty][tstep]==0){if(!((tx==hx+tstep && ty==hy-1) || (tx==hx+tstep && ty==hy+1))){visit[tx][ty][tstep]=1;if(tx==hx+cur.step&&ty==hy||tx==hx+tstep&&ty==hy){//黑被抓住||黑被白堵死而走投无路return 1;}node tmp;tmp.x=tx;tmp.y=ty;tmp.step=tstep;a[rear++]=tmp;}}}front++;}return 0;
}
int main()
{//freopen("a.txt","w",stdout);int t;scanf("%d",&t);while(t--){for(int i=1; i<=8; i++){scanf("%s",map[i]+1);}scanf("%d%d",&wx,&wy);int xx=wx;wx=9-wy;wy=xx;scanf("%d%d",&hx,&hy);xx=hx;hx=9-hy;hy=xx;int ans=bfs();if(ans==1){printf("White\n");}elseprintf("Black\n");}return 0;
}