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

来源:互联网 发布:空间数据的逻辑运算 编辑:程序博客网 时间:2024/06/07 06:14

【魔方系列】最大色块

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 18YYYYYYYYYYYYYYYYYYYRRRRYYRRRRYYRRRRYYRYYYYYYYYRYYRYYYYYRRRRYYRRRRYYRRRRYYYYYRYYRYYYYYYYYRYYRRRRYYRRRRYYRRRRY

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<<"---"<<endltypedef 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;}


原创粉丝点击