HDU 1253 勝利大逃亡,hdu1253勝利大逃亡

來源:互聯網
上載者:User

HDU 1253 勝利大逃亡,hdu1253勝利大逃亡

題目連結:http://acm.hdu.edu.cn/showproblem.php?pid=1253


解析:

這道題目非常容易TLE,所以我們需要三維BFS+剪枝。


代碼:

#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>#include <algorithm>#include <vector>#include <queue>#include <stack>#include <set>#include <map>using namespace std;#define Lowbit(x) ((x)&(-(x)))#define ll long long#define mp make_pair#define ff first#define ss second#define pb push_back#define mod 10000007//#define LOCAL#define MAXN 100010#define INF 1e9#define Max 100010typedef struct node{    int x,y,z;    int cost;}NODE;bool flag[55][55][55];  //標記是否訪問過int p[55][55][55];      //記錄地圖//六個方向int tx[]={0,0,0,0,1,-1};int ty[]={0,1,0,-1,0,0};int tz[]={1,0,-1,0,0,0};int A,B,C,T;bool check(int x,int y,int z){    if(x<0||x>=A||y<0||y>=B||z<0||z>=C||flag[x][y][z]||p[x][y][z])        return false;    return true;}int main(){    int K;    scanf("%d", &K);    while(K--){        queue<NODE> que;        scanf("%d%d%d%d", &A,&B,&C,&T);        memset(flag, false, sizeof(flag));        int i,j,k;        for(i=0; i<A; ++i){            for(j=0; j<B; ++j){                for(k=0; k<C; ++k){                    scanf("%d", &p[i][j][k]);                    step[i][j][k] = -1;                }            }        }        //BFS        NODE now={0,0,0,0};        int ans = -1;        que.push(now);        flag[0][0][0] = true;        while(!que.empty()){            now = que.front();            que.pop();            if(now.cost>T)  break;            if(now.x==A-1&&now.y==B-1&&now.z==C-1){                ans = now.cost;                break;            }            NODE tmp;            for(i=0; i<6&&flag; ++i){                tmp.x = now.x+tx[i];                tmp.y = now.y+ty[i];                tmp.z = now.z+tz[i];                tmp.cost = now.cost+1;                if(check(tmp.x,tmp.y,tmp.z)){                    flag[tmp.x][tmp.y][tmp.z] = true;                    //剪枝,當該點以最快速度都不能在規定時間到達時,去掉                    if(tmp.cost+abs(tmp.x-A+1)+abs(tmp.y-B+1)+abs(tmp.z-C+1)>T)                        continue;                    que.push(tmp);                }            }        }        printf("%d\n", ans);    }    return 0;}


幫忙講下HDU 1253 勝利大逃亡 的解題思路 (ACM)

用dfs會逾時,而且BFS不剪枝的話也會逾時。。。三維搜尋。。。
搜尋題,三維BFS,設定前後上下左右6個方向搜尋

P.s:1、原來沒有想到當map[A-1][B-1][C-1]是1的情況,所以一直wa,之後上網參考了一下別人的代碼,恍然大悟加了if(map[a-1][b-1][c-1]==1) {printf("-1\n");continue;}後立即A了。

2、要認清剪枝的重要性,剪枝非常有利於提高代碼的效率,就這題而言,若沒有if(a+b+c-3>t) {printf("-1\n");continue;}(起點與終點間最短路徑大於時間限度),雖然沒逾時,但要937MS,加上這句後效率迅速提高到500MS,差不多提高了兩倍
附上已AC的代碼:
#include<stdio.h>

#include<queue>

using namespace std;

typedef struct

{

int x,y,z,steps;

}point;

point start,end;

int a,b,c,t,n;

int map[51][51][51];

int dir[6][3]={{1,0,0}, {-1,0,0}, {0,1,0}, {0,-1,0}, {0,0,1}, {0,0,-1}};

int bfs(point start)

{

queue<point>q;

int i;

point cur,next;

if(start.x==a-1&&start.y==b-1&&start.z==c-1)//考慮起點和終點相同的情況

{

return 0;

}

start.steps=0;

map[start.x][start.y][start.z]=1;

q.push(start);

while(!q.empty())

{

cur=q.front();//取隊首元素

q.pop();

for(i=0;i<6;i++) //廣度優先搜尋

{

next.x=cur.x+dir[i][0];

next.y=cur.y+dir[i][1];

next.z=cur.z+dir[i][2];

if(next.x==a-1 && next.y==b-1 && next.z==c-1) //下一步就是目的地

{

return cur.steps+1;

}

if(next.x>=0&&next.x<a&&next.y>=0&&next.y<b&&next.z>=0&&next.z<c) if(map[next.x][next.y][next.z]!=1) {

map[next.x][next.y][next.z]=1;

next.steps......餘下全文>>
 
hdu1253勝利大逃亡

if(judge(tz+dir[i][0],tx+dir[i][1],ty+dir[i][2]))
{
QueueAppend(Q,tz+dir[i][0],tx+dir[i][1],ty+dir[i][2],tstep+1);
map[tz][tx][ty]=1;
}

map[tz][tx][ty]=1;
改map[tz+dir[i][0]][tx+dir[i][1]][ty+dir[i][2]]=1;試試看
 

聯繫我們

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