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,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
///Memory Limit Exceeded #include <iostream>
#include <queue>
using namespace std;
struct node { int x, y,step; };
struct node s,e;
int dd[4][2]={-1,0,1,0,0,-1,0,1},n,ans;
int g[9][9]={ 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
};
queue <node> q; void bfs()
{ node t,t2; while (! q.empty() ) q.pop(); q.push(s); while ( !q.empty()) { t=q.front(); q.pop(); if (t.x==e.x && t.y==e.y) { ans=t.step; return ; }
for (int i=0; i<4; i++)
{ t2.x=t.x+dd[i][0]; t2.y=t.y+dd[i][1]; t2.step=t.step+1; if ( g[t2.x][t2.y]==0 ) q.push(t2);
}
}
} int main(int argc, char *argv[])
{ cin>>n;
while (n--)
{ cin>>s.x>>s.y>>e.x>>e.y; s.step=0;
ans=0;
bfs();
cout<<ans<<endl;
}
return 0;
} ******************************************************************************************************* //ACCept #include <iostream>
#include <queue>
#define N 9
#define M 9
using namespace std; int map[N][M]=
{
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,
};
int d[4][2]={-1,0,0,1,1,0,0,-1 }; struct point
{
int x,y,step;
}s,e; queue <point> my; int BFS(point s)
{
int i,x,y;
point t,temp;
while (!my.empty()) my.pop();
my.push(s);
while (!my.empty())
{
t=my.front();
my.pop();
for (i=0; i<4; i++)
{
x=t.x+d[i][0];
y=t.y+d[i][1];
if (x==e.x&& y==e.y) return t.step+1;
if (x>=0 && x<N && y>=0 && y<M && map[x][y]==0)
{
temp.x=x;
temp.y=y;
temp.step=t.step+1;
my.push(temp);
}
}
}
} int main()
{
int n,x0,y0,ans;
cin>>n;
while (n--)
{
cin>>s.x>>s.y>>e.x>>e.y;
s.step=0;
if (s.x==e.x && s.y==e.y) ans=0;
else ans=BFS(s);
cout<<ans<<endl;
}
return 0;
} ************************************************************* Accept #include <cstring>
#include <iostream>
#include <queue>
using namespace std;
int ans;
int g[9][9]=
{
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,
};
int dd[4][2]={-1,0,0,1,1,0,0,-1 };
int bz[10][10];
struct node{ int x,y,step;};
struct node s,e;
queue <node> q; int bfs( )
{
int i,x,y;
node t,t2;
q.push(s); bz[s.x][s.y]=1;
while (!q.empty())
{
t=q.front();
q.pop();
if (t.x==e.x && t.y==e.y) return t.step;
for (i=0; i<4; i++)
{
x=t.x+dd[i][0];
y=t.y+dd[i][1];
if (g[x][y]==0&&!bz[x][y])
{ t2.x=x; t2.y=y; t2.step=t.step+1;
bz[x][y]=1;
q.push(t2);
}
}
}
} int main()
{
int n;
cin>>n;
while (n--)
{ while (!q.empty()) q.pop();
memset(bz,0,sizeof(bz));
cin>>s.x>>s.y>>e.x>>e.y; s.step=0;
cout<<bfs( )<<endl;
}
return 0;
}
nyist 58 最小步数 BFS的更多相关文章
- ny 58 最少步数 (BFS)
题目:http://acm.nyist.net/JudgeOnline/problem.php?pid=58 就是一道简单的BFS 练习练习搜索,一次AC #include <iostream& ...
- 带你学习BFS最小步数模型
最小步数模型 一.简介 最小步数模型和最短路模型的区别? 最短路模型:某一个点到另一个点的最短距离(坐标与坐标之间) 最小步数模型:不再是点(坐标),而是状态到另一个状态的转变 BFS难点所在(最短路 ...
- yzoi2226最小步数的详细解法
Description - 问题描述 在各种棋中,棋子的走法总是一定的,如中国象棋中马走“日”.有一位小学生就想如果马能有两种走法将增加其趣味性,因此,他规定马既能按“日”走,也能如象一样走“田”字. ...
- 1到n的最小步数
1到n的最小步数 Time Limit: 1 Sec Memory Limit: 128 MB 给你一个数n,让你求从1到n的最小步数是多少. 对于当前的数x有三种操作: 1: x+1 2: x ...
- ny58 最小步数
最少步数 时间限制: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 ...
- POJ 1753 Flip Game (高斯消元 枚举自由变元求最小步数)
题目链接 题意:4*4的黑白棋,求把棋全变白或者全变黑的最小步数. 分析:以前用状态压缩做过. 和上题差不多,唯一的不同是这个终态是黑棋或者白棋, 但是只需要把给的初态做不同的两次处理就行了. 感觉现 ...
- One Person Game(扩展欧几里德求最小步数)
One Person Game Time Limit: 2 Seconds Memory Limit: 65536 KB There is an interesting and simple ...
- Algorithm --> 棋盘中求出A到B的最小步数
求出A到B的最小步数 给定象棋盘,以及位置A和B, 求出从A到B的最小步数 代码: #include <cstdio> #include <iostream> #include ...
- [leetcode]45. Jump Game II青蛙跳(跳到终点最小步数)
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
随机推荐
- jquery生成UUID的方法
来源: http://www.broofa.com/2008/09/javascript-uuid-function/ 1.代码: http://www.broofa.com/Tools/Math ...
- 利用ASP.NET AJAX的Timer讓GridView每隔一段時間做到自動換頁的功能
最近在討論區看到這個問題,小弟利用asp.net ajax的timer來實作這個功能 利用timer每隔一段時間,讓gridview自動跳頁並且更新gridview的內容 asp.net(c#) Gr ...
- A除以B_2
本题要求计算A/B,其中A是不超过1000位的正整数,B是1位正整数.你需要输出商数Q和余数R,使得A = B * Q + R成立. 输入格式: 输入在1行中依次给出A和B,中间以1空格分隔. 输出格 ...
- asp.net发布和更新网站
我们一般使用ftp软件来更新网站,而更新之前的一个步骤就是发布项目.以下将讲解asp.net mvc如何发布网站. 打开项目 右键点击项目,选择“发布” 第一次发布前,需要配置一下发布配置文件:点击” ...
- C# DateTime
//c datetime 格式化DateTime dt = DateTime.Now;Label1.Text = dt.ToString();//2005-11-5 13:21:25Label2.Te ...
- [转]Mysql导入导出工具Mysqldump和Source命令用法详解
Mysql本身提供了命令行导出工具Mysqldump和Mysql Source导入命令进行SQL数据导入导出工作,通过Mysql命令行导出工具Mysqldump命令能够将Mysql数据导出为文本格式( ...
- Servlet(三)
重定向 服务器向浏览器发送一个302状态码以及一个Location消息头(该消息头的值是一个地址,称之为重定向地址),浏览器收到后会立即向重定向的地址发出请求,使用相应对象的API方法实现(respo ...
- Codeforces 474F - Ant colony
注意到每个区间生存下来的蚂蚁的长度等于区间的gcd 于是可以先预处理出区间的gcd 然后二分查找就好了 预处理gcd我这里用的是倍增法 总的时间复杂度O(NlogN) /* Cf 271F 倍增求区间 ...
- IE6双倍margin间距解决方案
问题:在IE6下如果某个标签使用了float属性,同时设置了其外补丁“margin:10px 0 0 10px”可以看出,上边距和左边距同样为10px,但第一个对象距左边有20px. 解决 ...
- jQuery慢慢啃之CSS(六)
1.css(name|pro|[,val|fn])//访问匹配元素的样式属性 $("p").css("color");//获取 $("p") ...