POJ 1164 bfs??据说是一道递归的题,硬生生做成bfs

题意:很简单,不多说。

注意:墙和房子都应该占用一个位置,所以说示意图才会这么丑。

分析:

1)如何抽象出模型

2)如何辨别链接的房子

错误分析:

1)

bool find(int &i, int &j)
{
    for (i = 1; i < 2 * m; i += 2)
        for (j = 1; j < 2 * n; j += 2)

步长为2,不是1

2)

int y[4] = {-1, 0, 1, 0}; // calumn
int x[4] = {0, -1, 0, 1}; // row, north is -1,and south is +1

这种要分出前后左右顺序的是最烦的。

3)

        size = 1;// init with 1
        cnt++;

size是1开始,因为一开始就插入一个了。

4)

if(a[next.first][next.second] == 0 && 0 == a[cur.first][cur.second]) {
// cout << next.first << ' '<< next.second << '\t' << cur.first << ' '<< cur.second << '\n';
     continue;
}

"000"连接方式是不可行的。

代码:

#include <iostream>
#include <queue>
#include <utility>

using namespace std;

int a[110][110] = {0}; //0-base
int m, n;
bool v[110][110] = {0};

int y[4] = {-1, 0, 1, 0}; // calumn
int x[4] = {0, -1, 0, 1}; // row, north is -1,and south is +1

// ***
// *x* x is the dot, but wall is not same as the wall
// ***

void getWall(int i, int j)
{
    int tmp;
    cin >> tmp;
    a[i][j] = 2;
    for (int p = 0; p < 4; ++p)
        if ((tmp >> p) & 1)
            a[i + x[p]][j + y[p]] = 1; // cout << i + x[p] << ' ' << j + y[p] << '\n';
                                       // else cout << "sda\n";
}

bool find(int &i, int &j)
{
    for (i = 1; i < 2 * m; i += 2)
        for (j = 1; j < 2 * n; j += 2)
            if (!v[i][j]){
                // cout << i << ' ' << j << '\n';
                return true;
            }
                // return true;

    return false;
}

bool visit(pair<int, int> p)
{
    if (p.first < 1 || p.first >= 2 * m)
        return true;
    if (p.second < 1 || p.second >= 2 * n)
        return true;
    if (a[p.first][p.second] == 1)
        return true;
    if (v[p.first][p.second])
        return true;

    return false;
}

void bfs()
{
    int cnt, maxSize, size; //max-size
    cnt = 0;
    maxSize = 0;

    int i, j;
    pair<int, int> cur, next;
    queue<pair<int, int> > q;

    while (find(i, j))
    {
        q.push(pair<int, int>(i, j));
        v[i][j] = 1;

        size = 1;// init with 1
        cnt++;
        while (!q.empty())
        {
            cur = q.front();
            q.pop();
            for (int r = 0; r < 4; ++r)
            {
                next.first = cur.first + x[r];
                next.second = cur.second + y[r];
                // cout << next.first << ' '<< next.second << '\t' << cur.first << ' '<< cur.second << '\n';
                if(a[next.first][next.second] == 0 && 0 == a[cur.first][cur.second]) {
                    // cout << next.first << ' '<< next.second << '\t' << cur.first << ' '<< cur.second << '\n';
                    continue;
                }

                if (visit(next))
                    continue;
                else
                {
                    q.push(next);
                    // cout << next.first << ' '<< next.second << '\n';
                    v[next.first][next.second] = 1;
                    if (a[next.first][next.second] == 2)
                        size++;
                }
            }
            // system("pause");
        }

        if (maxSize < size)
            maxSize = size;
    }

    //  cout << '\t';
    // for (int i = 0; i <= n * 2; ++i)
    // {
    //     if (i % 2)
    //         cout << i / 2 << ' ';
    //     else
    //         cout << "  ";
    // }
    // cout << '\n';
    // for (int i = 0; i <= m * 2; ++i)
    // {
    //     if (i % 2)
    //         cout << i / 2;
    //     cout << '\t';
    //     for (int j = 0; j <= n * 2; ++j)
    //         cout << v[i][j] << ' ';
    //     cout << '\n';
    // }   

    cout << cnt << '\n' << maxSize << '\n';
}

int main()
{
    cin >> m >> n;
    for (int i = 0; i <= 2 * n; ++i)
        a[0][i] = a[2 * m][i] = 1;
    for (int i = 0; i <= 2 * m; ++i)
        a[i][0] = a[i][2 * n] = 1;

    for (int i = 0; i < m; ++i)
        for (int j = 0; j < n; ++j)
            getWall(i * 2 + 1, j * 2 + 1);

    // cout << '\t';
    // for (int i = 0; i <= n * 2; ++i)
    // {
    //     if (i % 2)
    //         cout << i / 2 << ' ';
    //     else
    //         cout << "  ";
    // }
    // cout << '\n';
    // for (int i = 0; i <= m * 2; ++i)
    // {
    //     if (i % 2)
    //         cout << i / 2;
    //     cout << '\t';
    //     for (int j = 0; j <= n * 2; ++j)
    //         cout << a[i][j] << ' ';
    //     cout << '\n';
    // }

    bfs();
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值