FZU 2150 Fire Game 多起点BFS
2016-10-13 20:10
459 查看
题意:给出一个maze,从任意两点#同时开始覆盖,寻找覆盖所有#的最短时间,若不能全部覆盖,则输出-1。
思路:由于maze很小,10*10而已。枚举任意两个#做bfs就行。
<span style="font-size:14px;">#include <iostream>
#include <queue>
#include <cstdio>
#include <cstring>
using namespace std;
int dirx[]={0,-1,1,0};
int diry[]={-1,0,0,1};
typedef struct Node{
int x;
int y;
int step;
Node(int xx,int yy,int ss){
x=xx;y=yy;step=ss;
}
}Node;
char a[30][30],maze[30][30];bool flag2;
queue <Node> que;
queue <Node> aim;
const int INF=0x3f3f3f3f;
int n,m;
int bfs(Node t){
que.push(t);
maze[t.x][t.y]='.';
Node kk=que.front();
maze[kk.x][kk.y]='.';
while(1){
Node q=que.front();
if(flag2){
aim.push(Node(q.x,q.y,1));
}
que.pop();
for(int i=0;i<4;i++){
int xx=q.x+dirx[i];int yy=q.y+diry[i];
if(xx>=0&&xx<n&&yy>=0&&yy<m&&maze[xx][yy]=='#'){
que.push(Node(xx,yy,q.step+1));
maze[xx][yy]='.';
}
}
if(que.empty()){
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
if(maze[i][j]=='#')
return INF;
return q.step-1;
}
}
}
int main()
{
int T;
scanf("%d",&T);
getchar();
for(int ppp=1;ppp<=T;ppp++){
scanf("%d %d",&n,&m);
getchar();
memset(a,0,sizeof(a));
for(int i=0;i<n;i++)
gets(a[i]);
for(int i=0;i<=n;i++)
for(int j=0;j<=m;j++)
maze[i][j]=a[i][j];
int lim=0;
bool flag=1;
flag2=1;
int ans=-1;
while(!que.empty()) que.pop();
while(!aim.empty()) aim.pop();
for(int i=0;i<n&&flag;i++){
for(int j=0;j<m;j++){
if(maze[i][j]=='#'){
if(lim==2) {flag=0;break;}
lim++;
ans=max(bfs(Node(i,j,1)),ans);
}
}
}
cout<<"Case "<<ppp<<": ";
if(flag){
flag2=0;
while(!aim.empty()){
queue <Node> k=aim;
while(!que.empty()) que.pop();
while(!k.empty()){
for(int i=0;i<=n;i++)
for(int j=0;j<=m;j++)
maze[i][j]=a[i][j];
que.push(aim.front());
ans=min(bfs(k.front()),ans);
k.pop();
}
aim.pop();
}
cout<<ans<<endl;
}else
cout<<-1<<endl;
}
}</span>
Description
Fat brother and Maze are playing a kind of special (hentai) game on an N*M board (N rows, M columns). At the beginning, each grid of this board is consisting of grass or just empty and then they start to fire all the grass. Firstly they choose two grids
which are consisting of grass and set fire. As we all know, the fire can spread among the grass. If the grid (x, y) is firing at time t, the grid which is adjacent to this grid will fire at time t+1 which refers to the grid (x+1, y), (x-1, y), (x, y+1), (x,
y-1). This process ends when no new grid get fire. If then all the grid which are consisting of grass is get fired, Fat brother and Maze will stand in the middle of the grid and playing a MORE special (hentai) game. (Maybe it’s the OOXX game which decrypted
in the last problem, who knows.)
You can assume that the grass in the board would never burn out and the empty grid would never get fire.
Note that the two grids they choose can be the same.
Input
The first line of the date is an integer T, which is the number of the text cases.
Then T cases follow, each case contains two integers N and M indicate the size of the board. Then goes N line, each line with M character shows the board. “#” Indicates the grass. You can assume that there is at least one grid which is consisting of grass
in the board.
1 <= T <=100, 1 <= n <=10, 1 <= m <=10
Output
For each case, output the case number first, if they can play the MORE special (hentai) game (fire all the grass), output the minimal time they need to wait after they set fire, otherwise just output -1. See the sample input and output for more details.
Sample Input
Sample Output
思路:由于maze很小,10*10而已。枚举任意两个#做bfs就行。
<span style="font-size:14px;">#include <iostream>
#include <queue>
#include <cstdio>
#include <cstring>
using namespace std;
int dirx[]={0,-1,1,0};
int diry[]={-1,0,0,1};
typedef struct Node{
int x;
int y;
int step;
Node(int xx,int yy,int ss){
x=xx;y=yy;step=ss;
}
}Node;
char a[30][30],maze[30][30];bool flag2;
queue <Node> que;
queue <Node> aim;
const int INF=0x3f3f3f3f;
int n,m;
int bfs(Node t){
que.push(t);
maze[t.x][t.y]='.';
Node kk=que.front();
maze[kk.x][kk.y]='.';
while(1){
Node q=que.front();
if(flag2){
aim.push(Node(q.x,q.y,1));
}
que.pop();
for(int i=0;i<4;i++){
int xx=q.x+dirx[i];int yy=q.y+diry[i];
if(xx>=0&&xx<n&&yy>=0&&yy<m&&maze[xx][yy]=='#'){
que.push(Node(xx,yy,q.step+1));
maze[xx][yy]='.';
}
}
if(que.empty()){
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
if(maze[i][j]=='#')
return INF;
return q.step-1;
}
}
}
int main()
{
int T;
scanf("%d",&T);
getchar();
for(int ppp=1;ppp<=T;ppp++){
scanf("%d %d",&n,&m);
getchar();
memset(a,0,sizeof(a));
for(int i=0;i<n;i++)
gets(a[i]);
for(int i=0;i<=n;i++)
for(int j=0;j<=m;j++)
maze[i][j]=a[i][j];
int lim=0;
bool flag=1;
flag2=1;
int ans=-1;
while(!que.empty()) que.pop();
while(!aim.empty()) aim.pop();
for(int i=0;i<n&&flag;i++){
for(int j=0;j<m;j++){
if(maze[i][j]=='#'){
if(lim==2) {flag=0;break;}
lim++;
ans=max(bfs(Node(i,j,1)),ans);
}
}
}
cout<<"Case "<<ppp<<": ";
if(flag){
flag2=0;
while(!aim.empty()){
queue <Node> k=aim;
while(!que.empty()) que.pop();
while(!k.empty()){
for(int i=0;i<=n;i++)
for(int j=0;j<=m;j++)
maze[i][j]=a[i][j];
que.push(aim.front());
ans=min(bfs(k.front()),ans);
k.pop();
}
aim.pop();
}
cout<<ans<<endl;
}else
cout<<-1<<endl;
}
}</span>
Description
Fat brother and Maze are playing a kind of special (hentai) game on an N*M board (N rows, M columns). At the beginning, each grid of this board is consisting of grass or just empty and then they start to fire all the grass. Firstly they choose two grids
which are consisting of grass and set fire. As we all know, the fire can spread among the grass. If the grid (x, y) is firing at time t, the grid which is adjacent to this grid will fire at time t+1 which refers to the grid (x+1, y), (x-1, y), (x, y+1), (x,
y-1). This process ends when no new grid get fire. If then all the grid which are consisting of grass is get fired, Fat brother and Maze will stand in the middle of the grid and playing a MORE special (hentai) game. (Maybe it’s the OOXX game which decrypted
in the last problem, who knows.)
You can assume that the grass in the board would never burn out and the empty grid would never get fire.
Note that the two grids they choose can be the same.
Input
The first line of the date is an integer T, which is the number of the text cases.
Then T cases follow, each case contains two integers N and M indicate the size of the board. Then goes N line, each line with M character shows the board. “#” Indicates the grass. You can assume that there is at least one grid which is consisting of grass
in the board.
1 <= T <=100, 1 <= n <=10, 1 <= m <=10
Output
For each case, output the case number first, if they can play the MORE special (hentai) game (fire all the grass), output the minimal time they need to wait after they set fire, otherwise just output -1. See the sample input and output for more details.
Sample Input
4 3 3 .#. ### .#. 3 3 .#. #.# .#. 3 3 ... #.# ... 3 3 ### ..# #.#
Sample Output
Case 1: 1 Case 2: -1 Case 3: 0 Case 4: 2
相关文章推荐
- FZU 2150 Fire Game (双起点BFS)
- FZU 2150 Fire Game(多起点bfs)
- FZU 2150 Fire Game (双起点BFS)
- FZU Problem 2150 Fire Game (双起点BFS啊 )
- FZU 2150 Fire Game (高姿势bfs--两个起点)
- FZU 2150 Fire Game(枚举两个源点的bfs)
- FZU - 2150 Fire Game(BFS)
- FZU 2150 Fire Game (暴搜/ BFS+DFS)
- FZU2150 Fire Game(BFS)
- FZU 2150 fire game (bfs)
- FZU-2150-Fire Game【BFS】
- FZU 2150 Fire Game (BFS_好题)
- 【FZU 2150】Fire Game(BFS)
- FZU - 2150 Fire Game —— BFS
- FZU 2150 Fire Game (BFS)
- FZU2150(Fire Game)(枚举+BFS)
- FZU - 2150 Fire Game(bfs)
- 【Fzu】2150 Fire Game(BFS)
- FZU 2150 Fire Game(bfs)
- fzu 2150 Fire Game_(bfs)