最少步数(bfs)
最少步数
- 描述
-
这有一个迷宫,有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,10表示道路,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)的更多相关文章
- 最少步数(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 ...
- POJ 3126 Prime Path【从一个素数变为另一个素数的最少步数/BFS】
Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 26475 Accepted: 14555 Descript ...
- ny 58 最少步数 (BFS)
题目:http://acm.nyist.net/JudgeOnline/problem.php?pid=58 就是一道简单的BFS 练习练习搜索,一次AC #include <iostream& ...
- 最少步数(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 ...
- 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 ...
- [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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- php web qq第三方登录
官方api地址:http://wiki.connect.qq.com/%E5%87%86%E5%A4%87%E5%B7%A5%E4%BD%9C_oauth2-01.去qq互联上注册申请成为开发者,并创 ...
- WSGI的理解
Python web开发中,服务端程序可分为2个部分: 服务器程序(用来接收.整理客户端发送的请求) 应用程序(处理服务器程序传递过来的请求) 在开发应用程序的时候,我们会把常用的功能封装起来,成为各 ...
- 网易2014校园招聘杭州Java笔试题
10) ABC http://soft.chinabyte.com/os/56/12516056.shtml 11) BD. 12) AC. http://blog.sina.com.cn/s/blo ...
- HDU 4970 Killing Monsters
开始以为是线段树,算了一下复杂度也觉得能过...但是这题貌似卡了线段树... 具体做法: 对每一个塔,记录attack[l]+=d,attack[r+1]-=d;这样对于每个block,受到的伤害就是 ...
- inPolygonTest学习和C++实现
大家好,我是小鸭酱,博客地址为:http://www.cnblogs.com/xiaoyajiang 此篇博客实现了判定平面一点是否在给定多边形内部的功能.精确,性能优良,因为只包含加法和乘法运算,效 ...
- 系统service
Context.TELEPHONY_SERVICE TelephonyManager tManager = (TelephonyManager)getSystemService(Context.TEL ...
- Ring3下Hook NtQueryDirectoryFile隐藏文件
NTSTATUS WINAPI Hook_NtQueryDirectoryFile(IN HANDLE FileHandle,IN HANDLE Event OPTIONAL,IN PIO_APC_R ...
- perl 爬取某理财网站产品信息
use LWP::UserAgent; use utf8; use DBI; $user="root"; $passwd="xxxxx"; $dbh=" ...
- UESTC_秋实大哥の恋爱物语 2015 UESTC Training for Search Algorithm & String<Problem K>
K - 秋实大哥の恋爱物语 Time Limit: 5000/2000MS (Java/Others) Memory Limit: 32000/32000KB (Java/Others) Su ...
- Word Search II 解答
Question Given a 2D board and a list of words from the dictionary, find all words in the board. Each ...