Problem_FZU 2150 Fire Game BFS 两点
来源:互联网 发布:js select 选中值 编辑:程序博客网 时间:2024/05/16 15:53
Accept: 2465 Submit: 8541
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
43 3.#.###.#.3 3.#.#.#.#.3 3...#.#...3 3###..##.#
Sample Output
Case 1: 1Case 2: -1Case 3: 0Case 4: 2
tho: 初始化dis(time)数组为Inf 。每次在队列中加入两个#的点,以这两个点为起点,拓展到的点时间加一。#号位置中dis中最大的即为烧完了的时间。 找出最小时间即为答案。#include <cstdio>#include <cstring>#include <cstdlib>#include <queue>#include <iostream>using namespace std;#define inf 0x3f3f3fint n,m;int dir[4][2]= {{0,1}, {0,-1}, {1,0}, {-1,0}};int dis[15][15];char ma[15][15];struct node{ int x,y;};int bfs(int x1, int y1, int x2, int y2){ int i, j; int xx,yy; queue<node>q; memset(dis,inf,sizeof(dis)); node cur, p1, p2, p; p1.x = x1, p1.y = y1; p2.x = x2, p2.y = y2; dis[x1][y1] = 0; dis[x2][y2] = 0; q.push(p1); q.push(p2); while(!q.empty()) { cur = q.front(); q.pop(); for(i=0; i<=3; i++) { xx = cur.x + dir[i][0]; yy = cur.y + dir[i][1]; if( (xx < 0) || (xx > n-1) || (yy < 0)|| (yy > m-1) ) continue; if(ma[xx][yy]=='#' && dis[xx][yy] > dis[cur.x][cur.y]+1) { dis[xx][yy] = dis[cur.x][cur.y] + 1; p.x = xx; p.y = yy; q.push(p); } } } int maxx=0; for(i=0; i<n; i++) { for(j=0; j<m; j++) { if(ma[i][j]=='#') maxx = max(maxx,dis[i][j]); } } return maxx;}int main(){ int T; int i,j; cin>>T; int cnt=0; while(T--) { cnt++; int t,ans=inf; cin>>n>>m; for(i=0; i<n; i++) scanf("%s",ma[i]); for(i=0; i<n; i++) for(j=0; j<m; j++) if(ma[i][j]=='#') for(int ii=0; ii<n; ii++) for(int jj=0; jj<m; jj++) { if(ma[ii][jj]=='#') { t=bfs(i,j,ii,jj); ans=min(ans, t); } } if(ans == inf) ans=-1; printf("Case %d: %d\n",cnt, ans); } return 0;}
#include <cstdio>#include <cstring>#include <cstdlib>#include <queue>#include <iostream>using namespace std;#define inf 0x3f3f3fint n,m;int dir[4][2]= {{0,1}, {0,-1}, {1,0}, {-1,0}};int dis[15][15];char ma[15][15];struct node{ int x,y;};int bfs(int x1, int y1, int x2, int y2){ int i, j; int xx,yy; queue<node>q; memset(dis,inf,sizeof(dis)); node cur, p1, p2, p; p1.x = x1, p1.y = y1; p2.x = x2, p2.y = y2; dis[x1][y1] = 0; dis[x2][y2] = 0; q.push(p1); q.push(p2); while(!q.empty()) { cur = q.front(); q.pop(); for(i=0; i<=3; i++) { xx = cur.x + dir[i][0]; yy = cur.y + dir[i][1];// if( (xx < 0) || (xx > n-1) || (yy < 0)|| (yy > m-1) ) continue; if(xx>=0&&xx<n&& yy>=0&& yy<m&& ma[xx][yy]=='#' && dis[xx][yy] > dis[cur.x][cur.y]+1 ) { dis[xx][yy] = dis[cur.x][cur.y] + 1; p.x = xx; p.y = yy; q.push(p); } } }// while(!q.empty()) q.pop(); int maxx=0; for(i=0; i<n; i++) { for(j=0; j<m; j++) { if(ma[i][j]=='#') maxx = max(maxx,dis[i][j]); } } return maxx;}int main(){ ios::sync_with_stdio(false);//如果开了这个,就用cin cout。 我用scanf和printf竟然WA或者TLE。。 int T; int i,j; cin>>T; int cnt=0; while(T--) { cnt++; int t,ans=inf; cin>>n>>m; for(i=0; i<n; i++) cin>>ma[i]; for(i=0; i<n; i++) for(j=0; j<m; j++) if(ma[i][j]=='#') for(int ii=0; ii<n; ii++) for(int jj=0; jj<m; jj++) { if(ma[ii][jj]=='#') { t=bfs(i,j,ii,jj); ans=min(ans, t); } } if(ans == inf) ans=-1; cout << "Case " << cnt << ": " << ans << endl; } return 0;}
Case 1: 1Case 2: -1Case 3: 0Case 4: 2
- Problem_FZU 2150 Fire Game BFS 两点
- Fire Game(两点bfs)
- fzu 2150 Fire Game(两点同时bfs)
- FZU - 2150 Fire Game(两点bfs)
- FZU Fire Game(两点BFS)
- 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)
- FZOJProblem 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)
- fzuoj 2150 Fire Game BFS
- activemq 控制面板里的 Number Of Pending Messages、 Messages Enqueued、Messages Dequeued含
- CSS初级课程
- 新手学java必备的基础知识一(与c++相比)
- android四大组件之Activity(一)
- Okhttp和网络数据
- Problem_FZU 2150 Fire Game BFS 两点
- hadoop2.x入门:在windows上用Eclipse编写程序
- 腾讯研发总监王辉:十亿级视频播放技术优化揭秘
- 记Dorado7学习(3)
- 【 hdoj 4240】 Route Redundancy 【dinic+spfa】
- 游戏运维的最佳实践:搜狐畅游自动化运维之旅!
- java Collections.sort()实现List排序的默认方法和自定义方法
- Duilib支持自绘tooltip
- Shell中$0, $?, $!等