0
点赞
收藏
分享

微信扫一扫

hdu 1253 胜利大逃亡 (bfs)


小记:看着提交的状态,那些大神们就是打击我们的自信心的,尼玛这么快,这么小的空间,这么短的代码,。。。唉。。比不得。好好学自己的,相信有一天我也能达到那水平。相信看这篇文章的读者都会达到的。或超越。。。


题解:题目说了六种情况,我就是用个二维数组存起每个方向的加减值,然后对每个点bfs,那些是墙的点 我就当它已经bfs了,直接跳。速度慢的不行,937MS,我擦,求快码参考,跪求。


代码奉上:

#include <stdio.h>
#include <string.h>
#include <queue>

using namespace std;
#define MAX 51

typedef struct n {
    int x,y,z,t;
}NODE;

bool visited[MAX][MAX][MAX];

int dir[6][3] = {
    0,0,1,
    0,0,-1,
    -1,0,0,
    1,0,0,
    0,1,0,
    0,-1,0
};
int s, n, m, T;

int judge(NODE p){
    if(p.x == s - 1 && p.y == n - 1 && p.z == m - 1){
        return 1;
    }
    return 0;
}

int bfs(){
    queue<NODE> q;
    NODE now,next;
    now.x = 0,now.y = 0, now.z = 0, now.t = 0;
    q.push(now);
    while(!q.empty()){
        now = q.front();
        q.pop();
        if(judge(now))return now.t;
        now.t++;
        for(int i = 0; i < 6; i ++){
            next.x = now.x + dir[i][0];
            next.y = now.y + dir[i][1];
            next.z = now.z + dir[i][2];
            next.t = now.t;

            if((next.x >-1&&next.x < s) &&(next.y > -1 && next.y < n) &&(next.z > -1 &&next.z < m)&&!visited[next.x][next.y][next.z]){
                visited[next.x][next.y][next.z] = 1;
                q.push(next);
            }
        }
    }
    return -1;
}
int main() {
    int Cases,ans;
    scanf("%d",&Cases);
    while(Cases--) {
        scanf("%d%d%d%d",&s,&n,&m,&T);
        for(int i = 0; i < s; i++){
            for(int j = 0; j < n; j++){
                for(int k = 0; k < m; k++){
                    scanf("%d",&visited[i][j][k]);
                }
            }
        }
        ans = bfs();
        if(ans > T)
            printf("-1\n");
        else
            printf("%d\n",ans);
    }
    return 0;
}




#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>

using namespace std;

#define mst(a,b) memset(a,b,sizeof(a))
#define REP(a,b,c) for(int a = b; a < c; ++a)
#define eps 10e-8

const int MAX_ = 60;
const int N = 100010;
const int INF = 0x7fffffff;

struct Point {
    int x, y, z;
    int step;
}p2, p1;

int dir[6][3] = {{0,-1,0},{0,1,0},{-1,0,0},{1,0,0},{0,0,1},{0,0,-1}};
int vis[MAX_][MAX_][MAX_];
int a[MAX_][MAX_][MAX_], num[10][10][100];
int n, m, k, ti;

char str[MAX_][MAX_];


int bfs(Point x)
{
    queue<Point> q;

    mst(vis, 0);
    x.step = 0;
    q.push(x);

    while(!q.empty()){
        Point cur;
        cur = q.front(); q.pop();

        if(cur.x == p2.x && cur.y == p2.y && cur.z == p2.z){
            return cur.step;
        }

        REP(i, 0, 6){
            Point nt;
            nt.x = cur.x + dir[i][0];
            nt.y = cur.y + dir[i][1];
            nt.z = cur.z + dir[i][2];
            nt.step = cur.step+1;
            if(nt.step > ti)continue;

            if((nt.x > -1 && nt.x < n) &&
               (nt.y > -1 && nt.y < m) &&
               (nt.z > -1 && nt.z < k) &&
               a[nt.x][nt.y][nt.z] == 0){

                    if(!vis[nt.x][nt.y][nt.z] || nt.step+1 < vis[nt.x][nt.y][nt.z]){
                        vis[nt.x][nt.y][nt.z] = nt.step+1;
                        q.push(nt);
                    }
            }
        }
    }
    return -1;
}

int main(){
	int T, cnt;
	scanf("%d", &T);
	while(T-- && scanf("%d%d%d%d", &n, &m, &k, &ti)){
	    cnt = 0;

        REP(i, 0, n)REP(j, 0, m)REP(r, 0, k){
            scanf("%d", &a[i][j][r]);
        }
        p1.x = p1.y = p1.z = 0;

        p2.x = n-1;
        p2.y = m-1;
        p2.z = k-1;

        int ans;
        ans = bfs(p1);
        printf("%d\n",ans);
	}
	return 0;
}




举报

相关推荐

0 条评论