zoukankan      html  css  js  c++  java
  • ZOJ2165 简单DFS搜索

     1 #include<iostream>
     2 #include<cstring>
     3 #include<cstdlib>
     4 #include<algorithm>
     5 #define MAXN 25
     6 using namespace std;
     7 int h,w;
     8 int ans;
     9 int dir[4][2]={-1,0,1,0,0,-1,0,1};
    10 char map[MAXN][MAXN];
    11 
    12 bool ok(int x,int y)
    13 {
    14     if(x<0||x>=h) return false;
    15     if(y<0||y>=w) return false;
    16     if(map[x][y]=='#') return false;
    17     return true;
    18 }
    19 void dfs(int a,int b){
    20     map[a][b]='#';
    21     ans++;
    22     int i;
    23     for(i=0;i<4;i++){
    24        int xx=a+dir[i][0];
    25        int yy=b+dir[i][1];
    26        if(ok(xx,yy)){
    27             dfs(xx,yy);
    28        }
    29     }
    30 
    31 }
    32 int main(){
    33     int q,e;
    34     int i,j;
    35     while(cin>>w>>h){
    36         for(i=0;i<h;i++){
    37             for(j=0;j<w;j++){
    38                 cin>>map[i][j];
    39                 if(map[i][j]=='@')
    40                   {q=i,e=j;}
    41             }
    42         }
    43         ans=0;
    44         dfs(q,e);
    45         cout<<ans<<endl;
    46         for(i=0;i<h;i++){
    47             for(j=0;j<w;j++){
    48                 cout<<map[i][j];
    49             }
    50             cout<<endl;
    51         }
    52     }
    53     return 0;
    54 }

    简单的DFS  刚开始把i定义成全局变量  orz  查了好久。。。这游戏太难了

  • 相关阅读:
    django rest_framework中将json输出字符强制为utf-8编码
    Java
    Java
    Oracle
    IDEA
    Ubuntu
    Ubuntu
    Ubuntu
    Ubuntu
    Oracle
  • 原文地址:https://www.cnblogs.com/acplayfacm/p/3840990.html
Copyright © 2011-2022 走看看