FZU 2150 Fire Game(bfs)
来源:互联网 发布:mysql字符集设置 编辑:程序博客网 时间:2024/04/27 19:41
Accept: 515 Submit: 2001
Time Limit: 1000 mSec Memory Limit : 32768 KB
Problem 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
3 3
.#.
###
.#.
3 3
.#.
#.#
.#.
3 3
...
#.#
...
3 3
###
..#
#.#
Sample Output
Case 2: -1
Case 3: 0
Case 4: 2
题意:
'.'表示空地,’#‘表示草地,选两个起点火,求把草全部烧完的最小时间。
题解:
把起始两个点同时放进队列里就行了,具体的话可以理解一下bfs。
#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#include<string>#include<algorithm>#include<cstdlib>#include<set>#include<queue>#include<stack>#include<vector>#include<map>#define N 100010#define Mod 10000007#define lson l,mid,idx<<1#define rson mid+1,r,idx<<1|1#define lc idx<<1#define rc idx<<1|1const double EPS = 1e-11;const double PI = acos(-1.0);typedef long long ll;const int INF=1000010;using namespace std;struct node{ int x, y; int num;};vector<node>q;char mp[11][11];bool vis[11][11];int n,m,Min,ans;int xx[4]= {-1,0,1,0};int yy[4]= {0,1,0,-1};queue<node>que;void bfs(node a,node b){ while(que.size()) que.pop(); memset(vis,0,sizeof vis); vis[a.x][a.y]=1; vis[b.x][b.y]=1; que.push(a); que.push(b); node t,tt; while(que.size()) { t=que.front(); ans=t.num; que.pop(); for(int i=0; i<4; i++) { tt.x=t.x+xx[i]; tt.y=t.y+yy[i]; tt.num=t.num+1; if(tt.x>=0&&tt.x<n&&tt.y>=0&&tt.y<m&&mp[tt.x][tt.y]=='#'&&!vis[tt.x][tt.y]) { que.push(tt); vis[tt.x][tt.y]=1; } } }}int main(){ //freopen("test.in","r",stdin); int t; while(cin>>t) { int ca=1; while(t--) { cin>>n>>m; q.clear(); for(int i=0; i<n; i++) for(int j=0; j<m; j++) { cin>>mp[i][j]; node o; if(mp[i][j]=='#') { o.x=i,o.y=j,o.num=0; q.push_back(o); } } int len=q.size(); Min=INF; for(int i=0; i<len; i++) { for(int j=0; j<len; j++) { ans=0; bfs(q[i],q[j]); int flag=1; for(int i=0; i<n; i++) { for(int j=0; j<m; j++) if(vis[i][j]==0&&mp[i][j]=='#') { flag=0; break; } if(!flag) break; } if(flag) Min=min(Min,ans); } } cout<<"Case "<<ca++<<":"<<" "; if(Min==INF) cout<<"-1"<<endl; else cout<<Min<<endl; } } return 0;}//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--2150--Fire Game【BFS】
- FZU 2150Fire 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)
- FZU 2150 Fire Game BFS
- 【BFS】FZU 2150 Fire Game
- [FZU](2150)Fire Game ---bfs
- fzu Fire Game bfs
- fzu 2150 Fire Game(两点同时bfs)
- FZU - 2150 Fire Game(bfs)
- fzu 2150 Fire Game 【技巧BFS】
- iOS 相对布局 NSLayoutConstraint 初次尝试
- Linux内核的设计与实现 读书笔记(7)中断处理
- 高性能网络编程5--IO复用与并发编程
- 网络流算法Dinic的Python实现
- Android Logcat之过滤多个标签
- FZU 2150 Fire Game(bfs)
- 有趣的注释
- Linux内核设计与实现 读书笔记(8)中断处理下半部
- UVALive - 3938 "Ray, Pass me the dishes!" 线段树
- 高性能网络编程7--tcp连接的内存使用
- 高性能网络编程6--reactor反应堆与定时器管理
- Ubuntu 10.04安装Vim
- Linux内核设计与实现 读书笔记(9)内部同步介绍
- Boosting与Gradient Boosting