uva 784 Maze Exploration(簡單dfs)

來源:互聯網
上載者:User

標籤:

這道題看上去很麻煩,什麼迷宮啊,門之類的,其實挺簡單的,就是讓把與 * 連通的都置為 # 包括 * ,

直接dfs就可以了,不過我wa了好多次。。。最後竟然是多讀了一個換行,忘了加getchar()了,gets()函數

會把緩衝區裡面的換行給讀進去的。。。應該把換行去掉,血的教訓啊。。。

代碼:

#include<stdio.h> #include<string.h>#include<stdlib.h>int dx[4]={-1,0,0,1};int dy[4]={0,-1,1,0}; char a[35][85];void dfs(int x,int y){for(int i=0; i<4; i++){int xx = x+dx[i];int yy = y+dy[i];if(a[xx][yy]==' '){a[xx][yy]='#';dfs(xx,yy);}}return ;}int main(){int T;scanf("%d",&T);getchar();while(T--){int i =0;int j;memset(a,'\0',sizeof(a));while(gets(a[i++])){if(a[i-1][0]=='_'){i--;break;}}for(int k=0; a[k][0]!='_'; k++){for(j=0; a[k][j]!='\0'; j++){if(a[k][j]=='*'){a[k][j]='#';dfs(k,j);}}}int k;for( k =0; a[k][0]!='_'; k++)puts(a[k]);puts(a[k]);}return 0;}


著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

uva 784 Maze Exploration(簡單dfs)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.