Fire Game FZU - 2150

题目链接:Fire Game FZU - 2150

===================================================

Fire Game

Time limit 1000 ms
Mem limit 32768 kB

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

===================================================

算法:BFS

思路:

  • 暴力枚举两个草点开始烧 易错点:在于可以同地方烧

===================================================

#include <iostream>
#include <queue>
#include <cstring>

using namespace std;

int n,m,ans,cao,step=1;
bool cp[15][15],fk[15][15];
int xx[] = {1,0,0,-1};
int yy[] = {0,1,-1,0};

struct node{
    int x,y,z;
};
node k1,k2;

void show(){
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++)
            cout<<fk[i][j]<<" ";cout<<endl;}
}
void show2(){
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++)
            cout<<cp[i][j]<<" ";cout<<endl;}
}

void init(){
    memset(fk,false,sizeof fk);
    ans = -1 , cao = 0 ;
    cin>>n>>m;
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
    {
        char ch = getchar();
        if(ch == '\n') ch = getchar();
        if(ch == '#') cao++,fk[i][j] = true;
    }
    //show();
}

bool check(int x,int y){
    return x>=0&&x<n&&y>=0&&y<m&&cp[x][y]&&fk[x][y];
}

void slove(int a,int b,int c,int d){
    memset(cp,true,sizeof cp);
    k1.x = a,k1.y = b,k1.z= 0; cp[a][b] = false;
    k2.x = c,k2.y = d,k2.z= 0; cp[c][d] = false;
    int cc = 2,cnt = 0;
    queue<node> q;q.push(k1);q.push(k2);
    while(!q.empty()){
        node p = q.front();q.pop();
        int x = p.x, y = p.y,z = p.z;
        //cout<<x<<" "<<y<<" "<<z<<endl;
        for(int i=0;i<4;i++) if(check(x+xx[i],y+yy[i])){
            p.x = x+xx[i], p.y = y+yy[i] , p.z = z + 1;q.push(p);
            cp[p.x][p.y] = false;cnt = p.z;cc++;
        }
    }
    //cout<<cc<<" "<<endl;
    //show2();
    if(cc == cao) ans = (ans==-1)? cnt : min(cnt,ans);
}

int main()
{
    int _;cin>>_;
    while(_--){
        init();
        if(cao==1) {cout<<"Case "<<step++<<": "<<0<<endl;continue;}
        for(int a=0;a<n;a++)
            for(int b=0;b<m;b++)
        {
            if(!fk[a][b]) continue;
            for(int c=a;c<n;c++)
                for(int d=(c==a)?b+1:0;d<m;d++)
            {
                //show();
                if(!fk[c][d]) continue;
                //cout<<a<<" "<<b<<" "<<c<<" "<<d<<" "<<cao<<endl;
                slove(a,b,c,d);
            }
        }
        cout<<"Case "<<step++<<": "<<ans<<endl;
    }
    return 0;
}

//优化 就是 可以用vector存储草坐标

===================================================

#include <iostream>
#include <cstring>
#include <vector>
#include <queue>
#include <cstdio>

using namespace std;

const int inf = 1e9;

int c[4]={1,0,-1,0};
int d[4]={0,1,0,-1};
int n,m;
string s;
bool vis[20][20];
bool akb[20][20];
struct node{
    int x,y,num;
}e;
vector<node> v;

bool ok(int x,int y){
    if(x<0||x>n||y<0||y>m) return false;
    return true;
}

bool check(){
    for(int i = 0;i < v.size();i ++){
        e = v[i];
        int x = e.x , y = e.y;
        if(!vis[x][y]) return false;
    }
    return true;
}

int bfs(node a,node b){
    memset(vis , false , sizeof vis);
    queue<node> q;
    int h;
    a.num = 0 , b.num = 0;
    q.push(a);q.push(b);
    vis[a.x][a.y] = true;vis[b.x][b.y] = true;
    while(!q.empty()){
        e = q.front();q.pop();
        int x  , y ;
        h = e.num;
        node w;
        for(int i= 0;i < 4;i++){
            x = e.x + c[i] , y = e.y + d[i];
            if(ok(x ,y)&&!vis[x][y]&&akb[x][y]){
                w.x = x , w.y = y;
                w.num = e.num + 1;
                vis[x][y]= true;
                q.push(w);
            }
        }
    }
    return h;
}

int main()
{
    int o = 1;int _;cin>>_;
    while(_--){

        memset(akb , false , sizeof akb);
        cin>>n>>m;
        v.clear();
        for(int i=0;i<n;i++){
            cin>>s;
            for(int j=0;j<m;j++)
            if(s[j]=='#'){
                akb[i][j]=true;
                e.x = i , e.y = j;
                v.push_back(e);
            }
        }

        int minn = inf;
        for(int i = 0;i < v.size();i ++)
            for(int j = i;j < v.size();j ++){
                int TnT = bfs(v[i],v[j]);
                if(TnT<minn&&check()) minn = TnT;
            }

        cout<<"Case "<<o++<<": ";
        if(minn == inf) cout<<-1<<endl;
        else cout<<minn<<endl;

    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

盐太郎

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值