最少步数

时间限制:3000 ms  |  内存限制:65535 KB
难度:4
 
描述

这有一个迷宫,有0~8行和0~8列:

1,1,1,1,1,1,1,1,1
 1,0,0,1,0,0,1,0,1
 1,0,0,1,1,0,0,0,1
 1,0,1,0,1,1,0,1,1
 1,0,0,0,0,1,0,0,1
 1,1,0,1,0,1,0,0,1
 1,1,0,1,0,1,0,0,1
 1,1,0,1,0,0,0,0,1
 1,1,1,1,1,1,1,1,1

0表示道路,1表示墙。

现在输入一个道路的坐标作为起点,再如输入一个道路的坐标作为终点,问最少走几步才能从起点到达终点?

(注:一步是指从一坐标点走到其上下左右相邻坐标点,如:从(3,1)到(4,1)。)

 
输入
第一行输入一个整数n(0<n<=100),表示有n组测试数据;
随后n行,每行有四个整数a,b,c,d(0<=a,b,c,d<=8)分别表示起点的行、列,终点的行、列。
输出
输出最少走几步。
样例输入
2
3 1 5 7
3 1 6 7
样例输出
12
11
TLE code
 #include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
int a,b,c,t;
int map[][][];
int Min=;
int dirx[]={,,,-},diry[]={,-,,},dirz[]={,-};
int dfs(int z,int x,int y,int step)
{
int i,j;
if(step>t||z<||z>=a||x<||x>=b||y<||y>=c)
return ;
if(map[z][x][y])
return ;
if(x==b-&&y==c-&&z==a-)
{
Min=min(Min,step);
return ;
}
else
{
step++;
map[z][x][y]=;
for(i=;i<;i++)
{
int xx=x+dirx[i],yy=y+diry[i];
dfs(z,xx,yy,step);
}
for(i=;i<;i++)
{
int zz=z+dirz[i];
dfs(zz,x,y,step);
}
map[z][x][y]=;
}
return ;
}
int main()
{
int k,i,j,p;
//freopen("in.txt","r",stdin);
cin>>k;
while(k--)
{
Min=;
cin>>a>>b>>c>>t;
for(i=;i<a;i++) //层
for(j=;j<b;j++)
for(p=;p<c;p++)
cin>>map[i][j][p];
dfs(,,,);
if(Min>t)
cout<<-<<endl;
else
cout<<Min<<endl;
}
}

AC code

 #include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
int a,b,c,d,m;
int x[][]={
,,,,,,,,,
,,,,,,,,,
,,,,,,,,,
,,,,,,,,,
,,,,,,,,,
,,,,,,,,,
,,,,,,,,,
,,,,,,,,,
,,,,,,,,,
};
int bfs(int p,int q,int s)
{
if(x[p][q]==)
return ;
if(p==c&&q==d)
{
m=min(s,m);
return ;
}
s++;
x[p][q]=;//不再返回
bfs(p-,q,s);//向四周搜索
bfs(p+,q,s);
bfs(p,q+,s);
bfs(p,q-,s);
x[p][q]=;//不得返回。。。看了半天才明白
return ;
}
int main()
{
int N;
cin>>N;
while(N--)
{
cin>>a>>b>>c>>d;
m=;
bfs(a,b,);
cout<<m<<endl;
}
return ;
}

