China mining freshman race H Lu Shen See the Island "Bfs/dfs the link block/connected block area at the boundary, this connecting block is invalid"

Source: Internet
Author: User

Time limit: C/s 1 sec, other languages 2 seconds
Space limitations: C/C + + 32768K, other languages 65536K
64bit IO Format:%lld topic description N*m now has a map of the size of the map, marked by the land (denoted by "#") and the Sea (with "." The number of islands on this map is now being calculated by the goddess. It is known that the island is made up of connected blocks of land, that is, the upper, lower, left, right, upper left, upper right, lower left, and lower right side of a land, which constitute connected blocks, etc. In addition, the island's detailed definition is as follows: 1. The islands must be surrounded by oceans. 2, if the connected block has any area at the map boundary, then the connected block is not an island. Input Description:
Line 1th enters two integer n,m, which represents the length and width of the map.
Line 2-n+1, enter m characters per line, character "#" for Land, "." Represents the ocean.
Data guarantee: 0<n,m≤200
Output Description:
Outputs a single line of integers representing the number of islands.
Example 1 input
3 3....# ....
Output
1
Description
Only the median of 1 land is an island, so the number of islands = 1
Example 2 input
3 3#...# ....
Output
0
Description
The middle connecting block has an area at the boundary, so not the island, number of islands = 0.

"Analysis": the BFS solves the connected block, noting that in the solution process, if the connected block region is at the boundary, it is not valid to record this connecting block, but the BFS operation still needs to be completed.
"Code":
#include <bits/stdc++.h>using namespacestd;Const intMAXN = -;Charmp[ -][ -];intvis[maxn][maxn]={0};intdir[][2] ={{1,0},{0,1},{-1,0},{0,-1},{-1,-1},{-1,1},{1,-1},{1,1}};intn,m,flag=0;voidDfsintXinty) {Vis[x][y]=1; if(x==0|| y==0|| x==n-1|| y==m-1) Flag=1;  for(intI=0;i<8; i++)    {        inttx=x+dir[i][0]; intty=y+dir[i][1]; if(mp[tx][ty]=='#'&&tx>=0&&ty>=0) {Mp[tx][ty]='.';        DFS (Tx,ty); }    }}intMain () {intsum=0; CIN>>n>>m;  for(intI=0; i<n;i++) Cin>>Mp[i];  for(intI=0; i<n;i++)    {         for(intj=0; j<m;j++)        {            if(mp[i][j]=='#') {flag=0;                DFS (I,J); if(flag==0) Sum++; } }} cout<<sum<<Endl; return 0;}
DFS

#include <bits/stdc++.h>using namespacestd;Const intmaxn= About; CharMP[MAXN][MAXN];intVIS[MAXN][MAXN];Const intdir[8][2]={{1,0},{-1,0},{0,1},{0,-1},{1,1},{1,-1},{-1,1},{-1,-1}}; intn,m,ans=0;voidBFsintXinty) {    if(mp[x][y]=='.') {Vis[x][y]=2; return; }    if(x==0|| y==0|| x==n-1|| y==m-1) {Vis[x][y]=2; //return;} queue<pair<int,int> >Q;    Q.push (Make_pair (x, y));  while(!Q.empty ()) {        intnowx=Q.front (). First; intnowy=Q.front (). Second; if(nowx==0|| nowy==0|| nowx==n-1|| nowy==m-1) {Vis[x][y]=2; //return;} q.pop ();  for(intI=0;i<8;++i) {            intx1=nowx+dir[i][0]; inty1=nowy+dir[i][1]; if(vis[x1][y1]==0&&mp[x1][y1]=='#') {Vis[x1][y1]=1;            Q.push (Make_pair (x1,y1)); }        }    }} intMain () { while(cin>>n>>L) {memset (MP,0,sizeof(MP)); memset (Vis,false,sizeof(VIS)); Ans=0;  for(intI=0; i<n;++i) Cin>>Mp[i];  for(intI=0; i<n;++i) {             for(intj=0; j<m;++j) {                if(!Vis[i][j]) {Vis[i][j]=1;                    BFS (I,J); if(vis[i][j]==1) {ans++; } }}} cout<<ans<<Endl; }     return 0;}
BFS

China mining freshman race H Lu Shen See the Island "Bfs/dfs the link block/connected block area at the boundary, this connecting block is invalid"

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.