小鼠迷宫问题【sdut1157】【dfs,bfs综合题目】
小鼠迷宫问题
Time Limit: 1500ms Memory limit: 65536K 有疑问?点这里^_^
题目描述
请编程对于给定的小鼠的迷宫,计算小鼠a通向小鼠b的所有最短道路。
输入
每组数据的第一行有3个正整数n,m,k,分别表示迷宫的行数,列数和封闭的房间数。
接下来的k行中,每行2个正整数,表示被封闭的房间所在的行号和列号。
最后的2行,每行也有2个正整数,分别表示小鼠a所处的方格(p,q)和小鼠b所处的方格(r,s)。
输出
每组数据输出两行,第一行是最短路长度;第2行是不同的最短路数。
每组输出之间没有空行。
如果小鼠a无法通向小鼠b则输出“No Solution!”。
示例输入
8 8 3
3 3
4 5
6 6
2 1
7 7
示例输出
11
96
来源
代码1:超时
这个代码只用dfs,这可以求出所有到达终点的路径,当然也包括最短路径,用存储起来的话,第一个不是0的数组元素就是所求,其中,下标是最短路径,值是方法数,但是由于求出了所有路径和其对应的方法数,而除了最短路径之外剩下的方法都是没有必要求出来的,所以超时了,必须先用bfs求出最短路,优化dfs代码。
#include<iostream>
#include<string.h>
#include<string>
#include<stdlib.h>
using namespace std;
int m,n,k,visited[][],mapx[][];
int sx,sy,ex,ey,sum,hx[];
void dfs(int ,int );
int main()
{
while(cin>>m>>n>>k)
{
memset(mapx,,sizeof(mapx));
memset(visited,,sizeof(visited));
memset(hx,,sizeof(hx));
sum=;
int i,u,v,j;
for(i=; i<=k; i++)
{
cin>>u>>v;
mapx[v][u]=;
}
cin>>sy>>sx>>ey>>ex;
dfs(sx,sy);
for(i=; i<=; i++)
if(hx[i]!=)
{
cout<<i<<endl;
cout<<hx[i]<<endl;
break;
}
if(i==)
cout<<"No Solution!"<<endl;
}
return ;
}
int h[]= {,-,,},z[]= {-,,,};
void dfs(int x,int y)
{
visited[x][y]=;
if(x==ex&&ey==y)
{
hx[sum]++;
}
sum++;
int heng,zong;
int i;
for(i=; i<=; i++)
{
heng=x+h[i];
zong=y+z[i];
if(heng<=||zong<=||heng>m||zong>n)
continue;
else
{
if(visited[heng][zong]==&&mapx[heng][zong]!=)
{
dfs(heng,zong);
}
}
}
visited[x][y]=;
sum--;
}
代码2:ac
#include<iostream>
#include<string.h>
#include<string>
#include<stdlib.h>
#include<queue>
#include<math.h>
using namespace std;
struct vode
{
int x,y,step;
};
int m,n,k,visited[][],mapx[][];
int sx,sy,ex,ey,hx[],zong,jishu,sum;
void dfs(int ,int ,int);
int bfs();
int main()
{
while(cin>>m>>n>>k)
{
memset(mapx,,sizeof(mapx));
memset(visited,,sizeof(visited));
memset(hx,,sizeof(hx));
zong=;
jishu=;
sum=;
int i,u,v;
for(i=; i<=k; i++)
{
cin>>u>>v;
mapx[v][u]=;
}
cin>>sy>>sx>>ey>>ex;
zong=bfs();
if(zong==-)
{
cout<<"No sulution!"<<endl;
continue;
}
else
{
memset(visited,,sizeof(visited));
dfs(sx,sy,sum);
cout<<zong<<endl;
cout<<jishu<<endl;
}
}
return ;
}
int h[]= {,-,,},z[]= {-,,,};
int bfs()
{
int i,heng,zong;
queue<struct vode>que;
struct vode q;
q.x=sx;
q.y=sy;
q.step=;
que.push(q);
int flag=;
while(!que.empty())
{
q=que.front();
for(i=; i<=; i++)
{
heng=q.x+h[i];
zong=q.y+z[i];
if(heng<=||zong<=||heng>ex||zong>ey)
continue;
else if(visited[heng][zong]==&&mapx[heng][zong]==)
{
visited[heng][zong]=;
if(heng==ex&&zong==ey)
{
flag=;
break;
}
else
{
struct vode p;
p.x=heng;
p.y=zong;
p.step=q.step+;
que.push(p);
}
}
}
if(flag==)
break;
que.pop();
}
if(que.empty()&&flag==)return -;
else return que.front().step;
}
void dfs(int x,int y,int sum)
{
if(x==ex&&y==ey&&sum==zong)
{
jishu++;
return ;
}
if(fabs(x-ex)+fabs(y-ey)+sum>zong)return ;
int i,heng,zong;
for(i=; i<=; i++)
{
heng=x+h[i];
zong=y+z[i];
if(heng>m||zong>n||heng<=||zong<=)
continue;
else
{
if(visited[heng][zong]==&&mapx[heng][zong]==)
{
visited[heng][zong]=;
dfs(heng,zong,sum+);
visited[heng][zong]=;
}
}
}
}
小鼠迷宫问题【sdut1157】【dfs,bfs综合题目】的更多相关文章
- ytu 1980:小鼠迷宫问题(DFS 深度优先搜索)
小鼠迷宫问题 Time Limit: 2 Sec Memory Limit: 64 MB Submit: 1 Solved: 1 [Submit][Status][Web Board] Desc ...
- 【DFS/BFS】NYOJ-58-最少步数(迷宫最短路径问题)
[题目链接:NYOJ-58] 经典的搜索问题,想必这题用广搜的会比较多,所以我首先使的也是广搜,但其实深搜同样也是可以的. 不考虑剪枝的话,两种方法实践消耗相同,但是深搜相比广搜内存低一点. 我想,因 ...
- FOJ1205 小鼠迷宫问题 (BFD+递推)
FOJ1205 小鼠迷宫问题 (BFD+递推) 小鼠a与小鼠b身处一个m×n的迷宫中,如图所示.每一个方格表示迷宫中的一个房间.这m×n个房间中有一些房间是封闭的,不允许任何人进入.在迷宫中任何位置均 ...
- DFS/BFS+思维 HDOJ 5325 Crazy Bobo
题目传送门 /* 题意:给一个树,节点上有权值,问最多能找出多少个点满足在树上是连通的并且按照权值排序后相邻的点 在树上的路径权值都小于这两个点 DFS/BFS+思维:按照权值的大小,从小的到大的连有 ...
- ID(dfs+bfs)-hdu-4127-Flood-it!
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4127 题目意思: 给n*n的方格,每个格子有一种颜色(0~5),每次可以选择一种颜色,使得和左上角相 ...
- [LeetCode] 130. Surrounded Regions_Medium tag: DFS/BFS
Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'. A reg ...
- HDU 4771 (DFS+BFS)
Problem Description Harry Potter has some precious. For example, his invisible robe, his wand and hi ...
- POJ 3083 -- Children of the Candy Corn(DFS+BFS)TLE
POJ 3083 -- Children of the Candy Corn(DFS+BFS) 题意: 给定一个迷宫,S是起点,E是终点,#是墙不可走,.可以走 1)先输出左转优先时,从S到E的步数 ...
- [LeetCode]695. 岛屿的最大面积(DFS/BFS)、200. 岛屿数量(DFS/BFS待做/并差集待做)
695. 岛屿的最大面积 题目 给定一个包含了一些 0 和 1的非空二维数组 grid , 一个 岛屿 是由四个方向 (水平或垂直) 的 1 (代表土地) 构成的组合.你可以假设二维矩阵的四个边缘都被 ...
随机推荐
- jdk 1.7 在ubuntu 环境配置
在/opt/里解压了jdk 1.7后 设置环境变量 chen@caicai ~ $ vim .profile export JAVA_HOME=/opt/jdk1..0_79 export JRE_H ...
- LED notification in Android device
Code can control the LED notification in Android device, using android.app.Notification: 1 2 3 4 5 6 ...
- win7和ubuntu双系统删除ubuntu的方法
双系统,一般是先安装win7,再装ubuntu,开机用grub引导.假如装完双系统,某一天又想恢复使用windows怎么办呢? 也许你会说,直接用win7的磁盘管理工具,格式化ubuntu所在磁盘不就 ...
- phpcms模块开发简易教程
简介: 在phpcms中,各个功能是以模块为单位定义的(对应modules目录),如果需要新增功能最好的办法就是开发一个模块,然后复制到phpcms目录下,然后进入后台安装即可. 官方说明: phpc ...
- sed使用的并不是完全的正则表达式
经过实验发现,命令sed 's/pattern/replacement/' file中,pattern使用的并不是完全的正则表达式,而如果想使用正则表达式,需要使用sed命令的 -r 选项: sed ...
- 多节点 devstack 部署
1, 网络配置 每个节点 /etc/network/interfaces auto eth0 iface eth0 inet static address 192.168.42.11 netmask ...
- chrome调试命令模式
哈哈哈
- XsltListViewWebPart 和自定义列表视图
http://msdn.microsoft.com/zh-cn/library/ff806162(v=office.14).aspx
- 【转】Nginx服务器的反向代理proxy_pass配置方法讲解
[转]Nginx服务器的反向代理proxy_pass配置方法讲解 转自:http://www.jb51.net/article/78746.htm 就普通的反向代理来讲Nginx的配置还是比较简单的, ...
- 使用jsvc启动tomcat
1.在/usr/local/apache-tomcat-7.0.68/bin中有commons-daemon-native.tar.gz 压缩包 2.解压commons-daemon-native. ...