码迷,mamicode.com
首页 > 其他好文 > 详细

Problem 2150 Fire Game

时间:2017-10-11 11:15:56      阅读:218      评论:0      收藏:0      [点我收藏+]

标签:desc   logs   other   art   not   def   pac   include   queue   

Problem 2150 Fire Game

kuangbin专题一:I题

Accept: 2745    Submit: 9580
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

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

技术分享 Sample Output

Case 1: 1 Case 2: -1 Case 3: 0 Case 4: 2
 
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std ;

#define maxn 20
#define INF 10000000
int dirx[] = {0 , 0 , -1 , 1 } ;
int diry[] = {-1 , 1 , 0 , 0 } ;

struct node {
    int x ;
    int y ;
    int step ;
};

int min_num , result ;
node st1 , st2 ;
int n , m ;
bool visitt[maxn][maxn][maxn][maxn] ;
bool visit[maxn][maxn] ;
char map[maxn][maxn] ;

bool check(node p) {
    if(p.x>=1 && p.x<=n && p.y>=1 && p.y<=m && !visit[p.x][p.y] && map[p.x][p.y] ==#)
        return true ;
    return false ;
}

void BFS() {
    memset(visit , 0 , sizeof(visit)) ;
    queue<node> Q ;
    Q.push(st1) ;
    Q.push(st2) ;
    visit[st1.x][st1.y] = visit[st2.x][st2.y] = true ; 
    //min_num = 0 ;
    while(!Q.empty()) {
        node q = Q.front() ;
        Q.pop() ;
        min_num = q.step ; //(min_num , q.step) ;
        node turn ;
        for(int i=0 ; i<=3 ; i++) {
            turn.x = q.x + dirx[i] ;
            turn.y = q.y + diry[i] ;
            turn.step = q.step + 1 ;

            if(check(turn)) {
                visit[turn.x][turn.y] = 1 ;
                Q.push(turn) ;
            }
        }
    }
    
    for(int i=1 ; i<=n ; i++) {
        for(int j=1 ; j<=m ; j++) {
            if(!visit[i][j] && map[i][j] == #) {
                min_num = INF ;
            }
        }
    }

}

int main() {
    int t ;
    scanf("%d" , &t) ;
    for(int times = 1 ; times <= t ; times ++) {
        memset(visitt , 0 ,  sizeof(visitt)) ; 
        scanf("%d%d" , &n , &m) ;
        for(int i=1 ; i<=n ; i++) {
            for(int j=1 ; j<=m ; j++) {
                scanf(" %c" , &map[i][j]) ;
            }
        }

        result = INF ;
        // 枚举 两起点 多次广度优先搜索 取最优解
        for(int i=1 ; i<=n ; i++) {
            for(int j=1 ; j<=m  ; j++) {
                for(int k=1 ; k<=n ; k++) {
                    for(int g = 1 ; g <= m ; g++) {
                        if(!visitt[i][j][k][g] && map[i][j]==# && map[k][g] ==#) {
                            st1.x = i , st1.y = j , st2.x = k , st2.y = g ;
                            st1.step = st2.step = 0 ;
                            visitt[i][j][k][g] = 1 ; 
                            BFS() ;
                        //    cout<<result<<min_num<<endl ; 
                            result = min(result , min_num ) ;
                        }
                        
                    }
                }
            }
        }
        if(result == INF)
            printf("Case %d: -1\n", times) ;
        else
            printf("Case %d: %d\n" , times , result) ;
    }
    return 0 ;
}

 

Problem 2150 Fire Game

标签:desc   logs   other   art   not   def   pac   include   queue   

原文地址:http://www.cnblogs.com/yi-ye-zhi-qiu/p/7648609.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!