最少步数

时间限制: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. shell脚本练习(随机取名)

    1.写一个脚本,实现随机选人功能,脚本需传递一个参数进去,如    pick.sh 1 出现结果"家驹”  pick.sh 3 出现结果 "落叶" "jason ...

  2. 关于bootstrap列偏移的两种方式

    第一种方式: <div class="col-md-2 col-md-offset-9"> <input type="text" class= ...

  3. LFS,编译自己的Linux系统 - 编译临时系统

    编译GCC-4.8.2 PASS 1 解压并重命名 cd /mnt/lfs/sources tar -Jxf ../mpfr-3.1.2.tar.xz mv mpfr-3.1.2 mpfr tar - ...

  4. static的用法解析

    PHP中static变量的使用范围要更广一些,我们不仅可以在类,方法或变量前面添加static修饰符,我们甚至还能给函数内部变量添加static关键字.添加了static修饰符的变量即使在该函数执行完 ...

  5. C语言基础10

    栈区间:在函数内部声明的变量都存放在栈区间,比如int char 数组 结构体 指针,只管申请,系统会自动帮我们回收,收回的时间是作用域结束之后,遵循的原则是"先进后出". int ...

  6. Ucenter注册后,需要二次登录才能同步登录的解决方案

    1. 打开配置文件config.inc.php 在根目录data目录下最下方定义 define('DZ_DBTABLEPRE', '你的表前缀'); 2.打开uc_server/model/user. ...

  7. Bitmap 与ImageSource之间的转换

    public class ImageConverter { [DllImport("gdi32.dll", SetLastError = true)] private static ...

  8. Best Time to Buy and Sell Stock I II III

    Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of a gi ...

  9. FVANCOP/ChartNew.js

    FVANCOP/ChartNew.js FVANCOP/ChartNew.js

  10. UILabel Text 加下划线

    .h文件 #import <Foundation/Foundation.h> @interface CustomLabel : UILabel { BOOL _isEnabled; } @ ...