最少步数
时间限制: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的更多相关文章

  1. ny 58 最少步数 (BFS)

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

  2. 带你学习BFS最小步数模型

    最小步数模型 一.简介 最小步数模型和最短路模型的区别? 最短路模型:某一个点到另一个点的最短距离(坐标与坐标之间) 最小步数模型:不再是点(坐标),而是状态到另一个状态的转变 BFS难点所在(最短路 ...

  3. yzoi2226最小步数的详细解法

    Description - 问题描述 在各种棋中,棋子的走法总是一定的,如中国象棋中马走“日”.有一位小学生就想如果马能有两种走法将增加其趣味性,因此,他规定马既能按“日”走,也能如象一样走“田”字. ...

  4. 1到n的最小步数

    1到n的最小步数 Time Limit: 1 Sec  Memory Limit: 128 MB 给你一个数n,让你求从1到n的最小步数是多少. 对于当前的数x有三种操作: 1:  x+1 2:  x ...

  5. 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 ...

  6. POJ 1753 Flip Game (高斯消元 枚举自由变元求最小步数)

    题目链接 题意:4*4的黑白棋,求把棋全变白或者全变黑的最小步数. 分析:以前用状态压缩做过. 和上题差不多,唯一的不同是这个终态是黑棋或者白棋, 但是只需要把给的初态做不同的两次处理就行了. 感觉现 ...

  7. One Person Game(扩展欧几里德求最小步数)

    One Person Game Time Limit: 2 Seconds      Memory Limit: 65536 KB There is an interesting and simple ...

  8. Algorithm --> 棋盘中求出A到B的最小步数

    求出A到B的最小步数 给定象棋盘,以及位置A和B, 求出从A到B的最小步数 代码: #include <cstdio> #include <iostream> #include ...

  9. [leetcode]45. Jump Game II青蛙跳(跳到终点最小步数)

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

随机推荐

  1. WPF中的资源简介、DynamicResource与StaticResource的区别(转)

    什么叫WPF的资源(Resource)?资源是保存在可执行文件中的一种不可执行数据.在WPF的资源中,几乎可以包含图像.字符串等所有的任意CLR对象,只要对象有一个默认的构造函数和独立的属性. 也就是 ...

  2. java '相等'的比较.

    我们知道对于操作符 "==",如果比较的是原生类型(primitive type),表示的是 '值本身'是否相等;而对于引用类型(reference type),表示的是 '对象的 ...

  3. 在iframe中获取iframe外的对象

    parent.document.getElementById("dom ID"); $($(parent.document.getElementById("video-i ...

  4. GUID是什么意思及Guid在sqlserver中的使用

    GUID(全球唯一标识)是微软使用的一个术语,由一个特定的算法,给某一个实体,如Word文档,创建一个唯一的标识,GUID值就是这个唯一的标识码.GUID广泛应用于微软的产品中,用于识别接口.复制品. ...

  5. javascript DOM小结

    一:定义 dom:文档对象模型. dom是针对HTML和XML文档的一个API.dom描绘了一个层次化的节点树,允许开发人员添加.移除.修改页面的某一部分. 1:childNodes(返回当前节点的子 ...

  6. SGU 135.Drawing Lines

    水题,不说了. #include <iostream> using namespace std; int f[70000]={1}; int n; int main(){ cin>& ...

  7. zoj 3209.Treasure Map(DLX精确覆盖)

    直接精确覆盖 开始逐行添加超时了,换成了单点添加 #include <iostream> #include <cstring> #include <cstdio> ...

  8. overflow第一次觉得你有点可恶

    今天用css做下拉菜单,因为不需要做手机自适应,再手机里看起来工整一点就行,可是列表中最后一个li的宽度撑开了父div,导致看起来很糟糕,所以给父元素加overflow:hidden:但是下拉列表也被 ...

  9. TatukGIS - GisDefs - CheckDir 函数

    函数名称  CheckDir 所在单元  GisDefs 函数原型  function CheckDir(const _path: String): Boolean;   函数说明 如果 _path ...

  10. Connect mysql on Linux from Windows

    ON LINUX: 1 sudo apt-get install mysql-server 2 sudo apt-get install python-dev 3 sudo apt-get insta ...