POJ 2251 bfs

DESCRIPTION:
给你一个三维的迷宫。问你是否能从起点走到终点。如果能,输出最小步数。对我来说难得就是我没有想到怎么把他给你的三维图转换成map。恩。、好像解题报告上说。只要是这种的最短路都要用bfs。用dfs回很难。不太懂耶。>_<...

然后就是普通的bfs了。然后忘了三个输入全为0的时候结束程序。然后WA了一会。。然后就没有然后了。233333333333

附代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std;

int l, r, c;
int ans;
bool used[40][40][40];
bool map[40][40][40];

struct Node
{
    int l, r, c;
    int step;
    Node()
    {
        step = 0;
    }
}node[30000], now, temp, st, ed;

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

bool check(int l, int r, int c)
{
    if (l >= 0 && l < 30 && r >= 0 && r < 30 && c >= 0 && c < 30 && !used[l][r][c] && map[l][r][c])
        return true;
    return false;
}

bool bfs(int i, int j, int k)
{
     int top = 0;
     int tail = 0;
     node[tail++] = st;
     used[st.l][st.r][st.c] = 1;
     while(top < tail)
     {
         now = node[top++];
         if (now.l == ed.l && now.r == ed.r && now.c == ed.c)
         {
             ans = now.step;
             return true;
         }
         for (int i=0; i<6; ++i)
         {
             temp.l = now.l + move[i][0];
             temp.r = now.r + move[i][1];
             temp.c = now.c + move[i][2];
             if (check(temp.l, temp.r, temp.c))
             {
                 temp.step = now.step + 1;
                 node[tail++] = temp;
                 used[temp.l][temp.r][temp.c] = true;
             }
         }
     }
     return false;
}

int main()
{
    char t;
    while(cin >> l)
    {
        cin >> r >> c;
        if (l == 0 && r == 0 && c == 0)
            break;
        memset(used, 0, sizeof(used));
        memset(map, 0, sizeof(map));
        for (int ll=0; ll<l; ++ll)
        {
            for (int rr=0; rr<r; ++rr)
            {
                for (int cc=0; cc<c; ++cc)
                {
                    cin >> t;
                   if (t == ‘S‘)
                   {
                     map[ll][rr][cc] = true;
                     st.l = ll;
                     st.r = rr;
                     st.c = cc;
                     st.step = 0;
                   }
                   if (t == ‘.‘)
                    map[ll][rr][cc] = true;
                   if (t == ‘E‘)
                   {
                       map[ll][rr][cc] = true;
                       ed.l = ll;
                       ed.r = rr;
                       ed.c = cc;
                   }
                }
            }
        }
        if (bfs(st.l, st.r, st.c))
            cout << "Escaped in " << ans << " minute(s)." << endl;
        else cout << "Trapped!\n";
    }
    return 0;
}

时间: 2025-01-01 23:14:12

POJ 2251 bfs的相关文章

POJ 2251 Dungeon Master --- 三维BFS(用BFS求最短路)

POJ 2251 题目大意: 给出一三维空间的地牢,要求求出由字符'S'到字符'E'的最短路径,移动方向可以是上,下,左,右,前,后,六个方向,每移动一次就耗费一分钟,要求输出最快的走出时间.不同L层的地图,相同RC坐标处是相连通的.(.可走,#为墙) 解题思路:从起点开始分别往6个方向进行BFS(即入队),并记录步数,直至队为空.若一直找不到,则困住. /* POJ 2251 Dungeon Master --- 三维BFS(用BFS求最短路) */ #include <cstdio> #i

POJ 2251:Dungeon Master(三维BFS)

Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16178 Accepted: 6268 Description You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled wit

POJ 2251 Dungeon Master(三维6方向BFS)

B - Dungeon Master Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2251 Appoint description:  System Crawler  (2015-03-28) Description You are trapped in a 3D dungeon and need to find the quicke

BFS POJ 2251 Dungeon Master

题目传送门 1 /* 2 BFS:这题很有意思,像是地下城,图是立体的,可以从上张图到下一张图的对应位置,那么也就是三维搜索,多了z坐标轴 3 */ 4 #include <cstdio> 5 #include <algorithm> 6 #include <cstring> 7 #include <queue> 8 using namespace std; 9 10 const int MAXN = 33; 11 const int INF = 0x3f3

poj 2251 三维地图bfs

三维地图 poj 2251 #include<iostream> #include<queue> #include<cstdio> #include<cstring> using namespace std; char map[35][35][35]; int l,r,c; bool book[35][35][35]; // 定义 东西南北和上下 struct dis{ int x,y,z; int step; dis(int x,int y,int z,i

POJ 2251 Dungeon Master(地牢大师)

p.MsoNormal { margin-bottom: 10.0000pt; font-family: Tahoma; font-size: 11.0000pt } h1 { margin-top: 5.0000pt; margin-bottom: 5.0000pt; text-align: left; font-family: 宋体; font-weight: bold; font-size: 24.0000pt } span.10 { font-family: "Times New Rom

POJ Multiple (BFS,同余定理)

http://poj.org/problem?id=1465 Multiple Time Limit: 1000MS   Memory Limit: 32768K Total Submissions: 6164   Accepted: 1339 Description a program that, given a natural number N between 0 and 4999 (inclusively), and M distinct decimal digits X1,X2..XM

poj 2243 bfs 利用 结构体中的step成员保存步数 ,STL的队列

//BFS #include <iostream> #include <queue> using namespace std; bool used[8][8]; int move[8][2]={1,2, -1,2, -2,1, -2,-1, -1,-2, 1,-2, 2,-1, 2,1}; struct position { int i,j; int step; position(int a,int b,int c) { i=a; j=b; step=c; } }; int mai

poj 1915 BFS 利用 pre 计算步数------------------路径

// BFS #include <stdio.h> #include <string.h> int visited[301][301]; // visited 已经访问过了 int dic[8][2]={{2,1},{1,2},{-1,2},{-2,1},{-2,-1},{-1,-2},{1,-2},{2,-1}}; int head,end,n,ex,ey,sx,sy; struct quene { int x,y,pre; // pre 前一个结点 }q[100000]; vo