ZOJ2165 簡單DFS搜尋

來源:互聯網
上載者:User

標籤:style   blog   color   os   for   io   

 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  查了好久。。。這遊戲太難了

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.