The so-called irrigation method? is actually a BFS.
Note that this question is required to determine whether it is legal, only need to record the number of points passed, to determine whether it is a rectangular area.
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
using namespace Std;
BOOL vis[1005][1005];
int n,m,map[1005][1005],flag=0,cnt=0;
int dx[]={0,0,1,0,-1},dy[]={0,1,0,-1,0};
struct code
{
int x, y;
};
Queue <code> q;
Char s[1005];
BOOL Judge (int x,int y)
{
if ((x>=1) && (x<=n) && (y>=1) && (y<=m) && (map[x][y]==1) && (Vis[x] [Y]==false)]
return true;
return false;
}
void BFs (int bx,int by)
{
int cc=1,maxx=bx,maxy=by;
while (!q.empty ())
Q.pop ();
Code begin;
Begin.x=bx;begin.y=by;
Q.push (begin);
Vis[bx][by]=true;
while (!q.empty ())
{
Code Head=q.front ();
Q.pop ();
for (int i=1;i<=4;i++)
{
if (judge (Head.x+dx[i],head.y+dy[i]) ==true)
{
cc++;
Maxx=max (Maxx,head.x+dx[i]);
Maxy=max (Maxy,head.y+dy[i]);
Code now;
Now.x=head.x+dx[i];now.y=head.y+dy[i];
Vis[now.x][now.y]=true;
Q.push (now);
}
}
}
if ((maxx-bx+1) * (maxy-by+1)!=cc) flag=1;
}
int main ()
{
memset (vis,false,sizeof (VIS));
scanf ("%d%d", &n,&m);
for (int i=1;i<=n;i++)
{
scanf ("%s", s);
for (int j=0;j<m;j++)
{
if (s[j]== '. ') map[i][j+1]=0;
else map[i][j+1]=1;
}
}
for (int i=1;i<=n;i++)
for (int j=1;j<=m;j++)
if ((Vis[i][j]==false) && (map[i][j]==1))
{
cnt++;
BFS (I,J);
if (flag==1)
{
printf ("Bad placement.\n");
return 0;
}
}
printf ("There is%d ships.\n", CNT);
return 0;
}
Codevs 1536 Naval Warfare