入门BFS,第一次做,部分借鉴了大牛的

#include <iostream>
#include <cstdio>
#include <queue>
using namespace std; int a[5][5];
bool visit[5][5];
int dx[4]={0,1,0,-1}; // 四个方向:0为右,1为下,2为左,3为上
int dy[4]={1,0,-1,0}; struct Node
{
int x;
int y;
int s; // 路径长度
int direc[30]; //记录方向
}node,next;
bool judge(int x,int y)
{
if(x<0 || x>4 || y<0 || y>4)
return true;
if(visit[x][y]==true || a[x][y]==1)
return true;
visit[x][y]=true; // 访问后标记一下
return false;
}
Node bfs()
{
queue<Node>q;
visit[node.x][node.y]=true;
q.push(node);
while(!q.empty())
{
node=q.front();
if(node.x==4 && node.y==4)
return node;
q.pop();
for(int i=0;i<4;i++) // 判断四个方向
{
next=node;
next.x=node.x+dx[i];
next.y=node.y+dy[i];
if(judge(next.x,next.y))
continue;
next.direc[node.s]=i;
next.s=node.s+1;
q.push(next);
}
}
}
int main()
{
//freopen("in.txt","r",stdin);
for(int i=0;i<5;i++)
for(int j=0;j<5;j++)
scanf("%d",&a[i][j]);
Node ans=bfs();
int x=0,y=0;
printf("(0, 0)\n");
for(int i=0;i<ans.s;i++)
{
x+=dx[ans.direc[i]];
y+=dy[ans.direc[i]];
printf("(%d, %d)\n",x,y);
}
return 0;
}

POJ 3984 迷宫(BFS)的更多相关文章

  1. BFS(最短路+路径打印) POJ 3984 迷宫问题

    题目传送门 /* BFS:额,这题的数据范围太小了.但是重点是最短路的求法和输出路径的写法. dir数组记录是当前点的上一个点是从哪个方向过来的,搜索+,那么回溯- */ /************* ...

  2. POJ 3984 迷宫问题(简单bfs+路径打印)

    传送门: http://poj.org/problem?id=3984 迷宫问题 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions ...

  3. POJ 3984 迷宫问题

    K - 迷宫问题 Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Sta ...

  4. POJ 3984 - 迷宫问题 - [BFS水题]

    题目链接:http://poj.org/problem?id=3984 Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, ...

  5. POJ 3984 迷宫问题 bfs 难度:0

    http://poj.org/problem?id=3984 典型的迷宫问题,记录最快到达某个点的是哪个点即可 #include <cstdio> #include <cstring ...

  6. [POJ 3984] 迷宫问题(BFS最短路径的记录和打印问题)

    题目链接:http://poj.org/problem?id=3984 宽度优先搜索最短路径的记录和打印问题 #include<iostream> #include<queue> ...

  7. POJ - 3984 迷宫问题 【BFS】

    题目链接 http://poj.org/problem?id=3984 思路 因为要找最短路 用BFS 而且 每一次 往下一层搜 要记录当前状态 之前走的步的坐标 最后 找到最短路后 输出坐标就可以了 ...

  8. POJ 3984 迷宫问题(BFS)

    迷宫问题 Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, ...

  9. poj 3984 迷宫问题【bfs+路径记录】

    迷宫问题 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10103   Accepted: 6005 Description ...

随机推荐

  1. 【转载】linux-查询rpm包相关安装、卸载脚本

        测试过程中,有时要测试开发自己打的rpm包,为了确认打包正确,需要查询rpm包相关安装.卸载脚本,可以使用命令:   [root@6 /]#rpm -q --scripts mysql pos ...

  2. Linux用户登录查看命令总结 - w,who,last,lastlog

    Linux用户登录查看命令总结 - w,who,last,lastlog linux shell 747 次阅读  ·  读完需要 15 分钟 0 1. 查看登录用户信息 who -H 命令输出 NA ...

  3. Ansible_描述角色结构

    一.利用角色构造ansible playbook 1.What's 角色 1️⃣:Ansible角色提供了一种方法,让用户能以通用的方式更加轻松地重复利用Ansible代码. 我们可以在标准化目录结构 ...

  4. Scala 关键字

    Java关键字 Java 一共有 50 个关键字(keywords),其中有 2 个是保留字,目前还不曾用到:goto 和 const.true.false 和 null 看起来很像关键字,但实际上只 ...

  5. shell基础之exit,break,continue

    exit代码: 1 #!/bin/bash 2 echo "Is it morning? Please answer yes or no." 3 read YES_OR_NO 4 ...

  6. 校准仪的开发 ---等下整理 迪文屏的ICO文件 和输出配置问题

    要有ICO文件才能

  7. HDFS的小文件问题

    HDFS 中任何一个文件,目录或者数据块在 NameNode 节点内存中均以一个对象形式表示(元数据),而这受到 NameNode 物理内存容量的限制.每个元数据对象约占 150 byte,所以如果有 ...

  8. 大数据学习之路——环境配置(2)——mysql 在linux 系统上安装配置

    1.安装参考网址: https://blog.csdn.net/IronWring_Fly/article/details/103637801   设置新秘密: mysqladmin -u root ...

  9. 贪心算法leetcode-763

    int[] lastShow = new int[26]; var list = new LinkedList<Integer>(); for (int i = 0; i < s.l ...

  10. IDEA 最实用快捷键【MAC版本】

    目录 option + F7 Ctrl + B / Ctrl +鼠标左键(一键两用,可以无限循环的跳过来跳过去,我跳过去了,我又跳回去了) command + E (这个快捷键很有用,为什么我老是用不 ...