博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ 1915 简单 广搜题
阅读量:7072 次
发布时间:2019-06-28

本文共 1279 字,大约阅读时间需要 4 分钟。

题意要求是给出两个坐标,给出一个棋盘的大小,问用最小的步数 从初始坐标走到最后终点,

此题是典型的广搜题,但自己大意了,忘记把 标记数组visisted【】【】初始化了,以致于错了很多次,

 

就当给自己长教训吧

 

 

#include<iostream>

#include<queue>
#include<fstream>
using namespace std;
int bmp[8][2] = {
{-2,-1},{-2,1},{-1,-2},{-1,2},{1,-2},{1,2},{2,-1},{2,1}};
int visited[310][310];
int N,T,p;
struct node{
int x;
int y;
int temp;
};
queue <node> vi;
node first,next,last,top,jian;
bool cheak(int a, int b)
{
if(a >= 0 && a < T && b >= 0 && b < T )
return true;
return false;
}
void bfs(int x,int y)
{
while(!vi.empty())
vi.pop();
//p = 0;
top.x = x;
top.y = y;
top.temp = 0;
vi.push(top);
visited[top.x][top.y] = 1;
while(!vi.empty())
{
jian = vi.front();
vi.pop();
for(int i = 0;i < 8; i++)
{
next.x = jian.x + bmp[i][0];
next.y = jian.y + bmp[i][1];
next.temp = jian.temp;
if(cheak(next.x ,next.y) && !visited[next.x][next.y] )
{
if(next.x == last.x && next.y == last.y)
{
p = next.temp+1;
return;
}
next.temp++;
visited[next.x][next.y] = 1;
vi.push(next);
}
}
}
}

int main()

{
ifstream cin("in.txt");
cin>>N;
while(N--)
{
memset(visited,0,sizeof(visited));
cin>>T;
cin>>first.x>>first.y>>last.x>>last.y;
first.temp = 0;
if(first.x == last.x && first.y == last.y)
p = 0;
else
bfs(first.x,first.y);
cout<<p<<endl;
}
}

转载于:https://www.cnblogs.com/lfyy/archive/2012/10/30/2746329.html

你可能感兴趣的文章
查看手机号是否注册百度开发者
查看>>
前端工作流(draft)
查看>>
spring cloud eureka 高可用搭建
查看>>
OSChina 周二乱弹 —— 做羞羞事情的正确方法
查看>>
OSChina 周四乱弹 —— 会编程的女神长这样
查看>>
Coherence报socket错误
查看>>
面对、接受、处理、放下
查看>>
Eclipse自动生成jni头文件
查看>>
设计模式--外观模式
查看>>
mongodb_shard
查看>>
java成为移动互联网时代必学语言的六大理由
查看>>
git使用实践
查看>>
ABBYY FineReader 12的强大用途
查看>>
java实现KMP字符串匹配算法
查看>>
四种对象属性的复制
查看>>
springmvc-json小案例
查看>>
运行JAR文件(java -jar)
查看>>
解析Cloudera Manager内部结构、功能包括配置文件、目录位置等
查看>>
一致性哈希算法的java实现
查看>>
增强 Bash 的功能
查看>>