[IPUOJ10705]最大连通块 (dfs)

1.最大连通块(来源:http://oj.ipoweru.cn/problem/10705)

 (查错有了收获qwq开心)

题目描述

给出一个 x m 的 01 矩阵,求其中最大的一个全为 1 的连通块。

其中连通按四连通计算,即相邻两个格子都是 1 算连通,但斜着的两个格子不算连通。

输入格式

从标准输入读入数据。

输入的第一行包含一个正整数 T ,表示数据的组数。保证 T = 5

对于每个部分,第一行包含两个正整数 n m ,保证 1 \le n,m \le 1000

接下来 n 行,每行为一个长度为 m 的字符串,表示每个格子的情况。为 0 表示格子为白色,为 1 表示格子为黑色。保证至少有一个黑色的格子,保证不会出现其他字符。

输出格式

输出到标准输出。

输出 T 行,每行一个整数,表示最大的全为 1 的连通块。

样例1输入

5

1 20

11111000000011010101

1 20

01011111001000101000

1 20

00000100100000000000

1 20

10000110010000111001

1 20

01110000001000001010

 

样例1输出

5

5

1

3

3

 

样例2输入

5
5 5
10011
00110
01010
11110
11010
5 5
00000
00000
00000
00000
00100
5 5
01100
10011
10110
10101
10100
5 5
00101
10111
10001
01011
11011
5 5
01110
01101
11000
00110
10010

样例2输出

13

1

6

10

7

 

子任务

对于测试点 1 ,保证 n = 1, m = 10

对于测试点 2 ,保证 n = 1, m = 1000

对于测试点 3 ,保证 n = 4, m = 4

对于测试点 4 ,保证 n = 5, m = 6

对于测试点 5 ,保证 n = 1000, m = 1000


 
//40分代码(只有一行的)
#include<bits/stdc++.h> using namespace std; int maxn[25];//当前最长和最终最长 int n,nn,mm; char aa[1005]; int main() { memset(maxn,0,sizeof(maxn)); scanf("%d",&n); for(int i=1;i<=n;i++)maxn[i]=1; int i=1; while(i<=n) { int now=1; //最开始忘了清除! (这就是我调了一小时没有调出来的原因!!!TAT) cin>>nn>>mm; for(int j=1;j<=mm;j++) { cin>>aa[j]; if(j>=2) { //不区分1和2以后的结果就会多一个 if(aa[j]!='0'&&aa[j-1]!='0') { now++; maxn[i]=max(maxn[i],now); //一定要知道这个maxn放在哪里 } else if(aa[j]=='0')now=1; } } i++; }//这是循环结束的那个后括号=.= for(int i=1;i<=n;i++)cout<<maxn[i]<<endl; return 0; }
//100分代码
#include <iostream> #include <cstring> using namespace std; int t, n, m, ans, sum; char map[6][1005][1005]; bool searched[1005][1005]; int dx[4] = { 1, -1, 0, 0 }, dy[4] = { 0, 0, 1, -1 }, tx, ty; void search(int z, int x, int y) { if (ans < sum) ans = sum; for (int i = 0; i <= 3; i++) { tx = x + dx[i]; ty = y + dy[i]; if (map[z][tx][ty] == '1' && tx >= 1 && tx <= n && ty >= 0 && ty < m && searched[tx][ty] == 0) { searched[tx][ty] = 1; sum++; search(z, tx, ty); } } } int main() { cin >> t; for (int i = 1; i <= t; i++) { cin >> n >> m; for (int j = 1; j <= n; j++) cin >> map[i][j]; } for (int i = 1; i <= 5; i++) { for (int j = 1; j <= n; j++) for (int k = 0; k < m; k++) if (map[i][j][k] == '1' && searched[j][k] == 0) { sum++; searched[j][k] = 1; search(i, j, k); sum = 0; } cout << ans << endl; sum = 0; ans = 0; memset(searched, 0, sizeof(searched)); } return 0; }

 

posted @ 2019-07-17 21:19  QUEKI嶺冬  阅读(1096)  评论(0编辑  收藏  举报
/*! Color themes for Google Code Prettify | MIT License | github.com/jmblog/color-themes-for-google-code-prettify */ .pln{color:#4d4d4c}ol.linenums{margin-top:0;margin-bottom:0;color:#8e908c}li.L0,li.L1,li.L2,li.L3,li.L4,li.L5,li.L6,li.L7,li.L8,li.L9{padding-left:1em;background-color:#fff;list-style-type:decimal!important;}@media screen{.str{color:#718c00}.kwd{color:#8959a8}.com{color:#8e908c}.typ{color:#4271ae}.lit{color:#f5871f}.pun{color:#4d4d4c}.opn{color:#4d4d4c}.clo{color:#4d4d4c}.tag{color:#c82829}.atn{color:#f5871f}.atv{color:#3e999f}.dec{color:#f5871f}.var{color:#c82829}.fun{color:#4271ae}} /*下面是我设置背景色,字体大小和字体*/ .cnblogs-markdown code{ background:#fff!important; } .cnblogs_code,.cnblogs_code span,.cnblogs-markdown .hljs{ font-size:16px!important; } .syntaxhighlighter a, .syntaxhighlighter div, .syntaxhighlighter code, .syntaxhighlighter table, .syntaxhighlighter table td, .syntaxhighlighter table tr, .syntaxhighlighter table tbody, .syntaxhighlighter table thead, .syntaxhighlighter table caption, .syntaxhighlighter textarea { font-size: 16px!important; } .cnblogs_code, .cnblogs_code span, .cnblogs-markdown .hljs{ font-family:consolas, "Source Code Pro", monaco, monospace !important; } //以上是代码高亮 /* 文字特效 */