日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

HDU1010 Tempter of the Bone DFS+剪枝

發布時間:2024/10/6 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 HDU1010 Tempter of the Bone DFS+剪枝 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

點擊打開鏈接

Tempter of the Bone

Time Limit: 2000/1000 MS (Java/Others)????Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 141796????Accepted Submission(s): 37833

Problem Description

The 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.

Input

The 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 5

S.X.

..X.

..XD

....

3 4 5

S.X.

..X.

...D

0 0 0

Sample Output

?

NO

YES

Author

ZHANG, Zheng

Source

ZJCPC2004

Recommend

JGShining???|???We have carefully selected several similar problems for you:??1016?1242?1072?1312?1026

題意:

輸入一個n*m的迷宮,T為可以在迷宮中生存的最大時間,。S為起點,D為終點。.為路,X為障礙,每個格子只能踩一次,且只能維持一秒,然后該塊地板就會塌陷。所以你必須每秒走一步,且到D點時,所用時間為T。

題解:

從起點開始向上下左右四個方向搜索,遇到路“."則繼續向下搜索,遇到障礙”X“則結束搜索;當走到D點時且T=0時,輸出YES,否則為NO;這樣樸素的搜索會超時,所以需要剪枝

剪枝1:

如果剩余時間T小于當前點到終點的最短距離,則這條路徑不可能按時到大,停止向下搜索

最短路徑=abs(ex-x)+abs(ey-y);

剪枝2:

初始化n*m的01矩陣
0 1 0 1 0 1
1 0 1 0 1 0
0 1 0 1 0 1
1 0 1 0 1 0
0 1 0 1 0 1
下標從1開始,a【i】【j】=(i+j)% 2
在地圖上,權值相同的兩點距離為偶數
權值不同的兩點距離為奇數
若(a【x】【y】+a【ex】【ey】)為偶數,
如果剩余時間t為偶數,則可能到達
如果剩余時間t為奇數,則無法到達

若(a【x】【y】+a【ex】【ey】)為奇數,
如果剩余時間t為偶數,則無法到達
如果剩余時間t為奇數,則可能到達

綜上,
若(a【x】【y】+a【ex】【ey】+t)為偶數,
則可能到達
如果為奇數,則無法到達

#include<bits/stdc++.h> using namespace std; int sx,sy,ex,ey; int n,m; char mp[10][10]; int d[4][2]={{1,0},{-1,0},{0,1},{0,-1}}; bool flag; void dfs(int x,int y,int t) {if(flag==1) return;//若找到解,則不需要找其他的路徑//剪枝if(t<abs(ex-x)+abs(ey-y)) return ;//如果剩余時間小于當前點到達終點的橫縱坐標之和 /*if((t-abs(ex-x)+abs(ey-y))%2) return;//若無障礙,則差值為0,否則繞道 */ if((t+x+y+ex+ey)%2) return;//剪枝2if(t==0){if(x==ex&&y==ey){flag=1;return;}else return;}elsefor(int i=0;i<4;i++){int nx=x+d[i][0],ny=y+d[i][1];if(nx>0&&nx<=n&&ny>0&&ny<=m&&(mp[nx][ny]=='.'||mp[nx][ny]=='D')){mp[nx][ny]='X';//標記走過dfs(nx,ny,t-1);mp[nx][ny]='.';//回溯}} } int main() {int t;char str[10];while(scanf("%d%d%d",&n,&m,&t)!=EOF){if(n==0&&m==0&&t==0) break;for(int i=1;i<=n;i++){scanf("%s",str);for(int j=1;j<=m;j++){mp[i][j]=str[j-1];if(mp[i][j]=='S')sx=i,sy=j;else if(mp[i][j]=='D')ex=i,ey=j;}}flag=0;dfs(sx,sy,t);if(flag) printf("YES\n");else printf("NO\n");}return 0; }

?

總結

以上是生活随笔為你收集整理的HDU1010 Tempter of the Bone DFS+剪枝的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。