FZU Problem 2150 Fire Game (BFS,java)
来源:互联网 发布:java jsoup爬虫技术 编辑:程序博客网 时间:2024/05/22 10:59
Problem 2150 Fire Game
Accept: 2070 Submit: 7235
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
Sample Output
Case 2: -1
Case 3: 0
Case 4: 2
题目链接:http://acm.fzu.edu.cn/problem.php?pid=2150
解题思路,典型的BFS,枚举两个点,当两个点都满足“点火”条件时,两个点都压入队列(如果两个点位置重合,压入一个点即可)。每个点结构的成员变量有横坐标x、纵坐标y和步数cnt。
代码如下:
import java.util.*;public class Main{public static int T,n,m;public static int[] dx = {0,-1,0,1}; //改变上下左右四个方向的数组public static int[] dy = {-1,0,1,0};public static boolean flog= false; //flog用来标记是否能“完全燃烧”矩阵里的草public static char[] ch[] = new char[12][12]; //矩阵模型public static boolean[] vis[] = new boolean[12][12]; //标记是否访问过当前位置static class node{ //每个点的结构体int x;int y;int cnt;public node(int x,int y,int cnt){this.x = x;this.y = y;this.cnt = cnt;}}public static int BFS(int x1,int y1,int x2,int y2,int number){Queue <node> q = new LinkedList<node>(); //注意队列的初始化一定要写在BFS中q.add(new node(x1,y1,0)); //把初始位置的点压入队列if(x1!=x2 || y1!=y2){q.add(new node(x2,y2,0));}int curans = 0;while(!q.isEmpty()){node nd = q.poll(); //取出队首的点并把点从队列中移除boolean f = true; //用来判断在该点之后是否能继续走下去for(int i=0;i<4;i++){int xx = nd.x+dx[i];int yy = nd.y+dy[i];int ct = nd.cnt+1;if(xx < 0 || yy<0 || xx>=n ||yy>=m|| ch[xx][yy]=='.' || vis[xx][yy]){continue; //判断下一个点是否出界以及是否满足条件}vis[xx][yy] = true;f = false;number--; //每走完一步,剩余种草的位置个数减1if(number==0){ //燃烧完所有的草,返回最大步数flog = true;curans = Math.max(curans,ct);return curans;}node dd = new node(xx,yy,ct);q.add(dd); //把新的点压入队列}if(f){ //不能继续走下去,纪录当前点的步数curans = nd.cnt;}}return Integer.MAX_VALUE;}public static void main(String[] args){Scanner sc = new Scanner(System.in);T = sc.nextInt();for(int ca = 1;ca <= T;ca ++){n = sc.nextInt();m = sc.nextInt();int num = 0;String str = new String();for(int i=0;i<n;i++){str = sc.next();for(int j=0;j<str.length();j++){ch[i][j] = str.charAt(j);if(ch[i][j]=='#'){num++;}}}if(num<=2){ //种草的位置不多于2格,0步即可燃烧完System.out.println("Case "+ca+": 0");continue;}int ans = Integer.MAX_VALUE; //初始化ans的值为最大值for(int i=0;i<n;i++){for(int j=0;j<m;j++){if(ch[i][j]=='.'){continue;}for (int h = i;h<n;h++){for(int k=0;k<m;k++){if(ch[h][k]=='.'){continue;}flog = false; //初始化flog的值for(int i1=0;i1<12;i1++){ //初始化标记数组visfor(int i2=0;i2<12;i2++){vis[i1][i2] = false;}}vis[i][j] = true;vis[h][k] = true; int cur = BFS(i,j,h,k,num-2);if(flog){ans = Math.min(ans,cur); //成功燃烧完一次,更新ans为最小值}}}}}if(ans==Integer.MAX_VALUE){System.out.println("Case "+ca+": -1");}else{System.out.println("Case "+ca+": "+ans);}}}}
- FZU Problem 2150 Fire Game (BFS,java)
- 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)
- FZU 2150 Fire Game (DFS + BFS)
- FZU 2150 Fire Game(dfs+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.2150 Fire Game (BFS)
- FZU-2150---Fire Game (双向bfs)
- fzu 2150 Fire Game(BFS)
- discuz,table类文件函数返回数组,在控制器中读取并处理
- leetcode84. Largest Rectangle in Histogram
- HTTP Error 503. The service is unavailable.
- 使用xmake优雅地描述工程
- 2017-04-06
- FZU Problem 2150 Fire Game (BFS,java)
- 航天战场态势感知平台
- css居中和布局
- 2017最优秀的面试题
- SIMD——MMX指令集介绍
- thrift开发问题总结
- 并查集分析
- 排序
- 站点的前后数据加密