poj3984《迷宫问题》暑假集训-搜索进阶

K - 迷宫问题
Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
Submit Status

Description

定义一个二维数组:

int maze[5][5] = {
0, 1, 0, 0, 0,
0, 1, 0, 1, 0,
0, 0, 0, 0, 0,
0, 1, 1, 1, 0,
0, 0, 0, 1, 0,
};

它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线。

Input

一个5 × 5的二维数组,表示一个迷宫。数据保证有唯一解。

Output

左上角到右下角的最短路径,格式如样例所示。

Sample Input

0 1 0 0 0
0 1 0 1 0
0 0 0 0 0
0 1 1 1 0
0 0 0 1 0

Sample Output

(0, 0)
(1, 0)
(2, 0)
(2, 1)
(2, 2)
(2, 3)
(2, 4)
(3, 4)
(4, 4)
这是我的代码!

#include<iostream>
#include<algorithm>
#include<queue>
#include<string.h>
#include<stdio.h>
using namespace std;
int maps[5][5];
int dir[4][2]= {{1, 0}, {-1, 0}, {0, -1}, {0, 1}};
typedef struct maze
{
int x, y;
} MAZE;
MAZE s, n, p[5][5];//关键是定义了一个二维的结构体数组来存maps[i][j]处的前一个位置;

void Search(int a, int b);
void BFS();

int main()
{
int i, j;
for(i=0; i<5; i++)
for(j=0; j<5; j++)
cin >> maps[i][j];
s.x=0;
s.y=0;
//s=(MAZE){0, 0};
p[0][0]=(MAZE){-1, -1};
printf("(0, 0) ");
BFS();

}
void BFS()
{
queue<MAZE>que;
que.push(s);
while(!que.empty())
{
s=que.front();
que.pop();
for(int i=0; i<4; i++)
{
n.x=s.x+dir[i][0];
n.y=s.y+dir[i][1];
if(n.x<5&&n.x>=0&&n.y<5&&n.y>=0&&maps[n.x][n.y]==0)
{
maps[n.x][n.y]=1;
p[n.x][n.y]=s;
que.push(n);
}
if(n.x==4&&n.y==4)
{
Search(4,4);
return;
}
}
}
}
void Search(int a, int b)
{
if(a==0&&b==0)
return ;

Search(p[a][b].x, p[a][b].y);
printf("(%d, %d) ", a, b);
}

 这是我一个朋友的代码            
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;

#define PI acos-1.0//
#define N 10

const int dir[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};

struct node
{
    int x, y;
}s[N][N], sa;//定义的二维结构体数组;用处是判断

char g[N][N];
int vis[N][N];//防止重复搜索,这是我忘记的一点儿

void BFS (int x, int y);
void DFS (int x, int y);

int main ()
{
    for (int i=0; i<5; i++)
    {
        for (int j=0; j<5; j++)
            cin >> g[i][j];
        getchar ();
    }
    sa = (node) {0, 0};//整体赋值,其实就相当于sa.x=0; sa.y=0;
    s[1][1] = (node) {0, 0};//这里代表
    BFS (0, 0);
    DFS (4, 4);
    printf ("(4, 4) ");
}

void BFS (int x, int y)
{
    memset (vis, 0, sizeof (vis));//vis数组代表是否进行广搜过
    queue <node> que;
    que.push (sa);
    vis[sa.x][sa.y] = 1;

    while (que.size())
    {
        sa = que.front(); que.pop();
        for (int i=0; i<4; i++)
        {
            node q = sa;
            q.x += dir[i][0], q.y += dir[i][1];
            if (q.x>=0 && q.x<5 && q.y>=0 && q.y<5 && !vis[q.x][q.y] && g[q.x][q.y] == '0')
            {
                s[q.x][q.y] = (node) {sa.x, sa.y};//
                vis[q.x][q.y] = 1;
                que.push(q);
            }
        }
    }
}

void DFS (int x, int y)
{
    if (!x && !y)
        return;
    DFS (s[x][y].x, s[x][y].y);
    printf ("(%d, %d) ", s[x][y].x, s[x][y].y);
}
原文地址:https://www.cnblogs.com/wazqWAZQ1/p/4651363.html