【Fzu】2150 Fire Game(BFS)

9 篇文章 0 订阅
2 篇文章 0 订阅

先判断连通性,然后选择两个点进行BFS,最终时间比一下大小即可,AC时间是1s刚刚好过了。


#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<queue>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
char mz[12][12];
bool vis1[12][12];
int vis[12][12];
int n, m;
int cnt;
int Div[4][2] = { 0, 1, 0, -1, 1, 0, -1, 0 };
bool H[12][12][12][12];

struct pos
{
    int x, y;
    int steps;
    bool friend operator<(pos a, pos b)
    {
        return a.steps>b.steps;
    }
};

bool check(int x, int y)
{
    if (x >= 0 && x < n&&y >= 0 && y < m&&mz[x][y] == '#')
        return true;
    return false;
}

//判断连通性
void dfs(int x, int y,int id)
{
    for (int i = 0; i < 4; i++)
    {
        int xx = x + Div[i][0];
        int yy = y + Div[i][1];
        if (!vis[xx][yy] && check(xx, yy))
        {
            vis[xx][yy] = id;
            dfs(xx, yy,id);
        }
    }
}

int bfs(int x, int y,int x1,int y1)
{
    memset(vis1, 0, sizeof(vis1));
    priority_queue<pos>q;
    pos p, mid;
    p.x = x;
    p.y = y;
    p.steps = 0;
    q.push(p);
    p.x = x1;
    p.y = y1;
    q.push(p);
    int ans = 0;
    vis1[x][y] = 1;
    vis1[x1][y1] = 1;
    while (!q.empty())
    {
        p = q.top();
        q.pop();
        if (p.steps > ans)
            ans = p.steps;
        for (int i = 0; i < 4; i++)
        {
            int xx = p.x + Div[i][0];
            int yy = p.y + Div[i][1];
            if (!vis1[xx][yy] && check(xx, yy))
            {
                vis1[xx][yy] = 1;
                mid.x = xx;
                mid.y = yy;
                mid.steps = p.steps + 1;
                q.push(mid);
            }
        }
    }
    return ans;
}
int main()
{
    int t;
    cin >> t;
    int icase = 1;
    while (t--)
    {
        cin >> n >> m;
        cin.get();
        for (int i = 0; i < n; i++)
            scanf("%s", mz[i]);
        int id = 0;
        memset(vis, 0, sizeof(vis));
        memset(H, 0, sizeof(H));
        for (int i = 0; i < n;i++)
        for (int j = 0; j < m;j++)
        if (mz[i][j] == '#'&&!vis[i][j])
        {
            vis[i][j] = ++id;
            dfs(i, j, id);
        }

        int ans = 0x7ffffff;
        for (int i = 0; i<n;i++)
        for (int j = 0; j < m; j++)
        {
            if (mz[i][j] == '#')
            for (int k = i; k < n; k++)
            for (int l = 0; l<m; l++)
            if ((vis[i][j] != vis[k][l] || id == 1) && mz[k][l] == '#'&&!H[i][j][k][l])
            {
                H[i][j][k][l] = H[k][l][i][j] = 1;
                ans = min(ans, bfs(i, j, k, l));
            }
        }

        cout << "Case " << icase++ << ": ";
        if (id>2)
            cout << "-1" << endl;
        else
            cout << ans << endl;
    }
}

较快的代码,留着思考

#include <string>
#include <queue>
#include <cstring>

using namespace std;

int n, m, ret, res;
string mz[11];
int vis[11][11];
queue<pair<int, int > > q;
int dir[4][2] = {-1,0,1,0,0,1,0,-1};

void bfs(int x1, int y1, int x2, int y2) {
    memset(vis, 0, sizeof(vis));
    while(!q.empty()) q.pop();
    q.push(make_pair(x1,y1));
    q.push(make_pair(x2,y2));
    vis[x1][y1] = 1;
    vis[x2][y2] = 1;
    ret = 1;
    while(!q.empty()) {
        pair<int, int> now = q.front(); q.pop();
        int x = now.first;
        int y = now.second;
        for (int i = 0; i < 4; ++i) {
            int tx = x+dir[i][0];
            int ty = y+dir[i][1];
            if (tx < 0 || ty < 0 || tx >= n || ty >= m) continue;
            if (mz[tx][ty] != '#') continue;
            if (vis[tx][ty]) continue;
            vis[tx][ty] = vis[x][y] + 1;
            if (ret < vis[tx][ty]) ret = vis[tx][ty];
            q.push(make_pair(tx,ty));
        }
    }
    return ;
}

void dfs(int x, int y, int ct) {
    for (int i = 0; i < 4; ++i) {
        int tx = x+dir[i][0];
        int ty = y+dir[i][1];
        if (tx < 0 || ty < 0 || tx >= n || ty >= m) continue;
        if (mz[tx][ty] != '#') continue;
        if (vis[tx][ty]) continue;
        vis[tx][ty] = ct;
        dfs(tx,ty,ct);
    }
    return ;
}

bool ok() {
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            if (mz[i][j] == '#' && !vis[i][j]) 
                return false;
        }
    }
    return true;
}

void output() {
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) 
            cout << vis[i][j] << " ";
        cout << endl;
    }
    return ;
}

bool mmvs[11][11][11][11];

int main() {
    int t;
    cin >> t;
    for (int kase = 1; kase <= t; ++ kase) {
        cin >> n >> m;
        for (int i = 0; i < n; ++i) {
            cin >> mz[i];
        }
        int ct = 0;
        memset(vis,0,sizeof(vis));
        memset(mmvs,0,sizeof(mmvs));
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < m; ++j) {
                if (!vis[i][j] && mz[i][j] == '#') {
                    vis[i][j] = ++ct;
                    dfs(i, j, ct);
                }
            }
        }
        res = 0x7fffffff;
        cout << "Case " << kase << ": ";
        if (ct > 2) { cout << "-1" << endl; continue;}
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < m; ++j) {
                if (mz[i][j] != '#') continue;
                for (int p = 0; p < n; ++p) {
                    for (int q = 0; q < m; ++q) {
                        if (mz[p][q] != '#') continue;
                        //if (i==p && j ==q) continue;
                        if(mmvs[i][j][p][q]) continue;
                        mmvs[i][j][p][q] = mmvs[p][q][i][j] = true;
                        bfs(i,j,p,q);
                        if (ok() && ret-1 < res) res = ret-1;
                        //output();
                    }
                }
            }
        }
        cout << res << endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值