【BFS】coj 1061 【魔方系列】最大色块
2017-07-13 19:51
253 查看
【魔方系列】最大色块
Time Limit: 1000msCase 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; }
相关文章推荐
- 【DFS】coj 1061 【魔方系列】最大色块
- 数学之美 系列十六 (下)- 不要把所有的鸡蛋放在一个篮子里 最大熵模型
- 数学之美 系列十七(上) 不要把所有的鸡蛋放在一个篮子里 -- 谈谈最大熵模型
- C/C++面试之算法系列--寻找最大公共子字符串
- 数学之美 系列十六(上) 不要把所有的鸡蛋放在一个篮子里 -- 谈谈最大熵模型
- 最大流最小割算法; BFS搜索增广路径; 算法简单,打印结果也比较清晰;
- 数学之美 系列十六(上) 不要把所有的鸡蛋放在一个篮子里 -- 谈谈最大熵模型
- C/C++面试之算法系列--几个最大子字符串的算法题
- 数学之美 系列十六 (下)- 不要把所有的鸡蛋放在一个篮子里 最大熵模型
- 数学之美 系列十六 (下)- 不要把所有的鸡蛋放在一个篮子里 最大熵模型
- 数学之美 系列十六(上) 不要把所有的鸡蛋放在一个篮子里 -- 谈谈最大熵模型
- 【数学之美 系列十六】 不要把所有的鸡蛋放在一个篮子里 -- 谈谈最大熵模型
- 华为AR28系列限制最大链接数配置
- 数学之美(系列十六 (下)) 之 不要把所有的鸡蛋放在一个篮子里 最大熵模型
- windows系列操作系统支持最大内存
- 数学之美 系列十六(下)- 不要把所有的鸡蛋放在一个篮子里 最大熵模型
- AR28/AR46系列路由器NAT限制最大连接数
- 数学之美 系列十六 (下)- 不要把所有的鸡蛋放在一个篮子里 最大熵模型(转载)
- 数学之美 系列十六(上) 不要把所有的鸡蛋放在一个篮子里 -- 谈谈最大熵模型
- 数学之美系列十六 -- 谈谈最大熵模型[zz]