首页 技术 正文
技术 2022年11月15日
0 收藏 809 点赞 2,542 浏览 2143 个字

小鼠迷宫问题

nid=24#time” title=”C、C++、go、haskell、lua、pascal Time Limit1500ms Memory Limit 65536K java、python2、python3、ruby、perl Time Limit3000ms Memory Limit 131072K” style=”padding:0px; margin:0px; color:rgb(83,113,197); text-decoration:none”>

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
BFS搜到最短路径。。然后BFS怒搜路径数
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cctype>
#include <cstdlib>
#include <set>
#include <map>
#include <vector>
#include <string>
#include <queue>
#include <stack>
#include <cmath>
#define LL long long
using namespace std;
const int INF = 0x3f3f3f3f;
struct node
{
int x,y,step;
};
int sb,ans,n,m,sx,sy,ex,ey,dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
bool ma[110][110],vis[110][110];
int bfs()
{
memset(vis,0,sizeof(vis));
queue <node> Q;
node now,next;
now.x=sx;now.y=sy;now.step=0;
vis[sx][sy]=1;
Q.push(now);
while(!Q.empty())
{
now=Q.front();Q.pop();
if(now.x==ex&&now.y==ey)
return now.step;
for(int i=0;i<4;i++)
{
next.x=now.x+dir[i][0];
next.y=now.y+dir[i][1];
if(next.x>=1&&next.x<=n&&next.y>=1&&next.y<=m&&!vis[next.x][next.y]&&ma[next.x][next.y])
{
vis[next.x][next.y]=1;
next.step=now.step+1;
Q.push(next);
}
}
}
return -1;
}
void dfs(int x,int y,int step)
{
if(step>sb) return ;
if(x==ex&&y==ey&&step==sb)
{
++ans;
return ;
}
for(int i=0;i<4;i++)
{
int tx=x+dir[i][0];
int ty=y+dir[i][1];
if(tx>=1&&tx<=n&&ty>=1&&ty<=m&&!vis[tx][ty]&&ma[tx][ty])
{
vis[tx][ty]=1;
dfs(tx,ty,step+1);
vis[tx][ty]=0;
}
}
}
int main()
{
int k,u,v;
while(~scanf("%d%d%d",&n,&m,&k))
{
memset(ma,1,sizeof(ma));
while(k--)
{
scanf("%d%d",&u,&v);
ma[u][v]=0;
}
scanf("%d%d%d%d",&sx,&sy,&ex,&ey);
sb=bfs();
if(sb==-1)
{
puts("No Solution!");
continue;
}
printf("%d\n",sb);
memset(vis,0,sizeof(vis));
ans=0;vis[sx][sy]=1;
dfs(sx,sy,0);
printf("%d\n",ans);
}
return 0;
}

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,082
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,556
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,406
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,179
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,815
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,898