FZU 2150 Fire Game

1 篇文章 0 订阅

题意:给出k个长宽分别为m,n的矩阵,矩阵有若干的区域有草堆,判断草堆连通块数量和点燃所有草堆的时间

链接:http://acm.fzu.edu.cn/problem.php?pid=2150

思路:搜索连通块数量是否大于2,并且分别判断连通块数量为1和2时的情况,当连通块数量为2时,找两个草堆中的两个点花费时间最短;当连通块数量为1时,找出这个草堆中花费时间最短的两个点。

注意点:记录搜索的点的个数,重复搜索会导致TLE


以下为AC代码:

RunIDSubmit TimeStatePIDLanguageTimeMemLenUser
5969802015-02-28 16:33:34Accepted2150GNU C++640 ms284KB3851Bluminous11

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <vector>
#include <deque>
#include <list>
#include <cctype>
#include <algorithm>
#include <climits>
#include <queue>
#include <stack>
#include <cmath>
#include <map>
#include <set>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#define ll long long
#define ull unsigned long long
#define all(x) (x).begin(), (x).end()
#define clr(a, v) memset( a , v , sizeof(a) )
#define pb push_back
#define mp make_pair
#define read(f) freopen(f, "r", stdin)
#define write(f) freopen(f, "w", stdout)
using namespace std;
//const double pi = acos(-1);
const int dir[4][2] = { 1,0, -1,0, 0,1, 0,-1 };
char str[15][15];
char s[15][15];
int vis[15][15];
int path[15][15];
bool num[15][15][15][15];
int m, n;
struct node{
    int x, y, cnt;
    node(){}
    node( int _x, int _y, int _cnt ):x(_x), y(_y), cnt(_cnt){}
};
void init()
{
    scanf ( "%d%d", &m, &n );
    clr ( str, '.' );
    clr ( num, 0 );
    for ( int i = 1; i <= m; i ++ ){
        scanf ( "%s", &str[i][1] );
        str[i][n+1] = '.';
    }
}
int dfs ( int x, int y, int k )
{
    s[x][y] = '.';
    vis[x][y] = k;
    for ( int i = 0; i < 4; i ++ ){
        int xi = x + dir[i][0];
        int yi = y + dir[i][1];
        if ( s[xi][yi] == '#' )
            dfs ( xi, yi, k );
    }
    return 1;
}
int bfs ( int x, int y, int x1, int y1 )
{
    int ans = 0;
    queue<node> q;
    clr ( path, 0x3f3f3f3f );
    q.push ( node ( x, y, 0 ) );
    q.push ( node ( x1, y1, 0 ) );
    path[x][y] = 0;
    path[x1][y1] = 0;
    while ( ! q.empty() ){
        node tmp = q.front();
        q.pop();
        for ( int i = 0; i < 4; i ++ ){
            int xi = tmp.x + dir[i][0];
            int yi = tmp.y + dir[i][1];
            int cnt = tmp.cnt + 1;
            if ( path[xi][yi] > cnt && str[xi][yi] == '#' ){
                path[xi][yi] = cnt;
                q.push ( node ( xi, yi, cnt ) );
                ans = max ( cnt, ans );
            }
        }
    }
    return ans;
}
int solve()
{
    memcpy ( s, str, sizeof ( s ) );
    clr ( vis, 0 );
    int block = 0;
    for ( int i = 1; i <= m; i ++ ){
        for ( int j = 1; j <= n; j ++ ){
            if ( s[i][j] == '#' )
                dfs ( i, j, ++ block );
        }
    }
    if ( block > 2 ){
        return -1;
    }
    if ( block == 0 ){
        return 0;
    }
    else if ( block == 1 ){
        int ans = 0x3f3f3f3f;
        for ( int i = 1; i <= m; i ++ ){
            for ( int j = 1; j <= n; j ++ ){
                if ( str[i][j] == '#' ){
                    for ( int p = 1; p <= m; p ++ ){
                        for ( int q = 1; q <= n; q ++ ){
                            if ( str[p][q] == '#' ){
                                if ( num[i][j][p][q] == 0 ){
                                    ans = min ( ans, bfs ( i, j, p, q ) );
                                    num[i][j][p][q] = num[p][q][i][j] = 1;
                                }
                            }
                        }
                    }
                }
            }
        }
        return ans;
    }
    else if ( block == 2 ){
        int ans[3];
        clr ( ans, 0x3f3f3f3f );
        for ( int i = 1; i <= m; i ++ ){
            for ( int j = 1; j <= n; j ++ ){
                if ( vis[i][j] )
                    ans[vis[i][j]] = min ( ans[vis[i][j]], bfs ( i, j, i, j ) );
            }
        }
        return max ( ans[1], ans[2] );
    }
}
int main()
{
    ios::sync_with_stdio( false );
    int t;
    scanf ( "%d", &t );
    for ( int i = 1; i <= t; i ++ ){
        init();
        printf ( "Case %d: %d\n", i, solve() );
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值