题目:http://acm.nyist.net/JudgeOnline/problem.php?pid=58

就是一道简单的BFS 练习练习搜索,一次AC

#include <iostream>
#include <vector>
#include <cstring>
#include <cstdio> using namespace std; int a[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
};
bool bool_judge[9][9];
typedef struct point
{
int x,y,t;
point()
{
x = -1; y = -1; t = -1;
}
}point; vector<point>v;
vector<point>::iterator p;
point q; void init()
{
memset(bool_judge,false,sizeof(bool_judge));
/* for(int i = 0; i < 9; i++)
{
for(int j = 0; j < 9; j++)
printf("%d ",a[i][j]);
printf("\n");
}*/
}
void add(point k,int t)
{
if(k.x-1 >= 0 && a[k.x - 1][k.y] == 0 && bool_judge[k.x - 1][k.y] == false)
{
q.x = k.x - 1,q.y = k.y,q.t = t;
v.push_back(q);
bool_judge[k.x - 1][k.y] = true;
} if(k.x + 1 <= 8 && a[k.x+1][k.y] == 0 && bool_judge[k.x+1][k.y] == false)
{
q.x = k.x + 1,q.y = k.y,q.t = t;
v.push_back(q);
bool_judge[k.x + 1][k.y] = true;
} if(k.y - 1 >= 0 && a[k.x][k.y - 1] == 0 && bool_judge[k.x][k.y - 1] == false)
{
q.x = k.x,q.y = k.y - 1,q.t = t;
v.push_back(q);
bool_judge[k.x][k.y - 1] = true;
} if(k.y + 1 <= 8 && a[k.x][k.y+1] == 0 && bool_judge[k.x][k.y + 1] == false)
{
q.x = k.x,q.y = k.y + 1,q.t = t;
v.push_back(q);
bool_judge[k.x][k.y + 1] = true;
}
} int BFS(int x2,int y2)
{
while(v.empty() != true)
{
q = v[0];
p = v.begin();
v.erase(p);
//printf("chu %d %d\n",q.x,q.y);
if(q.x == x2 && q.y == y2)
{
return q.t;
}
add(q,++q.t);
}
return 0;
}
int main()
{
init();
int n;
scanf("%d",&n);
int x1,y1,x2,y2;
while(n--)
{
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
q.x = x1,q.y = y1,q.t = 0;
v.push_back(q);
bool_judge[x1][y1] = true;
int sum = BFS(x2,y2);
printf("%d\n",sum);
v.clear();
init();
}
return 0;
}

ny 58 最少步数 (BFS)的更多相关文章

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

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

  3. 最少步数(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 ...

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

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

  5. 南阳理工 58 最少步数 (DFS)

    描述 这有一个迷宫,有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, ...

  6. 最少步数(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 ...

  7. 最少步数(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 ...

  8. T1330 最少步数(#Ⅱ- 8)(广度优先搜索)

    [题目描述] 在各种棋中,棋子的走法总是一定的,如中国象棋中马走“日”.有一位小学生就想如果马能有两种走法将增加其趣味性,因此,他规定马既能按“日”走,也能如象一样走“田”字.他的同桌平时喜欢下围棋, ...

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

随机推荐

  1. 两个有序list合并

    package 剑指office; import java.util.ArrayList; import java.util.List; public class ListMerge { /** * ...

  2. gets()函数

    基本信息: 可以无限读取,不会判断上限,以回车结束读取(这个换行符也被读取了),所以程序员应该确保buffer的空间足够大,以便在执行读操作时不发生溢出. 函数原型: char*gets(char*b ...

  3. linux系统find命令使用

    find命令简介 1.find的作用 find是个使用频率比较高的命令.常常用它在系统特定目录下,查找具有某种特征的文件. 2.    find命令的格式 find  [-path……]-option ...

  4. 动态include与静态include的区别

    搬一下以前写的 个人总结: 动态INCLUDE 用jsp:include动作实现 <jsp:include page="included.jsp" flush="t ...

  5. Android存储小结

    转自:http://www.liaohuqiu.net/cn/posts/storage-in-android/ android系统自身自带有存储,另外也可以通过sd卡来扩充存储空间.前者好比pc中的 ...

  6. Chrome disable adobe flash player

    New tab and input : chrome://plugins/ so easy~!

  7. Couldn't read row 0, col -1 from CursorWindow

    java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow.  Make sure the Curso ...

  8. 3D空间坐标系转换复习

    模型坐标系:ModelSpace     即模型本身为中心  模型本身自己 世界坐标系:WorldSpace    世界坐标系   整个世界 视角坐标系: ViewSpace       眼睛看到的范 ...

  9. CC++初学者编程教程(3) 安装VS2010 boost标准库开发环境

    1.      BOOST编译过程非常复杂,目前为了学习BOOST,首先搭建基于VS2010的BOOST开发环境. Boost库 8 9. 10. 11 12 13 14 15. 16. 17. 18 ...

  10. stl之map 排序

    排序问题,STL中默认是采用小于号来排序的,因为设置int等类型做key,它本身支持小于号运算,在一些特殊情况,比如关键字是一个结构体,涉及到排序就会出现问题,因为它没有小于号操作,insert等函数 ...