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. csv文件中出现乱码的解决方法

    1.首先用UE打开CSV文件,发现没有乱码了. 2.然后新建一个txt文本,把CSV中的数据复制到txt文本中,保存格式为ANSI/ASCII. 3.复制txt文件,再把副本后缀改为CSV格式,再用E ...

  2. 转:什么是Node.js?

    Node不是万能药!但的确能解决一些关键问题 学习Node不是一件轻松事儿,但你所收到的回报是对得起你的付出的.因为当下Web应用开发中的诸多难题唯有JavaScript才能解决. 目录 专家们的警告 ...

  3. CIA 读书笔记

    对此书的评价只有八个字:粗制滥造,到处粘贴. 对于通过表情识别人情绪的教程,最好要有图,图很重要,也最好有案例.

  4. 创建一个动态Web项目:

    开始你的Eclipse,然后进入“文件”>“新建”>“动态Web项目,然后输入项目名称为HelloWorldStruts2和设置其他的选项,在下面的屏幕: 选择在屏幕上的所有默认选项,最后 ...

  5. VMWare虚拟机下为Ubuntu 12.04.2配置静态IP(NAT方式)

    http://www.cnblogs.com/objectorl/archive/2012/09/27/vmware-ubuntu-nat-static-ip-settings.html 参考以上方式 ...

  6. 使用java命令运行class文件带包名时出错

    会出现classnotfound的错误. 如:文件x:/Test.java; package xx public class Test{} javac 编译后,java命令出错,需要手动构建包目录xx ...

  7. android 自定义 listView

    目录: 1.主布局 ListView <?xml version="1.0" encoding="utf-8"?><RelativeLayou ...

  8. ANDROID版本号号和版本号名称的重要性介绍

    转载请注明出处http://blog.csdn.net/y150481863/article/details/41249159,来自[http://blog.csdn.net/y150481863] ...

  9. Map集合按value的大小排序

    public static void main(String[] args) { Map<String, Integer> map = new HashMap<String, Int ...

  10. SpringBoot中的配置文件

    http://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html