码迷,mamicode.com
首页 > 其他好文 > 详细

Poj3984--迷宫问题(BFS)

时间:2015-08-05 22:03:23      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:

迷宫问题

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 10056   Accepted Submission(s) : 5652
Problem 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)
 

 

Source
PKU
//应该是建邻接表,但我没学会, 话说一组数据的题真的好吗?
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;

const int MAX = 10;
struct point
{
    int x,y,pre;    
}q[MAX];
int front = 0, rear = 1, sx, sy, ex, ey;
int arr[MAX][MAX];
int dx[4]={1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
int n, m;
void output(int i)              //回溯输出; 什么鬼? 
{
    if(q[i].pre != -1)
    {
        output(q[i].pre);
        printf("(%d, %d)\n",q[i].x,q[i].y);
    }
}

void bfs(int sx,int sy)
{
    q[front].x = sx;
    q[front].y = sy;
    q[front].pre = -1;
    arr[sx][sy] = 1;
    while(front < rear)     //模拟; 
    {
        for(int i=0;i<4;i++)
        {
            int nx = q[front].x + dx[i];
            int ny = q[front].y + dy[i];
            if(nx<0 || nx>=5 || ny<0 || ny>=5 || arr[nx][ny])
                continue;
            else
            {
                arr[nx][ny] = 1;
                q[rear].x = nx;
                q[rear].y = ny;
              // printf("%d %d\n", q[rear].x, q[rear].y); 
                q[rear++].pre = front;
            }                                                                          
            if(nx == 4 && ny == 4)  output(front);
        }
        front++;
    }
}

int main()
{
    for(int i=0;i<5;i++)
        for(int j=0;j<5;j++)
                scanf("%d",&arr[i][j]);
    printf("(0, 0)\n");
    bfs(0,0);
    printf("(4, 4)\n");
    return 0;
} 

//学长的邻接表:

技术分享
 1 #include<stdio.h>
 2 #include<string.h>
 3 int head[100100],cnt;
 4 struct s
 5 {
 6     int u,v,w;
 7     int next;
 8 }edge[100010];
 9 void add(int u,int v,int w)
10 {
11     edge[cnt].u=u;
12     edge[cnt].v=v;
13     edge[cnt].w=w;
14     edge[cnt].next=head[u];
15     head[u]=cnt++;
16 }
17 int main()
18 {
19     int n;
20     while(scanf("%d",&n)!=EOF)
21     {
22         int i;
23         cnt=0;
24         memset(head,-1,sizeof(head));
25         for(i=0; i<n; i++)
26         {
27             int u,v,w;
28             scanf("%d%d%d",&u,&v,&w);
29             add(u,v,w);
30         }
31         int u;
32         scanf("%d",&u);
33         for(i=head[u]; i!=-1; i=edge[i].next)             
34         {
35             int v=edge[i].v;
36             int w=edge[i].w;
37         }
38     }
39     return 0;
40 }
View Code

 

Poj3984--迷宫问题(BFS)

标签:

原文地址:http://www.cnblogs.com/fengshun/p/4705780.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!