zoukankan      html  css  js  c++  java
  • 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.)

    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

    代码:

    #include<iostream>
    #include<stdio.h>
    #include<queue>
    #include<cstring>
    #include<stack>
    #include<vector>
    
    #define INF 0x3f3f3f3f
    
    using namespace std;
    
    char board[15][15];
    bool book[15][15];
    
    struct data{
    	int x,y,TP;
    };
    data p1,p2;
    int FX[4][2] = {{0,1},{1,0},{0,-1},{-1,0}};
    int N,M,sum,Min;
    
    void BFS(){
    	queue<data> Q;
    	Q.push(p1);
    	int result = 1,time = 0;
    	if(p1.x!=p2.x||p1.y!=p2.y){
    		Q.push(p2);
    		result++;
    	}
    	while(!Q.empty()){
    		data TT = Q.front();
    		time = TT.TP;
    		Q.pop();
    		for(int i=0 ; i<4 ; i++){
    			int xt = TT.x+FX[i][0];
    			int yt = TT.y+FX[i][1];
    			if(xt<0 || xt>=N || yt<0 || yt>=M)continue;
    			if(book[xt][yt] || board[xt][yt] == '.')continue;
    			result++;
    			book[xt][yt] = true;
    			data mid;
    			mid.x = xt,mid.y = yt,mid.TP = TT.TP+1;
    			Q.push(mid);
    		}
    	}
    	if(result == sum){
    		if(time<Min)Min = time;
    	}
    }
    
    
    int flag;
    int main(){
    	int T;
    	scanf("%d",&T);
    	while(T--){
    		scanf("%d %d",&N,&M);
    		for(int i=0 ; i<N ; i++)scanf("%s",&board[i]);
    		sum = 0,Min = INF;
    		for(int i=0 ; i<N ; i++){
    			for(int j=0 ; j<M ; j++){
    				if(board[i][j] == '#')sum++;
    			}
    		}
    		for(int i=0 ; i<N ; i++){
    			for(int j=0 ; j<M ; j++){
    				if(board[i][j] == '#'){
    					p1.x = i,p1.y = j,p1.TP = 0;
    					for(int i1=0 ; i1<N ; i1++){
    						for(int j1=0 ; j1<M ; j1++)
    							if(board[i1][j1] == '#'){
    								p2.x = i1,p2.y = j1,p2.TP = 0;
    								memset(book,false,sizeof(book));
    								book[i1][j1] = true;
    								book[i][j] = true;
    								BFS();
    							}
    					}
    				}
    			}
    		}
    		printf("Case %d: ",++flag);
    		if(Min == INF)printf("-1
    ");
    		else printf("%d
    ",Min);
    	}
    	return 0;
    }
    


    双起点其实与单起点没有太大的区别,就是开头两个点入队而已(起码这道题是这样的),千万不要自己吓自己。

  • 相关阅读:
    手写Promise——基于es6的Promise实现(含详细注释)
    手写promise
    package.json里面配置的啥
    package.json配置详解
    package.json的所有配置项及其用法,你都熟悉么
    sass语法进阶小结
    [转]利用vue-cli3快速搭建vue项目详细过程
    vue的接口封装和状态管理
    Vue项目封装请求数据的接口总结
    JSDoc入门使用指南 -- 手摸手教你用JSDoc(超好用的js文档生成工具)
  • 原文地址:https://www.cnblogs.com/vocaloid01/p/9514314.html
Copyright © 2011-2022 走看看