FZU-Problem 2150 Fire Game(兩點bfs)

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.)java

You can assume that the grass in the board would never burn out and the empty grid would never get fire.node

Note that the two grids they choose can be the same.this

Inputspa

The first line of the date is an integer T, which is the number of the text cases.code

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.blog

1 <= T <=100, 1 <= n <=10, 1 <= m <=10遊戲

Outputci

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.get

Sample Inputinput

4
3 3
.#.
###
.#.
3 3
.#.
#.#
.#.
3 3
...
#.#
...
3 3
###
..#
#.#

Sample Output

Case 1: 1
Case 2: -1
Case 3: 0
Case 4: 2

題意:題目的意思是,兩我的在玩遊戲以前,須要把草堆所有清除掉。他們兩個能夠選擇同一個草堆點燃,也能夠選擇不一樣的(但只能點燃一次,此外火勢是能夠蔓延的時間爲1分鐘)。

問是否能夠清除(燃燒)全部的草堆,若能的話輸出作小的燃燒時間,不能就請輸出-1。

思路:首先草堆的數量小於等於2,確定是能燒完的,此時時間輸出爲0

其餘狀況:開始的兩個着火點能夠相同或者不一樣,因此咱們對全部的草堆進行兩兩組合,以這兩個草堆爲着火點同時進行廣搜

若是燃燒的草堆數量等於草堆數量,則返回時間;不然返回-1

注意:代碼實現有不少注意事項,詳見code

代碼:
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Scanner;
class Node{
        int x;
        int y;
        int step;
        public Node(int x,int y,int step){
                this.x=x;
                this.y=y;
                this.step=step;
        }
}
public class Main{
        static int n,m,c,cnt;
        static final int N=15;
        static final int INF=2147483647;
        static char map[][]=new char[N][N];
        static boolean vis[][]=new boolean[N][N];
        static ArrayDeque<Node> q=new ArrayDeque<Node>();
        static Node node[]=new Node[N*N];
        static int dx[]={0,0,1,-1};
        static int dy[]={1,-1,0,0};
        static int bfs(Node t1,Node t2){
                //每次廣搜每次初始化、清空
                for(int i=0;i<N;i++) Arrays.fill(vis[i], false);
                while(!q.isEmpty()) q.poll();
                q.offer(new Node(t1.x,t1.y,0));
                q.offer(new Node(t2.x,t2.y,0));
                vis[t1.x][t1.y]=true;
                vis[t2.x][t2.y]=true;
                //必須判斷是否爲同1個點
                if(t1.x==t2.x && t1.y==t2.y) cnt=1;
                else cnt=2;
//                System.out.println("t1.x="+t1.x+" t1.y="+t1.y+" cnt="+cnt);
                while(!q.isEmpty()){
                        Node t=q.poll();
                        for(int i=0;i<4;i++){
                                int xx=t.x+dx[i];
                                int yy=t.y+dy[i];
                                if(xx<0||yy<0||xx>=n||yy>=m||vis[xx][yy]||map[xx][yy]!='#') continue;
                                vis[xx][yy]=true;
                                cnt++;
                                if(cnt==c) return t.step+1;
                                q.offer(new Node(xx,yy,t.step+1));
                        }
                }
                return -1;
        }
        public static void main(String[] args) {
                Scanner scan=new Scanner(System.in);
                int T=scan.nextInt();
                for(int k=1;k<=T;k++){
                        n=scan.nextInt();
                        m=scan.nextInt();
                        for(int i=0;i<n;i++) map[i]=scan.next().toCharArray();
                        c=0;
                        for(int i=0;i<n;i++)
                            for(int j=0;j<m;j++)
                                    if(map[i][j]=='#'){
                                            node[c++]=new Node(i,j,0);
                                    }
                        if(c<=2) {
                                System.out.println("Case "+k+": 0");    
                                continue;
                        }
                        int ans=INF;
                        for(int i=0;i<c;i++)
                            for(int j=i;j<c;j++){
                                    int  res=bfs(node[i],node[j]);//這裏錯搞了2次bfs
                                    if(res!=-1) ans=Math.min(ans, res);
                            }
                        if(ans==INF) System.out.println("Case "+k+": -1");
                        else System.out.println("Case "+k+": "+ans);
                }
        }
 }
相關文章
相關標籤/搜索