zoukankan      html  css  js  c++  java
  • 深度优先遍历

    /*深搜的灵魂所在就是递归*/
    //以此题为例http://acm.hdu.edu.cn/showproblem.php?pid=1312

    #include<stdio.h>
    #include<string.h>

    char map[25][25];
    int n,m;
    int mark[25][25];

    void dfs(int x,int y)
    {
     if(x>=0&&y>=0&&x<n&&y<m)
     {
      if(map[x][y]=='.'&&mark[x][y]==0)
      {
       mark[x][y]=1;
       dfs(x+1,y);
       dfs(x-1,y);
       dfs(x,y+1);
       dfs(x,y-1);
      }
     }
    }

    int main()
    {
     while(scanf("%d %d",&m,&n)&&(n!=0&&m!=0))
     {
      memset(mark,0,sizeof(mark));
      int i,j;
      int x,y;
      for(i=0;i<n;i++)
      {
       scanf("%s",&map[i]);
      }
      int flag=0;
      for(i=0;i<n;i++)
      {
       for(j=0;j<m;j++)
       {
        if(map[i][j]=='@')
        {
         x=i;
         y=j;
         flag=1;
         break;
        }
       }
       if(flag)
       break;
       }
       map[i][j]='.';
       dfs(x,y);
       int ans=0;
       for(i=0;i<n;i++)
       {
        for(j=0;j<m;j++)
        {
         ans+=mark[i][j];
        }
       }
       printf("%d ",ans);
     }
     return 0;
    }

  • 相关阅读:
    poj1573
    poj2632
    poj2993 poj2996
    poj3295 构造法
    poj2965枚举
    poj1753 枚举
    poj942Paths on a Grid
    poj1019
    poj1321棋盘问题
    博客园访问量有些小,我就没有必要在复制一份了,博客园就这样吧,继续CSDN了。
  • 原文地址:https://www.cnblogs.com/ouyang_wsgwz/p/6480277.html
Copyright © 2011-2022 走看看