ACM水題-Rescue LK(AC,迷宮問題,DFS求解)

來源:互聯網
上載者:User

Rescue LK

Time Limit:1000MS  Memory Limit:65536K
Total Submit:83 Accepted:42

Description

ziliang loves LK. But LK was kidnapped by Monster.oyy,and was put in a labyrinth.
After thousands of hard fighting, ziliang finally enter the labyrinth. He must find a way to rescue LK !!! He has a map of the labyrinth, but the puzzle is too complicated, that he need your help.

Input

Input has several case. Each case first give an n and m, that is the height and length of the labyrinth. Followed by the map. In the map, ‘#’ means the wall, ‘Z’ means the position of ziliang, ‘L’
means position of LK. The ‘.’ Means empty cells.
ziliang can only walk on empty cell,and can only go up,down,left,right. And n,m will be less than 50.

Output

If ziliang can reach LK, output “LK has a good life”
Else output “Mission Failed”

Sample Input

5 5######Z..####.##..L######7 7########Z....######.######.##..#..##L.###########

Sample Output

LK has a good lifeMission Failed

Source

GDUT Monthly 2007.11 by ziliang

 

 

 

/*------------------------------------------------------------------------------------------典型的迷宮問題:通常的做法,就是用棧、DFS這兩種做法,不過歸根到底其實也是一種,都是回溯而已,棧與DFS的區別也就是遞迴與非遞迴而已。    大概想法:用DFS來進行路徑探索。每探索一步可行,就設定標誌已經走,一直走下去,直到找到終點  或者遇到不可行的格子為止。找到終點就返回1,遇到不可行就返回上一層,並重圍走過的  格子,換一個方向這樣。狀況:AC,0MS----------------------------------------------------------------------------------------*/#include<stdio.h>char chMap[52][52] ;const int nDir[4][2] = {{1,0},{0,1},{-1,0},{0,-1}} ;   //方向int m = 0 ;int n = 0 ;int zX = 0 ;//起點的X座標int zY = 0 ;//起點的Y座標int DFS(int x,int y) ;int main(void){int i = 0 ;int j = 0 ;while(scanf("%d%d",&n,&m) != EOF){getchar() ;for(i = 0 ; i < n ; ++i){for(j = 0 ; j < m ; ++j){scanf("%c",&chMap[i][j]) ;if('Z' == chMap[i][j]){zX = i ;zY = j ;}}getchar() ;}if(1 == DFS(zX,zY)){puts("LK has a good life") ;}else{puts("Mission Failed") ;}}return 0 ;}int DFS(int x,int y){int i = 0 ;int j = 0 ;if(x < 0 || y < 0 || x >= n || y >= m){return 0 ;}for(i = 0 ; i < 4 ; ++i)//換一個方向{if('L' == chMap[x+nDir[i][0]][y+nDir[i][1]]){return 1 ;}else if('.' == chMap[x+nDir[i][0]][y+nDir[i][1]]){chMap[x+nDir[i][0]][y+nDir[i][1]] = '#' ;//如果格子可行,設定已經if(1 == DFS(x+nDir[i][0],y+nDir[i][1]))//設這個方向走下去{return 1 ;}chMap[x+nDir[i][0]][y+nDir[i][1]] = '.' ;//重設原來的狀態。}}return 0 ;}

 

聯繫我們

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