#include <iostream>
#include <stdio.h>
//#define DBG
using namespace std;
const int MAXSIZE = 10000;
char map[MAXSIZE][MAXSIZE];
int N,M;
unsigned long LakeCounts;
bool iFirstLake = true;
bool iCounted = true;
void dfs(int x,int y)
{
int m,n;
n = x;m = y;
//边界条件,防止越界
if(m >= M || m < 0) return;
if(n >= N || n < 0) return;
if(map[n][m] == 'W')
{
// if(!iFirstLake)
// {
if(!iCounted)
{
LakeCounts++;
#ifdef DBG
cout << "LAKE COUNTS ADD!";
cout << endl;
cout << "AT LINE : " << n;
cout << " column : " << m << endl;
#endif
iCounted = true;
}
// }
/*
if(iFirstLake)
{
iFirstLake = false;
iCounted = false;
} */
#ifdef DBG
cout << "This is line " << n << " column " << m;
cout << " and this is " << map[n][m] << endl;
#endif
map[n][m] = '.';
dfs(x+1,y);
dfs(x-1,y); // <->
dfs(x,y+1);
dfs(x,y-1); // ↑↓
dfs(x+1,y+1);
dfs(x-1,y-1);//
dfs(x+1,y-1);
dfs(x-1,y+1);///
}
}
int main()
{
char buf;
freopen("b:\\acm\\poj2386\\input.txt","r",stdin);
cin >> N >> M;
for(int i = 0 ; i < N ; i++)
for(int j = 0 ; j < M ; j++)
{
cin >> buf;
if (buf != '\0')
map[i][j] = buf;
}
///这里从0开始搜索貌似不行
#ifdef DBG
for(int i = 0;i < N ; i++)
{
for(int j = 0; j < M ; j++)
{
cout << map[i][j];
}
cout << endl;
}
#endif
for(int i=0;i<N;i++)
{
for(int j = 0; j < M;)
{
if(map[i][j] != 'W')
{
j++;continue;
}
//map[i][j] == 'W'
/*
iCounted = true;
iFirstLake = true; */
iCounted = false;
dfs(i,j);
j++;
}
}
cout << LakeCounts << endl;
// fclose(stdin);
//cout << "Hello world!" << endl;
return 0;
}
【ACM-POJ】poj2386,Lake Counting
最新推荐文章于 2019-07-10 09:26:47 发布