Skip to content

Instantly share code, notes, and snippets.

@KT-Yeh
Created February 16, 2014 02:42
Show Gist options
  • Select an option

  • Save KT-Yeh/9028517 to your computer and use it in GitHub Desktop.

Select an option

Save KT-Yeh/9028517 to your computer and use it in GitHub Desktop.
#include <cstdio>
using namespace std;
char image[30][30];
void DFS(int &n, int i, int j)
{
image[i][j] = '0';
if (i-1 >= 0 && image[i-1][j] == '1') DFS(n, i-1, j);
if (i+1 < n && image[i+1][j] == '1') DFS(n, i+1, j);
if (j-1 >= 0 && image[i][j-1] == '1') DFS(n, i, j-1);
if (j+1 < n && image[i][j+1] == '1') DFS(n, i, j+1);
if (i-1 >= 0 && j-1 >= 0 && image[i-1][j-1] == '1') DFS(n, i-1, j-1);
if (i-1 >= 0 && j+1 < n && image[i-1][j+1] == '1') DFS(n, i-1, j+1);
if (i+1 < n && j-1 >= 0 && image[i+1][j-1] == '1') DFS(n, i+1, j-1);
if (i+1 < n && j+1 < n && image[i+1][j+1] == '1') DFS(n, i+1, j+1);
}
int main()
{
// freopen ("input.txt","rt",stdin);
int n,Case = 1;
while (scanf("%d", &n) != EOF){
getchar();
for (int i = 0; i < n; ++i)
gets(image[i]);
int num = 0;
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j)
if (image[i][j] == '1'){
DFS(n, i, j);
++num;
}
printf("Image number %d contains %d war eagles.\n", Case++, num);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment