(hdu step 4.3.1)Tempter of the Bone(在特定的時間約束下,判斷是否能夠從起點達到終點),hdutempter

來源:互聯網
上載者:User

(hdu step 4.3.1)Tempter of the Bone(在特定的時間約束下,判斷是否能夠從起點達到終點),hdutempter

題目:

Tempter of the Bone
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1134 Accepted Submission(s): 379
 
Problem DescriptionThe doggie found a bone in an ancient maze, which fascinated him a lot. However, when he picked it up, the maze began to shake, and the doggie could feel the ground sinking. He realized that the bone was a trap, and he tried desperately to get out of this maze.

The maze was a rectangle with sizes N by M. There was a door in the maze. At the beginning, the door was closed and it would open at the T-th second for a short period of time (less than 1 second). Therefore the doggie had to arrive at the door on exactly the T-th second. In every second, he could move one block to one of the upper, lower, left and right neighboring blocks. Once he entered a block, the ground of this block would start to sink and disappear in the next second. He could not stay at one block for more than one second, nor could he move into a visited block. Can the poor doggie survive? Please help him. 
InputThe input consists of multiple test cases. The first line of each test case contains three integers N, M, and T (1 < N, M < 7; 0 < T < 50), which denote the sizes of the maze and the time at which the door will open, respectively. The next N lines give the maze layout, with each line containing M characters. A character is one of the following:

\\\\\\\'X\\\\\\\': a block of wall, which the doggie cannot enter; 
\\\\\\\'S\\\\\\\': the start point of the doggie; 
\\\\\\\'D\\\\\\\': the Door; or
\\\\\\\'.\\\\\\\': an empty block.

The input is terminated with three 0\\\\\\\'s. This test case is not to be processed. 
Output
            For each test case, print in one line \\\\\\\"YES\\\\\\\" if the doggie can survive, or \\\\\\\"NO\\\\\\\" otherwise. 
Sample Input
4 4 5S.X...X...XD....3 4 5S.X...X....D0 0 0
 
Sample Output
NOYES
 
AuthorZHANG, Zheng 
SourceZJCPC2004 
RecommendJGShining 


題目分析:

                “判斷是否能夠從起點達到終點”,這種題用BFS就能輕鬆搞定。然後這道題的特點就在於“在指定時間的約束下”。那麼這時候我們可以使用DFS來做。   本來想自己寫一下想法的。但是發現網上有人寫的比我將要寫的詳細多了。所以投一下懶,用一下別人的總結。


  1. 1010 temp of the bone
  2. sample input:
  3. 4 4 5
  4. S.X.
  5. ..X.
  6. ..XD
  7. ....
  8. 問題:
  9. (1):
  10. 在發現當前節點無法到達時,這點彈出棧,並且把這點的標記重新刷為'.'
  11. (2):
  12. 如何在dfs中既要保證到達又要使時間正好呢?? 在函數中通過這種形式實現:
  13. dfs(int si,int sj,int cnt) 就是用cnt來記錄當時的時間,並且在
  14. if( si==di && sj==dj && cnt==t )
  15.     {
  16.         escape = 1;
  17.         return;
  18.     }
  19. 的時候 即當前點到達了終點並且時間恰好等於題目所給限制時間時,跳出
  20. 並且escape標記為真
  21. (3):
  22. 如何讓一個點有順序地遍曆它四周地能到達的點呢??
  23. 聰明並且簡短的方法是設施一個dir[4][2] 數組 控制方向
  24. 並且設定它的值為dir[4][2]={{0,-1},{0,1},{1,0},{-1,0}};
  25. 遍曆的時候用for(i:0->4)就非常方便了
  26. (4):
  27. 千萬要注意的是節點越界的情況, dfs(int si,int sj,int cnt)的時候一定要把 si, sj 控制在給你的矩陣內 在後面會提到一個我的列子 就是因為訪問了[0, -1]的位置導致了其
  28. 他資料被更改
  29. (5):
  30. 讀入矩陣的時候,可以採用for(i = 1; i <= N; i++)
  31.                for(j = 1; j <= M; j++)
  32.                 scanf("%c", &map[i][j]);       
  33. 的方法,好處在於可以控制和計算每一個讀入的資料,壞處是調試的時候對矩陣的觀察不太方便,而且好像還會有錯誤,在2102"A計劃"用這種方法讀入資料時好像就會wa,
  34. 另一種方法是for(i = 0; i < N; i++) gets(map[i]);
  35. 這樣讀入的資料在調試觀察的時候十分方便 gets()讀入的預設為字串,在vc調試的時候是顯式的 可以直接觀察矩陣 缺點是對矩陣中各個資料的計算和控制無法實現,需要讀完後再遍曆一遍
  36. (6)
  37. 能用bfs還是盡量用bfs 我不會bfs.... dfs的遞迴在調試的時候不是很方便,而且bfs要比dfs快,調試也要方便,因為它沒有遞迴
  38. (7)
  39. 關於剪枝,沒有剪枝的搜尋不太可能,這題老劉上課的時候講過兩個剪枝,一個是奇偶剪枝,一個是路徑剪枝
  40. 奇偶剪枝:
  41. 把矩陣標記成如下形式:
  42. 0,1,0,1,0
  43. 1,0,1,0,1
  44. 0,1,0,1,0
  45. 1,0,1,0,1
  46. 很明顯,如果起點在0 而終點在1 那顯然 要經過奇數步才能從起點走到終點,依次類推,奇偶相同的偶數步,奇偶不同的奇數步
  47. 在讀入資料的時候就可以判斷,並且做剪枝,當然做的時候並不要求把整個矩陣0,1刷一遍,讀入的時候起點記為(Si,Sj) 終點記為(Di,Dj) 判斷(Si+Sj) 和 (Di+Dj) 的奇偶性就可以了
  48. 路徑剪枝:
  49. 矩陣的大小是N*M 牆的數量記為wall 如果能走的路的數量 N*M - wall 小於時間T,就是說走完也不能到總的時間的,這顯然是錯誤的,可以直接跳出了
  50. 課件裡面給過這題的標程,在dfs的過程中有個沒提到的剪枝,就是記錄當前點到終點的最短路,如果小於剩餘的時間的話,就跳出
  51. 這個剪枝我覺得更科學,它畢竟是動態麼,標程裡面是這麼寫的:
  52. temp = (t-cnt) - abs(si-di) - abs(sj-dj);
  53. if( temp<0 || temp&1 ) return;
  54. 其中求當前點到終點的最短路是這樣 abs(si-di) - abs(sj-dj) 這個就比較粗糙了 明顯沒有考慮到碰到牆要拐彎的情況
  55. 那求最短路有沒有什麼好辦法呢?
  56. 我曾經想到過用 Dijkstraq求最短路的 ,明顯大才小用,在論壇裡看到一個方法覺得可以用在這裡
  57. 給定下例:
  58. S.X.
  59. ..X.
  60. ..XD
  61. ....
  62. 每個點到終點的最短路是不是這樣呢:
  63. S6X2
  64. 65X1
  65. 54XD
  66. 4321
  67. 這怎麼求呢??從終點開始遍曆整個數組,終點是0,它周圍的點都+1,牆就不計數,依次類推,就能求得這個矩陣的一個最短時間矩陣,在dfs的時候比較當前點到終點的最短路,如果大於剩餘時間的話就跳出
  68. 這個方法的預先處理還是非常快的,我沒有用過,但是感覺會非常有用處.
  69. (8)
  70. 在做這題的時候,我碰到過一個神奇的事情,在程式運行至下面代碼時
  71. if( map[ si+dir[i][0] ][ sj+dir[i][1] ] != 'X')           
  72.     map[ si+dir[i][0] ][ sj+dir[i][1] ] = 'X';
  73. T被改變了!! 這絲毫和T沒有關係啊,怎麼改變T的值呢??
  74. 原來在起點map[0][0]進入時,我沒有注意到map[ si+dir[i][0] ][ sj+dir[i][1] ] 實際做的是map[0][-1] = 'X'; 很危險的一個賦值,書本上千萬次強調的東西讓我碰上了,這個地方我找了很久,因此我覺得有必要單獨列出來提醒自己
      


代碼如下:

/* * a.cpp * *  Created on: 2015年2月24日 *      Author: Administrator */#include <iostream>#include <cstdio>using namespace std;const int maxn = 9;char map[maxn][maxn];//地圖矩陣.用於儲存地圖的情況int n;//行數int m;//列數int t;//目標時間int x1,y1;//起點int x2,y2;//終點int dir[4][2]={//方向矩陣{1,0},{-1,0},{0,1},{0,-1}};/** * 判斷下一步是否合法 */bool check(int x,int y){if( x < 0 || x >= n || y <0 || y >= m){//如果座標越界return false;//則表明下一步不合法}return true;//否則下一步合法}/** * DFS。 * si:當前結點的行數 * sj:當前節點的列數 * cnt:到達(si,sj)是所用的時間 */bool dfs(int si,int sj,int cnt){if(si == x2 && sj == y2 && cnt == t){//如果在指定時間內到達目標點return true;//返回true}/** * 如果當前結點越界。這個判斷是不需要的。 * 加了還可能逾時。因為是否越界的判斷在產生下一節點的時候就已經做了 *///if(check(si,sj) == false){//return false;//}/** * * t-cnt:表示剩餘時間 * abs(x2-si) + abs(y2-sj):從當前節點道目標節點所需要的時間 */int temp = (t - cnt) - (abs(x2-si) + abs(y2-sj));//最短路徑剪枝if(temp < 0){//如果剩餘時間不足以到達目標節點return false;//返回false,表示這條路徑不成功}//奇偶剪枝if(temp&1){return false;}int i;for(i = 0 ; i < 4 ; ++i){//遍曆當前節點的相鄰結點int tempi = si+dir[i][0];//計算下一節點的座標int tempj = sj+dir[i][1];if(check(tempi,tempj) == false){//如果計算出來的下一屆點的座標不合法continue;//則跳過這一結點,計算下一結點}if(map[tempi][tempj] != 'X'){//如果當前節點不是牆壁map[tempi][tempj] = 'X';//江當前節點設定為牆壁bool flag = dfs(tempi,tempj,cnt+1);//沿著這一節點往下搜if(flag == true){//如果這一條路徑可行return true;//則返回true.表示目標節點可以到達}//當知心以下代碼的時候表示通過這一節點無法到達目標節點map[tempi][tempj] = '.';//拿獎當前節點重新設定成'.'可用狀態}}return false;//如果經過上面都無法找到一條路徑.那麼到現在的時候就已經表明目標節點無法到達}int main(){while(scanf("%d%d%d",&n,&m,&t) != EOF,n){int i;int j;for(i = 0 ; i < n ; ++i){cin >> map[i];}int wall = 0;//用於包村牆壁的數量for(i = 0 ; i < n ; ++i){for(j = 0 ; j < m ; ++j){if(map[i][j] == 'S'){//記錄起點的位置x1 = i;y1 = j;}else if(map[i][j] == 'D'){//記錄終點的位置x2 = i;y2 = j;}else if(map[i][j] == 'X'){//統計牆壁的數量wall++;}}}//路徑剪枝int nums = n*m - wall;//計算可走的步數if(nums < t){//如果可走的步數<指定的時間printf("NO\n");//那麼表明路已經走完了也還沒有達到指定時間.name返回false.表示沒有這樣的一條路徑continue;}map[x1][y1] = 'X';//獎起點設定為牆壁'x'bool result = dfs(x1,y1,0);//從起點開始遍曆if(result == true){//如果resultweighttrue,表明有這麼一條路徑printf("YES\n");}else{printf("NO\n");//否則表明沒有這樣一條路徑}}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.