hdoj 1253 胜利大逃亡 多种解法的比较

#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#include<algorithm>
using namespace std;

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

struct node
{
	int x, y, z;
	int ceng;
}que[50000];

int maze[N][N][N];
int a, b, c, t;
int num;
int head, tail;

int Scan()
{
	int res = 0 , ch;
	while( !( ( ch = getchar() ) >= '0' && ch <= '9' ) )
	{
		if( ch == EOF )  return 1 << 30 ;
	}
	res = ch - '0' ;
	while( ( ch = getchar() ) >= '0' && ch <= '9' )
		res = res * 10 + ( ch - '0' ) ;
	return res ;
}

int BFS()
{
	node cur;
	cur.x = 1, cur.y = 1, cur.z = 1, cur.ceng = 0;
	maze[cur.x][cur.y][cur.z] = 0;

	que[tail++] = cur;
	while(head < tail)
	{
		node temp = que[head++];

		if(temp.ceng > t) //无法到达
			return -1;
		if(temp.x == a && temp.y == b && temp.z == c && temp.ceng <= t)
			return temp.ceng;

		for(int i = 0; i < 6; ++i)
		{
			node now;
			now.x = temp.x + dir[i][0]; now.y = temp.y + dir[i][1]; now.z = temp.z + dir[i][2];
			if(maze[now.x][now.y][now.z])
			{
				now.ceng = temp.ceng + 1;
				que[tail++] = now;
				maze[now.x][now.y][now.z] = 0;
			}
		}
	}
	return -1;
}

int main()
{
	int ncase;
	ncase = Scan();
	while(ncase--)
	{
		num = head = tail = 0;
		memset(maze, 0, sizeof(maze));
		a = Scan(), b = Scan(), c = Scan(), t = Scan();
		for(int i = 1; i <= a; ++i)
			for(int j = 1; j <= b; ++j)
				for(int k = 1; k <= c; ++k)
				{
					maze[i][j][k] = Scan() ^ 1;
					num += maze[i][j][k];
				}

		if(maze[a][b][c] == 0 || num < a + b + c - 3) //终点无法走
		{
			printf("-1\n");
			continue;
		}
		printf("%d\n", BFS());
	}
	return 0;
}

这个算法是对这个题目我所见到的之中最为高效的。


#include <iostream>
#include <queue>
#include<cstring>
#include<cmath>
#include<stdio.h>
using namespace std;

int Castle[52][52][52];  // the castle
int dir[6][3]={       // direction
    {0, 0, 1},   // up
    {0, 0, -1},  // down
    {1, 0, 0},   // left
    {-1, 0, 0},  // right
    {0, 1, 0},   // front
    {0, -1, 0},  // after
};

int A, B, C, T, nCases;

typedef struct node
{
    int x, y, z;
    int step;
}Node;
void BFS()
{
    queue<Node> Que;
    Node pre, last;
    pre.x = pre.y = pre.z = 1;
    pre.step = 0;
    Que.push(pre);
    while(!Que.empty())
    {
        pre = Que.front();
        Que.pop();
        if(pre.step > T)   // 剪枝一
            break;
        if(abs(double(A-pre.x)) + abs(double(B-pre.y)) + abs(double(C-pre.z)) > T)    //剪枝二,这个剪枝使时间减少了
            break;
        if(pre.x==A && pre.y==B && pre.z==C)
        {
            printf("%d\n", pre.step);
            return;
        }
        for(int i=0; i<6; ++i)
        {
            last.x = pre.x+dir[i][0];
            last.y = pre.y+dir[i][1];
            last.z = pre.z+dir[i][2];
            last.step = pre.step+1;
            if(Castle[last.x][last.y][last.z]==0)
                {
                  Castle[last.x][last.y][last.z]=1;
                    Que.push(last);
                }
        }
    }
    printf("-1\n");
}


int main()
{
    freopen("input.txt", "r", stdin);
    scanf("%d", &nCases);
    while(nCases--)
    {
        memset(Castle,1,sizeof(Castle));
        scanf("%d %d %d %d", &A, &B, &C, &T);
        for(int i=1; i<=A; ++i)  //
            for(int j=1; j<=B; ++j)  //
                for(int k=1; k<=C; ++k)   //
                    scanf("%d", &Castle[i][j][k]);
        BFS();
    }
    return 0;
}

本题要注意的是题目中假定0是路,1是墙,并且在优化的时候 可以这样考虑的,广度优先搜索其实都有这个特点的,凡是通过0,1甚至不是的也是可以的,都可以只用一个

数组,保存是否已经访问过的数组完全可以省去,因为题目遍历的时候具有一次性

#include <iostream>
#include <queue>
#include<cstring>
#include<cmath>
#include<stdio.h>
using namespace std;

int Castle[52][52][52];  // the castle
int dir[6][3]={       // direction
    {0, 0, 1},   // up
    {0, 0, -1},  // down
    {1, 0, 0},   // left
    {-1, 0, 0},  // right
    {0, 1, 0},   // front
    {0, -1, 0},  // after
};

int A, B, C, T, nCases;
int head, tail,num;

 struct node
{
    int x, y, z;
    int step;
}que[50000];
void BFS()
{
    node pre ,last;
    pre.x = pre.y = pre.z = 1;
    pre.step = 0;
    que[tail++] = pre;
    while(head<tail)
    {
        node temp = que[head++];
        if(temp.step > T)   // 剪枝一
            break;
        if(abs(double(A-temp.x)) + abs(double(B-temp.y)) + abs(double(C-temp.z)) > T)    //剪枝二,这个剪枝使时间减少了
            break;
        if(temp.x==A && temp.y==B &&temp.z==C)
        {
            printf("%d\n", temp.step);
            return;
        }
        for(int i=0; i<6; ++i)
        {
            last.x = temp.x+dir[i][0];
            last.y = temp.y+dir[i][1];
            last.z = temp.z+dir[i][2];
            last.step = temp.step+1;
            if(Castle[last.x][last.y][last.z]==0)
                {
                  Castle[last.x][last.y][last.z]=1;
                  que[tail++] = last;
                }
        }
    }
    printf("-1\n");
}


int main()
{
    freopen("input.txt", "r", stdin);
    scanf("%d", &nCases);
    while(nCases--)
    {   head = tail = 0;
        memset(Castle,1,sizeof(Castle));
        scanf("%d %d %d %d", &A, &B, &C, &T);
        for(int i=1; i<=A; ++i)  //
            for(int j=1; j<=B; ++j)  //
                for(int k=1; k<=C; ++k)   //
                    scanf("%d", &Castle[i][j][k]);
        BFS();
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值