1536 Naval warfare

Source: Internet
Author: User

Think of a few connected blocks, ~ ~ This is a 5 points

#include <cstdio>#include<iostream>using namespacestd;Charmap[ the][ the];intn,m,ans,vis[ the][ the];voidDfsintIintj) {    if(i<1|| j<1|| i>n| | J&GT;M)return ; if(Vis[i][j])return ; if(!Vis[i][j]) {Vis[i][j]=1; if(map[i][j]=='#'){            intf1=0, f2=0, f3=0, f4=0; if((map[i+1][j]=='#'&&!vis[i+1][J]) | | map[i+1][j]!='#') f1=1; if((map[i-1][j]=='#'&&!vis[i-1][J]) | | map[i-1][j]!='#') f2=1; if((map[i][j+1]=='#'&&!vis[i][j+1])|| map[i][j+1]!='#') f3=1; if((map[i][j-1]=='#'&&!vis[i][j-1])|| map[i][j-1]!='#') f4=1; if(F1&AMP;&AMP;F2&AMP;&AMP;F3&AMP;&AMP;F4) ans++; }} dfs (i+1, J); DFS (I,j+1); DFS (i-1, J); DFS (I,j-1);}intMain () {CIN>>n>>m;  for(intI=1; i<=n;i++)         for(intj=1; j<=m;j++) Cin>>Map[i][j]; DFS (1,1); if(ANS) printf ("there is%d ships.\n", ans); Elseprintf"Bad placement.\n"); return 0;} 

Read the topic, DFS has timed out

#include <cstdio>#include<iostream>using namespacestd;Const intdx[2]={0,1};Const intdy[2]={1,0};Charmap[1010][1010];intn,m,ans,vis[1010][1010];intFlag;voidDfsintIintJintnum) {    if(i<1|| j<1|| i>n| | j>m| | Flagreturn ; if(Vis[i][j]&&vis[i][j]!=num) {flag=1;return ;} VIS[I][J]=num;  for(intk=0;k<2; k++){        intx=i+dx[k],y=j+Dy[k]; if(map[x][y]=='#'&&vis[x][y]>=0) {DFS (x,y,num); }    }}intMain () {CIN>>n>>m;  for(intI=1; i<=n;i++)         for(intj=1; j<=m;j++) Cin>>Map[i][j];  for(intI=1; i<=n&&!flag;i++){         for(intj=1; j<=m&&!flag;j++){            if(map[i][j]=='#'&&map[i+1][j]=='#'&&map[i][j+1]=='#')                if((map[i+1][j+1]=='#'|| map[i-1][j-1]=='#'|| map[i+1][j-1]=='#'|| map[i-1][j+1]=='#')); Else{flag=1; Break; }            if(map[i][j]=='#'&&!Vis[i][j]) {ans++;            DFS (I,j,ans); }        }    }    if(!flag) printf ("there is%d ships.\n", ans); Elseprintf"Bad placement.\n"); return 0;} 

To search the Internet, mostly with BFS, with a

Exercises

Although said is a silver, but I just saw when still have to have the idea Ah!!

Traverse the matrix, find an xx,yy lattice (*.) as the upper-left corner of the current rectangle, start the BFS, if the process appears x< xx| | y< yy Words is impossible (hand painting can be seen), if there is no such situation, the record x-coordinate, the y-coordinate of the maximum value of Maxx,maxy, and then record the number of lattice CNT, if cnt== (maxx-xx+1) * (maxy-yy+1) Indicates that the rectangle is full, the current rectangle conforms to the requirements, one can find it.

AC Code:

#include <queue>#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespacestd;#defineN 1010Const intdx[]={-1,0,1,0};Const intdy[]={0,1,0,-1};CharMap[n][n];intvis[n][n],n,m;structnode{intx, y; Node (intx=0,inty=0): X (x), Y (y) {}};intInsideintXintY) {returnx>=1&&x<=n&&y>=1&&y<=m;}intBFsintXxintyy) {    intMaxx=xx,maxy=yy,cnt=0; Queue<node>P;    Q.push (Node (xx,yy)); VIS[XX][YY]=1;  while(!Q.empty ()) {Node K=Q.front (); Q.pop (); CNT++;  for(intI=0;i<4; i++){            intnx=k.x+dx[i],ny=k.y+Dy[i]; if(Inside (Nx,ny) &&map[nx][ny]=='#'&&!Vis[nx][ny]) {Vis[nx][ny]=1; Maxx=Max (MAXX,NX); Maxy=Max (Maxy,ny);                Q.push (Node (nx,ny)); if(nx<xx| | NY&LT;YY)return 0; }        }    }    if(cnt== (maxx-xx+1) * (maxy-yy+1))return 1; return 0;}intMain () {CIN>>n>>m; intCnt=0;  for(intI=1; i<=n;i++) scanf ("%s", map[i]+1);  for(intI=1; i<=n;i++)         for(intj=1; j<=m;j++)            if(!vis[i][j]&&map[i][j]=='#')                 if(!BFS (I,J)) {printf ("Bad placement.\n");return 0; }                Elsecnt++; printf ("there is%d ships.", CNT); return 0;}

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.