zoukankan      html  css  js  c++  java
  • POJ 3984 迷宫问题

    迷宫问题
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 16724   Accepted: 9981

    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)

    bfs+搜索记录
     1 #include<iostream>
     2 #include<cstring>
     3 #include<queue>
     4 #include<algorithm>
     5 using namespace std;
     6 
     7 struct node
     8 {
     9     int x,y,w;
    10     int fx[25],fy[25];
    11     int n;
    12 };
    13 
    14 int maze[5][5];
    15 int xx[4]={-1,1,0,0},yy[4]={0,0,1,-1};
    16 
    17 int main()
    18 {
    19     for(int i=0;i<5;i++)
    20         for(int j=0;j<5;j++)
    21             cin>>maze[i][j];
    22     queue<node> Q;
    23     node q;
    24     q.x=q.y=q.w=q.n=0;
    25     memset(q.fx,0,sizeof(int)*25);memset(q.fy,0,sizeof(int)*25);
    26     Q.push(q);
    27     while(!Q.empty())
    28     {
    29         node t=Q.front();Q.pop();t.n++;
    30         t.fx[t.n]=t.x;t.fy[t.n]=t.y;
    31         if(t.x==4&&t.y==4) {q=t;break;}
    32         for(int i=0;i<4;i++)
    33         {
    34             if(t.x+xx[i]<0||t.x+xx[i]>4||t.y+yy[i]<0||t.y+yy[i]>4||maze[t.x+xx[i]][t.y+yy[i]]==1) continue;
    35             node p=t;
    36             p.x+=xx[i];p.y+=yy[i];p.w++;
    37             Q.push(p);
    38         }
    39     }
    40     for(int i=1;i<=q.n;i++)
    41         printf("(%d, %d)
    ",q.fx[i],q.fy[i]);
    42     return 0;
    43 }
  • 相关阅读:
    VS2008编写MFC程序--使用opencv2.4()
    November 02nd, 2017 Week 44th Thursday
    November 01st, 2017 Week 44th Wednesday
    October 31st, 2017 Week 44th Tuesday
    October 30th, 2017 Week 44th Monday
    October 29th, 2017 Week 44th Sunday
    October 28th, 2017 Week 43rd Saturday
    October 27th, 2017 Week 43rd Friday
    October 26th, 2017 Week 43rd Thursday
    October 25th, 2017 Week 43rd Wednesday
  • 原文地址:https://www.cnblogs.com/InWILL/p/5982429.html
Copyright © 2011-2022 走看看