Codevs 1536 Naval Warfare

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.