首页 > 其他 > 详细

poj 3984 迷宫问题

时间:2014-10-12 18:50:49      阅读:266      评论:0      收藏:0      [点我收藏+]

题目链接:http://poj.org/problem?id=3984

 

思路:

    经典型的DFS题目。搜索时注意剪枝:越界处理,不能访问处理。

代码:

#include <iostream>
using namespace std;

const int MAX_N = 15;
int map[MAX_N][MAX_N];
typedef struct Node
{
    int x;
    int y;
}Road;
Road vis[MAX_N];

int SearchRoad( int i, int j, int count )
{
    if ( i == 4 && j == 4 )
    {
        vis[count].x = i;
        vis[count].y = j; 
        for ( int i = 0; i <= count; ++i )
            printf( "(%d, %d)\n", vis[i].x, vis[i].y );
    }
    else
    if ( i >= 5 || j >= 5 || map[i][j] == 1 )
        return 0;
    else
    {
        vis[count].x = i;
        vis[count].y = j;

        count++;
        SearchRoad( i+1, j, count );
        SearchRoad( i, j+1, count );
    }

    return 0;
}


int main()
{
    for ( int i = 0; i < 5; ++i )
        for ( int j = 0; j < 5; ++j )
            scanf( "%d", &map[i][j] );

    SearchRoad( 0, 0, 0 );
    return 0;
}

 

poj 3984 迷宫问题

原文:http://www.cnblogs.com/tallisHe/p/4020866.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!