Codeforces 128 A Statues【预处理+Bfs】
来源:互联网 发布:网络打印机连接不上 编辑:程序博客网 时间:2024/06/06 12:42
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.
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".
If Maria wins, print string "WIN". If the statues win, print string "LOSE".
.......A................................................M.......
WIN
.......A........................................SS......M.......
LOSE
.......A.................................S......S.......MS......
LOSE
题目大意:
主角开局位于M处,他如果能够走到A处就是WIN,否则就是Lose。
主角先走一步之后, 所有的S下落一层,M不能走S,而且每轮有9种走法:向周围八个地方移动,以及原地不动。
思路:
1、首先预处理出所有S下落的时间,对应设定一个vector<int >mp【i】【j】,表示点(i,j)处会出现落石的时间集合。
2、然后我们Bfs起点,一点一点向A走,如果能够走到,输出WIN,否则输出LOSE即可。
Ac代码:
#include<stdio.h>#include<string.h>#include<queue>using namespace std;struct node{ int x,y,step;}now,nex;char a[10][10];int vis[10][10];vector<int >mp[10][10];int fx[9]={0,0,1,-1,0,1,1,-1,-1};int fy[9]={1,-1,0,0,0,1,-1,1,-1};int n=8,m=8;void Bfs(int sx,int sy){ queue<node >s; now.x=sx; now.y=sy; s.push(now); memset(vis,0,sizeof(vis)); vis[sx][sy]=1; while(!s.empty()) { now=s.front(); if(a[now.x][now.y]=='A') { printf("WIN\n"); return ; } s.pop(); for(int i=0;i<9;i++) { nex.x=now.x+fx[i]; nex.y=now.y+fy[i]; nex.step=now.step; if(nex.x>=0&&nex.x<n&&nex.y>=0&&nex.y<m) { if(i!=4&&vis[nex.x][nex.y]==1)continue; int flag=0; for(int k=0;k<mp[nex.x][nex.y].size();k++) { if(nex.step==mp[nex.x][nex.y][k]||nex.step+1==mp[nex.x][nex.y][k])flag=1; } if(flag==0) { nex.step+=1; s.push(nex); vis[nex.x][nex.y]=1; } } } } printf("LOSE\n"); return ;}int main(){ for(int i=0;i<8;i++) { scanf("%s",a[i]); } int sx,sy; for(int i=0;i<8;i++) { for(int j=0;j<8;j++) { if(a[i][j]=='M') { sx=i;sy=j; } if(a[i][j]=='S') { mp[i][j].push_back(0); for(int k=1;k<8;k++) { int xx=i+k; int y=j; if(xx<8) { mp[xx][y].push_back(k); } } } } } Bfs(sx,sy);}
- Codeforces 128 A Statues【预处理+Bfs】
- CodeForces 128A Statues 简单搜索
- CodeForces 128A Statues [基础DP]
- codeforces A. Statues
- CF 128A Statues
- Codeforces Beta Round #94 (Div. 1 Only)---A.Statues
- cf#94-div1-A. Statues
- codeforces 129C - Statues 图论 DFS
- HDU 2102 A计划 (BFS + 预处理)
- hihocoder1426 What a Ridiculous Election(bfs预处理)
- 【codeforces 367A Sereja and Algorithm】+ 预处理
- Codeforces 598D Igor In the Museum【预处理Bfs】
- BFS预处理 hihoCoder1251 Today Is a Rainy Day
- UVALive - 7263 Today Is a Rainy Day (BFS预处理)
- HDU3533 Escape —— BFS / A*算法 + 预处理
- UVALive 7263 Today Is a Rainy Day(BFS预处理)
- 【codeforces #3】A——BFS+贪心
- Codeforces 377A Maze【Bfs+逆向思维】
- C++单例模式
- js的回调函数
- 机器学习未来的发展趋势、初学者怎样学习机器学习
- 猜数字小游戏
- 04.磁盘
- Codeforces 128 A Statues【预处理+Bfs】
- PHP.6-PHP环境搭建(Windows环境下)-LAMP
- 简单工厂的核心 根据用户的输入创建对象赋值给父类
- 区块链制作教程
- 在Linux CentOS 6.8上安装Python 2.7.12
- 二叉树的重构(知道前序中序)
- 全栈工程师修炼史
- jdk与jre的区别
- 八大排序--选择排序