广搜 广搜 poj 3984
***求最短路径,然后再输出路径,
BFS+路径输出***
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <stack>
#include <queue>
#include <cmath> using namespace std;
typedef long long LL;
#define oo 0x3f3f3f3f
#define N 100 int maps[10][10];
int dir[4][2]= {{1,0}, {0,1}, {-1,0}, {0,-1}}, vis[N][N], step[N][N]; struct node
{
int x, y, s; } a, b; int k; void BFS(int x, int y)
{
k=0;
queue<node> q;
a.x=x;
a.y=y;
a.s=0;
q.push(a);
vis[0][0]=1;
step[0][0]=0;
k++; while(!q.empty())
{
a=q.front();
q.pop();
for(int i=0; i<4; i++)
{
b.x=a.x+dir[i][0];
b.y=a.y+dir[i][1];
if(b.x>=0&&b.x<5&&b.y>=0&&b.y<5&&maps[b.x][b.y]==0&&!vis[b.x][b.y])
{
b.s=a.s+1;
q.push(b);
step[b.x][b.y]=b.s;
vis[b.x][b.y]=1;
}
}
}
} void put(int x, int y)
{
if(x==0 && y==0)
{
printf("(%d, %d)\n", x, y);
return ;
}
for(int i=0; i<4; i++)
{
int nx=x+dir[i][0];
int ny=y+dir[i][1];
if(nx>=0 && nx<5 && ny>=0 && ny<5 && step[x][y]==step[nx][ny]+1 && vis[nx][ny])
{
put(nx, ny);
printf("(%d, %d)\n", x, y);
}
}
} int main()
{
for(int i=0; i<5; i++)
for(int j=0; j<5; j++)
scanf("%d", &maps[i][j]);
memset(vis, 0, sizeof(vis));
memset(step, 0, sizeof(step));
BFS(0, 0);
put(4, 4);
return 0;
}
广搜 广搜 poj 3984的更多相关文章
- POJ 3984 迷宫问题
K - 迷宫问题 Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Sta ...
- Q - 迷宫问题 POJ - 3984(BFS / DFS + 记录路径)
Q - 迷宫问题 POJ - 3984 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, ...
- BZOJ_1615_[Usaco2008_Mar]_The Loathesome_Hay Baler_麻烦的干草打包机_(模拟+宽搜/深搜)
描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1615 一个主动轮带着一些轮子转,轮子带着轮子转,轮子带着轮子转...一个非主动轮只会被一个轮子 ...
- POJ 3984(DFS入门题 +stack储存路径)
POJ 3984 Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, ...
- BFS(最短路+路径打印) POJ 3984 迷宫问题
题目传送门 /* BFS:额,这题的数据范围太小了.但是重点是最短路的求法和输出路径的写法. dir数组记录是当前点的上一个点是从哪个方向过来的,搜索+,那么回溯- */ /************* ...
- POJ 3984 迷宫问题 记录路径的广搜
主要是学一下如何在广搜中记录路径:每找到一个点我就记录下这个点是由那个点得来的,这样我找到最后个点后,我就可以通过回溯得到我走过的路径,具体看代码吧~ #include<cstdio> # ...
- poj 3984:迷宫问题(广搜,入门题)
迷宫问题 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7635 Accepted: 4474 Description ...
- 广搜+打表 POJ 1426 Find The Multiple
POJ 1426 Find The Multiple Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 25734 Ac ...
- PIGS POJ - 1149网络流(最短增广路---广搜) + 建图
题意: 第一行输入m和n,m是猪圈的数量,n是顾客的数量,下面n行 第 i+1行表示第i个顾客 , 输入第一个数字表示有几把猪圈的钥匙,后面输入对应的猪圈,最后一个数字输入顾客想买几头猪. 建图: 设 ...
- 广搜+输出路径 POJ 3414 Pots
POJ 3414 Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13547 Accepted: 5718 ...
随机推荐
- vue-project-------(模板语法,属性绑定)
<template> <h3>模板语法</h3> <p>{{msg}}</p> <p>{{number+1}}</p> ...
- MySQL运维11-Mycat分库分表之应用指定分片
一.应用指定分片 此规则是在运行阶段有应用自主决定路由到那个分片,根据提供的字段,然后按照指定的规则,截取该字段的部分子字符串当做分片的依据,该分别方法比较灵活,适用于某个字段有几个特殊的字符串拼接而 ...
- Educational Codeforces Round 26 Problem A
A. Text Volume time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- 新手友好、轻量级的C#/.NET万能工具库
前言 今天分享一个基于MIT License协议开源.免费.新手友好.轻量级的C#/.NET万能工具库.帮助类库(支持.NET和.NET Core,可以帮助开发者们减少常见重复功能方法查找,提高开发工 ...
- Python subprocess 使用(一)
Python subprocess 使用(一) 本文主要讲下 subprocess 的简单使用. 1: 通过subprocess 获取设备信息 import subprocess def get_an ...
- 踩坑ffmpeg录制的mp4无法在浏览器上播放
前言 使用ffmpeg编译好的程序在电脑上进行音视频转换,可以参考这篇:<windows电脑FFmpeg安装教程手把手详解_windows安装ffmpeg>,而我们要做的是在游戏引擎中集成 ...
- mysql将查询结果生成临时表
MySQL中将查询的结果生成临时表,列类型与查询的列一致,百度搜索到的没啥用. 直接上SQL: 将结果生成临时表 create temporary table temp_tb_name as (sel ...
- mybatis空格字符替换
mybatis空格字符替换 <select id="user" resultType="java.util.Map" parameterType=&quo ...
- flutter中去除导航栏与状态栏
方法一 SystemChrome.setEnabledSystemUIMode(SystemUiMode.manual, overlays: [SystemUiOverlay.bottom]); // ...
- 9、线性布局(Row和Column)
自定义的IconContainer void main() { runApp(MaterialApp( theme: ThemeData(primarySwatch: Colors.yellow), ...