最少步数(bfs)的更多相关文章

  1. 最少步数(bfs)

    最少步数 时间限制:3000 ms  |  内存限制:65535 KB 难度:4   描述 这有一个迷宫,有0~8行和0~8列: 1,1,1,1,1,1,1,1,1 1,0,0,1,0,0,1,0,1 ...

  2. POJ 3126 Prime Path【从一个素数变为另一个素数的最少步数/BFS】

    Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 26475 Accepted: 14555 Descript ...

  3. ny 58 最少步数 (BFS)

    题目:http://acm.nyist.net/JudgeOnline/problem.php?pid=58 就是一道简单的BFS 练习练习搜索,一次AC #include <iostream& ...

  4. 最少步数(dfs + bfs +bfs优化)

    最少步数 时间限制:3000 ms  |  内存限制:65535 KB 难度:4   描述 这有一个迷宫,有0~8行和0~8列: 1,1,1,1,1,1,1,1,1 1,0,0,1,0,0,1,0,1 ...

  5. ACM 最少步数

    最少步数 时间限制:3000 ms  |  内存限制:65535 KB 难度:4   描述 这有一个迷宫,有0~8行和0~8列: 1,1,1,1,1,1,1,1,1 1,0,0,1,0,0,1,0,1 ...

  6. [ACM_搜索] ZOJ 1103 || POJ 2415 Hike on a Graph (带条件移动3盘子到同一位置的最少步数 广搜)

    Description "Hike on a Graph" is a game that is played on a board on which an undirected g ...

  7. NYOJ 58 最少步数

    最少步数 时间限制:3000 ms  |  内存限制:65535 KB 难度:4   描述 这有一个迷宫,有0~8行和0~8列: 1,1,1,1,1,1,1,1,1 1,0,0,1,0,0,1,0,1 ...

  8. nyoj 1022 最少步数【优先队列+广搜】

    最少步数 时间限制:3000 ms  |  内存限制:65535 KB 难度:4   描述 这有一个迷宫,有0~8行和0~8列: 1,1,1,1,1,1,1,1,1 1,0,0,1,0,0,1,0,1 ...

  9. nyist 58 最小步数 BFS

    最少步数 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描述 这有一个迷宫,有0~8行和0~8列: 1,1,1,1,1,1,1,1,1 1,0,0,1,0,0,1,0,1 1,0 ...

随机推荐

  1. Ubuntu Codeblocks Installation

    Download and Installation sudo add-apt-repository ppa:damien-moore/codeblocks sudo apt update sudo a ...

  2. win 下python2.7 pymssql连接ms sqlserver 2000

    python DB-API 连接mysql 要用到库pymssql 下载请到https://pypi.python.org/pypi/pymssql/2.0.1我这里下载的是ms windows in ...

  3. 【5】将服务部署到本机(Ubuntu14.04)

    首先,先将文件复制到指定的文件夹 我这里选择在/var下面新建一个www的文件夹来存放 复制BLOG文件夹的内容到www文件夹下 sudo cp -r /home/jakeychen/Jakey/Bl ...

  4. iptables中规则的关系——以只允许某些IP段访问为例

    最近遇到一个问题:服务器被全球的IP频繁试图通过ssh登录. 于是想到通过iptables防火墙限制访问,达到:仅允许指定ip段访问ssh服务(22端口). 关于iptables添加规则的文章有很多, ...

  5. C# 调用 Web Service

    Web Service也叫XML Web Service WebService是一种可以接收从Internet或者Intranet上的其它系统中传递过来的请求,轻量级的独立的通讯技术.是:通过SOAP ...

  6. java设计模式--行为型模式--备忘录模式

    备忘录模式,我们平常所做的备忘录么.还得深深研究哦. 备忘录模式: 备忘录模式 概述 在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态.这样以后就可将该对象恢复到原先保存的状 ...

  7. Contains Duplicate 解答

    Question Given an array of integers, find if the array contains any duplicates. Your function should ...

  8. HTTP学习笔记

    最近在看HTTP权威指南, 然后准备从Python的request库入手,看它的源代码实现 http://cn.python-requests.org/zh_CN/latest/ 挖坑 今年准备在gi ...

  9. 格而知之6:我所理解的Runtime(1)

    基本简介 1.根据官方文档,OC有一个特性:它会尽可能把一些决定从编译时和链接时推迟到运行时才处理,所以这门语言需要的就不只是一个编译器,它还需要一个runtime系统来处理那些已经被编译过的代码. ...

  10. Android 四大组件之 Activity

    1 简介 Activity (活动) 即应用程序 显示的 界面.可以通过两种方式 设置显示的内容 1:纯代码方式 2:xml 布局方式 无论哪一种方式,都是通过 setContentView 来设置显 ...