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

    迷宫问题
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 10967   Accepted: 6547

    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,重点在于路径记录.

    标记上一个节点,找到解的时候递归调用输出路径.

    /*************************************************************************
        > File Name: code/2015summer/searching/KK.cpp
        > Author: 111qqz
        > Email: rkz2013@126.com 
        > Created Time: 2015年07月25日 星期六 13时33分00秒
     ************************************************************************/
    
    #include<iostream>
    #include<iomanip>
    #include<cstdio>
    #include<algorithm>
    #include<cmath>
    #include<cstring>
    #include<string>
    #include<map>
    #include<set>
    #include<queue>
    #include<vector>
    #include<stack>
    #define y0 abc111qqz
    #define y1 hust111qqz
    #define yn hez111qqz
    #define j1 cute111qqz
    #define tm crazy111qqz
    #define lr dying111qqz
    using namespace std;
    #define REP(i, n) for (int i=0;i<int(n);++i)  
    typedef long long LL;
    typedef unsigned long long ULL;
    int a[10][10];
    int head = 0;
    int tail = 1;
    int dirx[2]={1,0};
    int diry[2]={0,1};
    struct node
    {
        int x,y,pre;
    }q[10];
    
    void print(int x)
    {
        if (q[x].pre!=-1)
        {
          print(q[x].pre);
          printf("(%d, %d)
    ",q[x].x,q[x].y);
        }
    }
    void bfs()
    {
        q[head].x=0;
        q[head].y=0;
        q[head].pre=-1;
        while (head<tail)
        {
          if (q[head].x==4&&q[head].y==4) 
          {
            print(head);
            return;
          }
          for (int i = 0 ; i < 2 ; i++ )
          {
            int newx=dirx[i]+q[head].x;
            int newy=diry[i]+q[head].y;
            if (newx>=0&&newx<5&&newy>=0&&newy<5&&a[newx][newy]==0)
            {
                q[tail].x=newx;
                q[tail].y=newy;
                q[tail].pre=head;
                tail++;
            }
          }
          head++;
        }
    
    }
    int main()
    {
        for ( int i = 0 ; i < 5 ; i++ )
        {
          for ( int j = 0 ; j < 5;  j++)
          {
            cin>>a[i][j];
          }
        }
        printf("(0, 0)
    ");
        bfs();
      
        return 0;
    }
  • 相关阅读:
    DOM
    Event
    响应式,多列布局
    理解HTML语义化
    类加载过程
    反射
    注解
    线程池
    管程法
    Lock锁
  • 原文地址:https://www.cnblogs.com/111qqz/p/4675809.html
Copyright © 2011-2022 走看看