codeforces A. Statues

In this task Anna and Maria play a game with a very unpleasant rival. Anna and Maria are in the opposite squares of a chessboard (8 × 8): Anna is in the upper right corner, and Maria is in the lower left one. Apart from them, the board has several statues. Each statue occupies exactly one square. A square that contains a statue cannot have anything or anyone — neither any other statues, nor Anna, nor Maria.

Anna is present on the board as a figurant (she stands still and never moves), and Maria has been actively involved in the game. Her goal is — to come to Anna's square. Maria and statues move in turn, Maria moves first. During one move Maria can go to any adjacent on the side or diagonal cell in which there is no statue, or she can stay in the cell where she is. The statues during their move must go one square down simultaneously, and those statues that were in the bottom row fall from the board and are no longer appeared.

At that moment, when one of the statues is in the cell in which the Maria is, the statues are declared winners. At the moment when Maria comes into the cell where Anna has been waiting, Maria is declared the winner.

Obviously, nothing depends on the statues, so it all depends on Maria. Determine who will win, if Maria does not make a strategic error.

Input

You are given the 8 strings whose length equals 8, describing the initial position on the board. The first line represents the top row of the board, the next one — for the second from the top, and so on, the last line represents the bottom row. Each character string matches a single cell board in the appropriate row, and the characters are in the same manner as that of the corresponding cell. If the cell is empty, the corresponding character is ".". If a cell has Maria, then it is represented by character "M". If a cell has Anna, it is represented by the character "A". If a cell has a statue, then the cell is represented by character "S".

It is guaranteed that the last character of the first row is always "A", the first character of the last line is always "M". The remaining characters are "." or "S".

Output

If Maria wins, print string "WIN". If the statues win, print string "LOSE".

这个明显的是搜索题。这个是属于障碍物会动的搜索题。这种情况下bfs比起dfs处理起来更方便了。因为BFS可以一步步来求,当然dfs求解这题也是可以的。

主要解题过程如下:开始从上往下从左往右先把节点全部编号为0~63。把当前可满足的点存起来,放在一个队列中,随便数组也可以。当然开始是只有当前点,即左下角那点。然后枚举八个方向,把满足的点放在另一个队列中,暂称为目标队列。重复7、8次,每一次记得要更改status的位置,发现某一次其中目标队列为空那么就是没路可走了,那就输了,否则就是赢。

AC代码:

 

#include <iostream>
#include <cstring>
#include <set>
#include <cstdio>
#include <string>
#include <queue>
using namespace std;
queue<int> q1, q2 ;
char g[20][20];
int vis[70];//是否已存在队列中
int exist[70];//是否为status
int x[8]={0, 0, 1, -1, 1, -1, 1, -1};
int y[8]={1, -1, 0, 0, 1, 1, -1, -1};
int main(){
	int i, j, k;
	for(i=0; i<8; i++){
		scanf("%s", g[i]);
	}
	memset(exist, 0, sizeof(exist));
	for(i=0; i<8; i++){
		for(j=0; j<8; j++){
			if(g[i][j]=='S'){
				exist[i*8+j]=1;
			}
		}
	}
	q1.push(56);
	for(i=1; i<=8; i++){
		if(i%2==0){
			memset(vis, 0, sizeof(vis));
			while(!q2.empty()){
				int num=q2.front(); 
				q2.pop();
				if(!vis[num])
				if(num-8<0||!exist[num-8]){
					q1.push(num);
				}
				int xn=num/8, yn=num%8;
				for(j=0; j<8; j++){
					if(xn+x[j]>=0&&xn+x[j]<=7&&yn+y[j]<=7&&yn+y[j]>=0){
						if(!exist[(xn+x[j])*8+yn+y[j]]&&!vis[(xn+x[j])*8+yn+y[j]]){
							if((xn+x[j])*8+yn+y[j]-8>=0&&exist[(xn+x[j])*8+yn+y[j]-8]){
								continue;
							}
							q1.push((xn+x[j])*8+yn+y[j]);
							vis[(xn+x[j])*8+yn+y[j]]=1;
						}
					}
				}
			}
			if(q1.empty()){
				printf("LOSE");
				return 0;
			}
			for(j=63; j>=0; j--){
				if(exist[j]){
					exist[j]=0;
					if(j<56){
						exist[j+8]=1;
					}
				}
			}
		}
		else{
			memset(vis, 0, sizeof(vis));
			while(!q1.empty()){
				int num=q1.front(); 
				q1.pop();
				int xn=num/8, yn=num%8;
				if(num-8<0||!exist[num-8]){
					q2.push(num);
				}
				for(j=0; j<8; j++){
					if(xn+x[j]>=0&&xn+x[j]<=7&&yn+y[j]<=7&&yn+y[j]>=0){
						if(!exist[(xn+x[j])*8+yn+y[j]]&&!vis[(xn+x[j])*8+yn+y[j]]){
							if((xn+x[j])*8+yn+y[j]-8>=0&&exist[(xn+x[j])*8+yn+y[j]-8]){
								continue;
							}
							q2.push((xn+x[j])*8+yn+y[j]);
							vis[(xn+x[j])*8+yn+y[j]]=1;
						}
					}
				}
			}
			if(q2.empty()){
				printf("LOSE");
				return 0;
			}
			for(j=63; j>=0; j--){
				if(exist[j]){
					exist[j]=0;
					if(j<56){
						exist[j+8]=1;
					}
				}
			}
		}
	}
	printf("WIN");
	return 0;
}
 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值