poj3984《迷宫问题》暑假集训-搜索进阶
Crawling in process... Crawling failed Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
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
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<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《迷宫问题》暑假集训-搜索进阶的更多相关文章
- HDU2612 -暑假集训-搜索进阶N
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82828#problem/N这两天总是因为一些小错误耽误时间,我希望自己可以细心点.珍惜 ...
- POJ-3126 暑假集训-搜索进阶F题
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82828#problem/F 经验就是要认真细心,要深刻理解.num #include& ...
- Oil Deposits -----HDU1241暑假集训-搜索进阶
L - Oil Deposits Crawling in process... Crawling failed Time Limit:1000MS Memory Limit:32768KB ...
- 2015UESTC 暑假集训总结
day1: 考微观经济学去了…… day2: 一开始就看了看一道题目最短的B题,拍了半小时交了上去wa了 感觉自己一定是自己想错了,于是去拍大家都过的A题,十分钟拍完交上去就A了 然后B题写了一发暴力 ...
- 【算法系列学习三】[kuangbin带你飞]专题二 搜索进阶 之 A-Eight 反向bfs打表和康拓展开
[kuangbin带你飞]专题二 搜索进阶 之 A-Eight 这是一道经典的八数码问题.首先,简单介绍一下八数码问题: 八数码问题也称为九宫问题.在3×3的棋盘,摆有八个棋子,每个棋子上标有1至8的 ...
- STL 入门 (17 暑假集训第一周)
快速全排列的函数 头文件<algorithm> next_permutation(a,a+n) ---------------------------------------------- ...
- 暑假集训Day2 互不侵犯(状压dp)
这又是个状压dp (大型自闭现场) 题目大意: 在N*N的棋盘里面放K个国王,使他们互不攻击,共有多少种摆放方案.国王能攻击到它上下左右,以及左上左下右上右下八个方向上附近的各一个格子,共8个格子. ...
- 暑假集训Day1 整数划分
题目大意: 如何把一个正整数N(N长度<20)划分为M(M>=1)个部分,使这M个部分的乘积最大.N.M从键盘输入,输出最大值及一种划分方式. 输入格式: 第一行一个正整数T(T<= ...
- 暑假集训(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, ...
随机推荐
- nightwatchjs --Expect element to not include text
to have NightWatch to find text in a div. browser.assert.containsText('#output', 'find me') But how ...
- Netbeans打开包括中文文件时提示错误
Netbeans打开包括中文文件时提示错误.在Netbeans里找了半天没找到怎么设置,最后发现要改动Netbeans的配置文件才干解决. 编辑C:\Program Files\NetBeans 8. ...
- 使用 yarn 作为 Npm 的代替方案
相关传送门: # window 安装包下载https://yarnpkg.com/zh-Hans/docs/install#windows-stable # yarn官方网站 https://yarn ...
- thinkphp5.0极速搭建restful风格接口层实例
作为国内最流行的php框架thinkphp,很快就会发布v5.0正式版了,现在还是rc4版本,但已经很强大了下面是基于ThinkPHP V5.0 RC4框架,以restful风格完成的新闻查询(get ...
- asp.net core 系列之Response caching(1)
这篇文章简单的讲解了response caching: 讲解了cache-control,及对其中的头和值的作用,及设置来控制response caching; 简单的罗列了其他的缓存技术:In-me ...
- 卸载Linux自带openjdk
1.查看自带jdk版本 java -version 2.查看 rpm -qa | grep java 显示如下信息: java-1.4.2-gcj-compat-1.4.2.0-40jpp. ...
- 2013 年最好的 20 款免费 jQuery 插件
2013 年最好的 20 款免费 jQuery 插件 oschina 发布于: 2014年01月11日 (8评) 分享到 新浪微博腾讯微博 收藏+99 互联网上面有很多 jQuery 插件,这里我们 ...
- linux应用网址
APUE学习:(十三) [终篇] 一起学 Unix 环境高级编程 (APUE) 之 网络 IPC:套接字 linux应用学习笔记(已列目录供跳转):http://www.cnblogs.com/ggj ...
- mysqldump默认参数add-drop-table
mysqldump默认参数add-drop-table 原创 2014年01月28日 11:35:18 9214 接到一个同事电话,说UAT环境上的一张表被删了,要恢复一下.原来是新项目UAT,从测试 ...
- android的DrawerLayout用法
DrawerLayout的关键点(我认为的)就在于layout文件的layout_gravity属性的值,只有左右,两种选择,不能从上下滑出来,就算有这个效果也不是这个套路弄出来的. <?xml ...