小鼠迷宫问题

Time Limit: 1500ms   Memory limit: 65536K  有疑问?点这里^_^

题目描述

小鼠a与小鼠b身处一个m×n的迷宫中,如图所示。每一个方格表示迷宫中的一个房间。这m×n个房间中有一些房间是封闭的,不允许任何人进入。在迷宫中任何位置均可沿上,下,左,右4个方向进入未封闭的房间。小鼠a位于迷宫的(p,q)方格中,它必须找出一条通向小鼠b所在的(r,s)方格的路。请帮助小鼠a找出所有通向小鼠b的最短道路。

请编程对于给定的小鼠的迷宫,计算小鼠a通向小鼠b的所有最短道路。

输入

本题有多组输入数据,你必须处理到EOF为止。
每组数据的第一行有3个正整数n,m,k,分别表示迷宫的行数,列数和封闭的房间数。
接下来的k行中,每行2个正整数,表示被封闭的房间所在的行号和列号。
最后的2行,每行也有2个正整数,分别表示小鼠a所处的方格(p,q)和小鼠b所处的方格(r,s)。

输出

对于每组数据,将计算出的小鼠a通向小鼠b的最短路长度和有多少条不同的最短路输出。
每组数据输出两行,第一行是最短路长度;第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综合题目】的更多相关文章

  1. ytu 1980:小鼠迷宫问题(DFS 深度优先搜索)

     小鼠迷宫问题 Time Limit: 2 Sec  Memory Limit: 64 MB Submit: 1  Solved: 1 [Submit][Status][Web Board] Desc ...

  2. 【DFS/BFS】NYOJ-58-最少步数(迷宫最短路径问题)

    [题目链接:NYOJ-58] 经典的搜索问题,想必这题用广搜的会比较多,所以我首先使的也是广搜,但其实深搜同样也是可以的. 不考虑剪枝的话,两种方法实践消耗相同,但是深搜相比广搜内存低一点. 我想,因 ...

  3. FOJ1205 小鼠迷宫问题 (BFD+递推)

    FOJ1205 小鼠迷宫问题 (BFD+递推) 小鼠a与小鼠b身处一个m×n的迷宫中,如图所示.每一个方格表示迷宫中的一个房间.这m×n个房间中有一些房间是封闭的,不允许任何人进入.在迷宫中任何位置均 ...

  4. DFS/BFS+思维 HDOJ 5325 Crazy Bobo

    题目传送门 /* 题意:给一个树,节点上有权值,问最多能找出多少个点满足在树上是连通的并且按照权值排序后相邻的点 在树上的路径权值都小于这两个点 DFS/BFS+思维:按照权值的大小,从小的到大的连有 ...

  5. ID(dfs+bfs)-hdu-4127-Flood-it!

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4127 题目意思: 给n*n的方格,每个格子有一种颜色(0~5),每次可以选择一种颜色,使得和左上角相 ...

  6. [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 ...

  7. HDU 4771 (DFS+BFS)

    Problem Description Harry Potter has some precious. For example, his invisible robe, his wand and hi ...

  8. POJ 3083 -- Children of the Candy Corn(DFS+BFS)TLE

    POJ 3083 -- Children of the Candy Corn(DFS+BFS) 题意: 给定一个迷宫,S是起点,E是终点,#是墙不可走,.可以走 1)先输出左转优先时,从S到E的步数 ...

  9. [LeetCode]695. 岛屿的最大面积(DFS/BFS)、200. 岛屿数量(DFS/BFS待做/并差集待做)

    695. 岛屿的最大面积 题目 给定一个包含了一些 0 和 1的非空二维数组 grid , 一个 岛屿 是由四个方向 (水平或垂直) 的 1 (代表土地) 构成的组合.你可以假设二维矩阵的四个边缘都被 ...

随机推荐

  1. ZUI前段框架简介

    一.说明 基于Bootstrap定制 ZUI继承了Bootstrap 3中的大部分基础内容,但出于与Bootstrap不同的目的,一些组件都进行了定制和修改.这些变化包括: 移除了部分插件的限制,增加 ...

  2. Python自动化之常用模块

    1 time和datetime模块 #_*_coding:utf-8_*_ __author__ = 'Alex Li' import time # print(time.clock()) #返回处理 ...

  3. thinkphp的各种内部函数 D()、F()、S()、C()、L()、A()、I()

    D() 加载Model类 M() 加载Model类 A() 加载Action类 L() 获取语言定义 C() 获取配置值    用法就是   C("这里填写在配置文件里数组的下标" ...

  4. non

    I p(I q){r p(c((q>9?q-p(q/10):q)+'0')),q*10; }

  5. VQ结合SVM分类方法

    今天整理资料时,发现了在学校时做的这个实验,当时整个过程过重偏向依赖分类器方面,而又很难对分类器性能进行一定程度的改良,所以最后没有选用这个方案,估计以后也不会接触这类机器学习的东西了,希望它对刚入门 ...

  6. linux下logrotate 配置和理解

    对于Linux 的系统安全来说,日志文件是极其重要的工具.系统管理员可以使用logrotate 程序用来管理系统中的最新的事件,对于Linux 的系统安全来说,日志文件是极其重要的工具.系统管理员可以 ...

  7. Longest Increasing Path in a Matrix

    Given an integer matrix, find the length of the longest increasing path. From each cell, you can eit ...

  8. trap

    http://blog.csdn.net/elbort/article/details/8525599 http://mywiki.wooledge.org/SignalTrap

  9. swfit中的同步锁

    swfit 中 objective-c 中的@syncronized 这个东西不能用了,应该用 objc_sync_enter(self) 代码 objc_sync_exit(self) 代替!

  10. Delphi中Format与FormatDateTime函数详解

    copy:http://hi.baidu.com/yunfanleo/blog/item/0c51d9cdbc0531550eb34558.html Format是一个很常用,却又似乎很烦的方法,本人 ...