Ural 1250. Sea burial abstracts DFS from the problem

Source: Internet
Author: User

DFS first finds the sea and marks it out. Second, it marks the overseas DFS, and third, it finds the land.

The starting point of the second DFS is all the points around the graph, so that the four directions of DFS will mark the things not surrounded by the sea, and the rest will be surrounded by the sea.

It is difficult to read the questions and determine whether X and Y are abscissa or ordinate.

#include<iostream>#include<cstdio>#include<cstdlib>#include<cstring>#define MAXN 510using namespace std;int w,h,x,y;char map[MAXN][MAXN];bool vis[MAXN][MAXN];int move4[4][2]= {{0,1},{1,0},{-1,0},{0,-1}};int move8[8][2]= {{0,1},{0,-1},{-1,0},{1,0},{1,1},{-1,1},{1,-1},{-1,-1}};void dfssea(int a,int b){    if(map[a][b]=='1' ) return ;    if(map[a][b]!='.')  return ;    map[a][b]='1';    for(int i=0; i<8; i++)        dfssea(a+move8[i][0],b+move8[i][1]);}void dfsnotsea(int a,int b,char aim){    if(vis[a][b]==true)  return ;    if(map[a][b]=='1')  return ;    if(map[a][b]=='2')   return ;    map[a][b]=aim;    vis[a][b]=true;    for(int i=0; i<4; i++)        dfsnotsea(a+move4[i][0],b+move4[i][1],aim);}void dfs(int a,int b){    if(vis[a][b]||map[a][b]!='#')        return ;    vis[a][b]=true;    for(int i=0; i<4; i++)        dfs(a+move4[i][0],b+move4[i][1]);}int main(){    //freopen("in.txt","r",stdin);    scanf("%d%d%d%d",&w,&h,&x,&y);    getchar();    memset(map,'2',sizeof(map));    for(int i=1; i<=h; i++)    {        for(int j=1; j<=w; j++)            scanf("%c",&map[i][j]);        getchar();    }    memset(vis,false,sizeof(vis));    dfssea(y,x);    memset(vis,false,sizeof(vis));    for(int i=1; i<=w; i++)        dfsnotsea(1,i,'2');    for(int i=1; i<=w; i++)        dfsnotsea(h,i,'2');    for(int i=1; i<=h; i++)        dfsnotsea(i,1,'2');    for(int i=1; i<=h; i++)        dfsnotsea(i,w,'2');    memset(vis,false,sizeof(vis));    int ans=0;    for(int i=1; i<=h; i++)    {        for(int j=1; j<=w; j++)        {            if(map[i][j]=='#'&&vis[i][j]==false)            {                dfs(i,j);                ans++;            }        }    }    cout<<ans<<endl;    return 0;}

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.