Created
April 17, 2016 03:56
-
-
Save jianminchen/be42129298ca4ec156f06ecca58e72a1 to your computer and use it in GitHub Desktop.
use one dimension array to store the value, and no queue, no recursive call; confused, need to figure out how it is designed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cmath> | |
#include <cstdio> | |
#include <vector> | |
#include <iostream> | |
#include <algorithm> | |
#include <cstring> | |
using namespace std; | |
const int MAXN = 111; | |
int Set[MAXN]; | |
void init_set() { | |
memset(Set, -1, sizeof(Set)); | |
} | |
int find_set(int x) { | |
while (Set[x] >= 0) { | |
//Set[x] = find_set(Set[x]);// | |
x = Set[x]; | |
} | |
return x; | |
} | |
int union_set(int x, int y) { | |
int fx = find_set(x); | |
int fy = find_set(y); | |
if (fx == fy) | |
return 0; | |
Set[fx] += Set[fy]; | |
Set[fy] = fx; | |
return 1; | |
} | |
int n, m; | |
int mat[MAXN][MAXN]; | |
const int dx[] = { -1, -1, -1, 0, 1, 1, 1, 0 }; | |
const int dy[] = { -1, 0, 1, 1, 1, 0, -1, -1 }; | |
int id(int x, int y) { | |
return x * m + y; | |
} | |
bool isok(int x, int y) { | |
return x >= 0 && x < n && y >= 0 && y < m; | |
} | |
int main() { | |
while (scanf_s("%d %d", &n, &m) != EOF) { | |
for (int i = 0; i < n; ++i) { | |
for (int j = 0; j < m; ++j) { | |
scanf_s("%d", &mat[i][j]); | |
} | |
} | |
init_set(); | |
for (int i = 0; i < n; ++i) { | |
for (int j = 0; j < m; ++j) { | |
if (mat[i][j] == 0) continue; | |
for (int d = 0; d < 8; ++d) { | |
int ii = i + dx[d]; | |
int jj = j + dy[d]; | |
if (isok(ii, jj) && mat[ii][jj] != 0) { | |
union_set(id(i, j), id(ii, jj)); | |
} | |
} | |
} | |
} | |
int ans = 0; | |
for (int i = 0; i < n; ++i) { | |
for (int j = 0; j < m; ++j) { | |
if (mat[i][j] != 0) { | |
ans = max(ans, -Set[id(i, j)]); | |
} | |
} | |
} | |
printf("%d\n", ans); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment