zoukankan      html  css  js  c++  java
  • POJ 1979

    “.”能走,“#”不能走,“@”为起点,求所有能走到的地方。

    BFS:

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<queue>
     4 using namespace std;
     5 
     6 int w,h;
     7 int dir[4][2]={{0,+1},{-1,0},{0,-1},{+1,0}};
     8 char t[23][23];
     9 bool vis[23][23];
    10 struct type{
    11     int x,y;
    12 }st,now,next;
    13 
    14 int bfs()
    15 {
    16     int cnt=1;
    17     queue<type> q;
    18     q.push(st);
    19     while(!q.empty())
    20     {
    21         now=q.front();q.pop();
    22         for(int i=0;i<4;i++)  
    23         {  
    24             next.x=now.x+dir[i][0] , next.y=now.y+dir[i][1]; //尝试走一步 
    25             if(vis[next.x][next.y]==0) //如果这一步可以走 
    26             {
    27                 vis[next.x][next.y]=1; //将走过的这块tile标记为1
    28                 q.push(next); 
    29                 cnt++; //可走的tile计数+1 
    30             }
    31         }
    32     }
    33     return cnt;
    34 }
    35 
    36 int main()
    37 {
    38     while(scanf("%d%d",&w,&h) && (w*h)!=0)
    39     { 
    40         for(int i=1;i<=h;i++)
    41         {
    42             scanf("%s",(t[i]+1));
    43         }
    44         
    45         //将地图转化为相应的标记数组,所有不能走的、走过的tile均标记为1(包括边界以外的地方以及起点),能走的tile都标记为0
    46         memset(vis,1,sizeof(vis)); 
    47         for(int i=1;i<=h;i++)
    48         {
    49             for(int j=1;j<=w;j++)
    50             {
    51                 if(t[i][j]=='.') vis[i][j]=0;
    52                 if(t[i][j]=='#') vis[i][j]=1;
    53                 if(t[i][j]=='@') st.x=i,st.y=j,vis[i][j]=1;
    54             }
    55         }
    56         
    57         int ans=bfs();
    58         printf("%d
    ",ans);
    59     }
    60 }

     
    DFS:

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<queue>
     4 using namespace std;
     5 
     6 int w,h;
     7 int dir[4][2]={{0,+1},{-1,0},{0,-1},{+1,0}};
     8 char t[23][23];
     9 bool vis[23][23];
    10 struct type{
    11     int x,y;
    12 }st,next;
    13 
    14 void dfs(int x,int y,int &ans)
    15 {
    16     if(vis[x][y]) return; //如果这个tile走过直接退出函数
    17     vis[x][y]=1; //把这块tile标记为已走过
    18     ans++; //计数+1
    19     for(int i=0;i<4;i++)
    20     {
    21         next.x=x+dir[i][0] , next.y=y+dir[i][1]; //走一步
    22         if(next.x >= 1 && next.x <= h && next.y >= 1 && next.y <= w) //如果走的这一步tile在限定的 w * h 范围内就深搜一下
    23         {
    24             dfs(next.x,next.y,ans);
    25         }
    26     }
    27 }
    28 
    29 int main()
    30 {
    31     while(scanf("%d%d",&w,&h) && (w*h)!=0)
    32     { 
    33         for(int i=1;i<=h;i++)
    34         {
    35             scanf("%s",(t[i]+1));
    36         }
    37         
    38         for(int i=1;i<=h;i++)
    39         {
    40             for(int j=1;j<=w;j++)
    41             {
    42                 if(t[i][j]=='.') vis[i][j]=0;
    43                 if(t[i][j]=='#') vis[i][j]=1;
    44                 if(t[i][j]=='@') st.x=i,st.y=j,vis[i][j]=0;
    45             }
    46         }
    47         
    48         int ans=0;
    49         dfs(st.x,st.y,ans);
    50         printf("%d
    ",ans);
    51     }
    52 }


  • 相关阅读:
    获取bootstrap table数据并封装 为json
    不自动切换eclipse视图
    over 分析函数之 lag() lead()
    oracle日期的处理
    表空间的创建
    分析函数 over用法 之row_number() runk_number
    oracle 序列
    Laravel 5
    使用hexo+github搭建免费个人博客详细教程
    windows7设置定时任务运行ThinkPHP框架程序
  • 原文地址:https://www.cnblogs.com/dilthey/p/6804162.html
Copyright © 2011-2022 走看看