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)\n");
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)\n", 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)\n");
}

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)\n", s[x][y].x, s[x][y].y);
}

poj3984《迷宫问题》暑假集训-搜索进阶的更多相关文章

  1. HDU2612 -暑假集训-搜索进阶N

     http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82828#problem/N这两天总是因为一些小错误耽误时间,我希望自己可以细心点.珍惜 ...

  2. POJ-3126 暑假集训-搜索进阶F题

     http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82828#problem/F 经验就是要认真细心,要深刻理解.num #include& ...

  3. Oil Deposits -----HDU1241暑假集训-搜索进阶

    L - Oil Deposits Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:32768KB   ...

  4. 2015UESTC 暑假集训总结

    day1: 考微观经济学去了…… day2: 一开始就看了看一道题目最短的B题,拍了半小时交了上去wa了 感觉自己一定是自己想错了,于是去拍大家都过的A题,十分钟拍完交上去就A了 然后B题写了一发暴力 ...

  5. 【算法系列学习三】[kuangbin带你飞]专题二 搜索进阶 之 A-Eight 反向bfs打表和康拓展开

    [kuangbin带你飞]专题二 搜索进阶 之 A-Eight 这是一道经典的八数码问题.首先,简单介绍一下八数码问题: 八数码问题也称为九宫问题.在3×3的棋盘,摆有八个棋子,每个棋子上标有1至8的 ...

  6. STL 入门 (17 暑假集训第一周)

    快速全排列的函数 头文件<algorithm> next_permutation(a,a+n) ---------------------------------------------- ...

  7. 暑假集训Day2 互不侵犯(状压dp)

    这又是个状压dp (大型自闭现场) 题目大意: 在N*N的棋盘里面放K个国王,使他们互不攻击,共有多少种摆放方案.国王能攻击到它上下左右,以及左上左下右上右下八个方向上附近的各一个格子,共8个格子. ...

  8. 暑假集训Day1 整数划分

    题目大意: 如何把一个正整数N(N长度<20)划分为M(M>=1)个部分,使这M个部分的乘积最大.N.M从键盘输入,输出最大值及一种划分方式. 输入格式: 第一行一个正整数T(T<= ...

  9. 暑假集训(1)第八弹 -----简单迷宫(Poj3984)

    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, ...

随机推荐

  1. Android 使用SwipeBackLayout实现滑动返回上一级页面——实战来袭

    我们知道.APP在设计上习惯性的把返回button放在屏幕的左上角,那么,在非常多时候(尤其是大屏幕手机),操作改返回button,就会有诸多不便了.为了更加方便实现"返回"功能. ...

  2. 如何去掉MapReduce输出的默认分隔符

    我们在用MapReduce做数据处理的时候,经常会遇到将只需要输出键或者值的情况,如context.write(new Text(record), new Text("")),这样 ...

  3. windows 配置squid反向代理服务器

    发现Window版本的Squid 和 Linux 配置有点不一样 一.配置squid\etc目录1.squid.conf.default 拷贝一份重新命名为squid.conf2.cachemgr.c ...

  4. Android实现一键获取课程成绩dome

    欢迎转载但请标明出处:http://blog.csdn.net/android_for_james/article/details/50984493 两周废寝忘食的创作最终成功了,如今拿出来分享一下. ...

  5. swift - 全屏pop手势

    UINavigationController系统自带有侧滑手势,但是这个手势第一点只能边缘侧滑才可以有效,第二点当手动隐藏系统的导航时,这个手势就不能生效了 为了能到达到全屏pop的效果这里有2中解决 ...

  6. android Webview 实现js调用java代码实现Activity跳转

    今天有了一个需求,在android里webview加载的html页面,要求点击html页面的按钮实现Activity的跳转. 咱是是菜鸟,webview的接触不多,于是就和度娘来了次亲密接触.在其中也 ...

  7. bzoj3531【SDOI2014】旅行

    3531: [Sdoi2014]旅行 Time Limit: 20 Sec  Memory Limit: 512 MB Submit: 850  Solved: 433 [Submit][Status ...

  8. jquery将form表单序列化常json

    var formData = {};$.each(form.serializeArray(),function(i, item){ formData[item.name] = item.value;} ...

  9. 在Mac上为自己手动编译安装一套PHP7的开发环境

    首先你得去官网下载php7 beta1的版本 这里由于我是在mac上安装,所以就去下载linux相关的版本,地址也直接附上了php7 beta1windows版的官方也有发布详情猛戳:这里 解压安装包 ...

  10. oracle 累加功能,累加百分比

    最近做数据分析,需要用到累加功能,发现强大的oracle还真有,用over(order by field) 例子: 数据表中最后一列就是累加的效果 累加sql: select t.acc_pedal_ ...