【BFS】coj 1061 【魔方系列】最大色块

【魔方系列】最大色块

Time Limit: 1000ms
Case Time Limit: 30000ms
Memory Limit: 65536KB
64-bit integer IO format: lld     
Java class name:Main
DreamFox有时候还原魔方玩腻了,就会想出一些新点子来玩转魔方。有一次灵感一来,哈,魔方拼字!于是弄来他收藏的12个精品魔方,迅速的拼出了他们宿舍号525,及其壮观华丽!


当然有时也想不到要拼什么好,于是就乱拼,弄出一些乱七八糟的东西。这时候DreamFox的好奇心又来了,这6个颜色哪个颜色连在一起的块数最多呢?比如那个照片上,肯定是黄色连在一起的颜色最大啦。一共有66块(应该没数错吧)。DreamFox现在想知道,每次他随机拼出的图案,最大的那个同颜色色块有多大。当然,他很懒,所以就麻烦你帮忙了~!

Input

第1行:魔方围成的行,列(m,n<=48)
第2行以后:m行n列的二维字符数组,表示魔方拼出的图案。R表示红色,O表示橙色,B表示蓝色,G表示绿色,W表示白色,Y表示黄色。

Output

共1行:表示最大色块的面积(每小格面积为1,若一个块的上下左右四个方向有相同色块,说明相邻)

Sample Input

6 18
YYYYYYYYYYYYYYYYYY
YRRRRYYRRRRYYRRRRY
YRYYYYYYYYRYYRYYYY
YRRRRYYRRRRYYRRRRY
YYYYRYYRYYYYYYYYRY
YRRRRYYRRRRYYRRRRY

Sample Output

66

///AC代码
#include <iostream>
#include <set>
#include <map>
#include <stack>
#include <cmath>
#include <queue>
#include <cstdio>
#include <bitset>
#include <string>
#include <vector>
#include <iomanip>
#include <cstring>
#include <algorithm>
#include <functional>
#define PI acos(-1)
#define eps 1e-8
#define inf 0x3f3f3f3f
#define debug(x) cout<<"---"<<x<<"---"<<endl
typedef long long ll;
using namespace std;

char str[55][55];
bool vis[55][55];
int dix[4] = {1, -1, 0, 0};
int diy[4] = {0, 0, 1, -1};
int m, n;

struct node
{
    int x, y;
};
int cango(int x, int y)
{
    return x >= 0 && x < m && y >= 0 && y < n && vis[x][y] == false;
}

int bfs(int x, int y)
{
    if (vis[x][y] == true)
    {
        return 0;
    }
    queue<node> qu;
    node gg;
    gg.x = x;
    gg.y = y;
    qu.push(gg);
    //memset(vis, false, sizeof(vis));
    vis[x][y] = true;
    int sum = 0;

    while (!qu.empty())
    {
        gg = qu.front();
        qu.pop();
        sum++;
        for (int i = 0; i < 4; i++)
        {
            int _x = gg.x + dix[i];
            int _y = gg.y + diy[i];
            if (cango(_x, _y) && str[_x][_y] == str[x][y])
            {
                node hh;
                hh.x = _x;
                hh.y = _y;
                qu.push(hh);
                vis[_x][_y] = true;
            }
        }
    }
    return sum;
}

int main()
{
    while (cin >> m >> n)
    {
        memset(vis, false, sizeof(vis));
        for (int i = 0; i < m; i++)
        {
            cin >> str[i];
        }
        int ans = 0;
        for (int i = 0; i < m; i++)
        {
            for (int j = 0; j < n; j++)
            {
                ans = max(ans, bfs(i, j));
            }
        }
        cout << ans << endl;
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值