poj3278 一维bfs 水题
来源:互联网 发布:淘宝新店没生意怎么办 编辑:程序博客网 时间:2024/06/05 08:45
/*一维bfs,确定边界,标记数组,结构体包括坐标和当前步数*/
#include <cstdio>
#include <queue>
#include <cstring>
#define N 100005
#include <cstdio>
#include <queue>
#include <cstring>
#define N 100005
using namespace std;
int vis[N];
typedef struct note {
int x;//当前点坐标
int s;//当前点已走步数
}point;
int x;//当前点坐标
int s;//当前点已走步数
}point;
point start; int endd;
int isbroder(int x)
{
return x >= 0 && x <= N;
}
{
return x >= 0 && x <= N;
}
int bfs(point s, int e)
{
queue<point> q;
while (!q.empty()) q.pop();
q.push(s);
vis[s.x] = 1;
{
queue<point> q;
while (!q.empty()) q.pop();
q.push(s);
vis[s.x] = 1;
while (!q.empty())
{
point cur = q.front(); q.pop();
if (cur.x == endd) return cur.s;//临界条件判断
else//穷举三种走法
{
point t;
if (isbroder(cur.x * 2) && !vis[cur.x * 2])
{
t.x = 2 * cur.x; t.s = cur.s + 1;
vis[t.x] = 1;
q.push(t);
}
if (isbroder(cur.x - 1) && !vis[cur.x - 1])
{
t.x = cur.x - 1; t.s = cur.s + 1;
vis[t.x] = 1;
q.push(t);
}
{
point cur = q.front(); q.pop();
if (cur.x == endd) return cur.s;//临界条件判断
else//穷举三种走法
{
point t;
if (isbroder(cur.x * 2) && !vis[cur.x * 2])
{
t.x = 2 * cur.x; t.s = cur.s + 1;
vis[t.x] = 1;
q.push(t);
}
if (isbroder(cur.x - 1) && !vis[cur.x - 1])
{
t.x = cur.x - 1; t.s = cur.s + 1;
vis[t.x] = 1;
q.push(t);
}
if (isbroder(cur.x + 1) && !vis[cur.x + 1])
{
t.x = cur.x + 1; t.s = cur.s + 1;
vis[t.x] = 1;
q.push(t);
}
}
}
return -1;//查找失败
}
{
t.x = cur.x + 1; t.s = cur.s + 1;
vis[t.x] = 1;
q.push(t);
}
}
}
return -1;//查找失败
}
int main()
{
while (scanf("%d%d", &start.x, &endd) != EOF)
{
memset(vis, 0, sizeof(vis));
start.s = 0;
int ans;
ans = bfs(start,endd);
printf("%d\n", ans);
}
return 0;
}
{
while (scanf("%d%d", &start.x, &endd) != EOF)
{
memset(vis, 0, sizeof(vis));
start.s = 0;
int ans;
ans = bfs(start,endd);
printf("%d\n", ans);
}
return 0;
}
0 0
- poj3278 一维bfs 水题
- POJ3278 BFS 水题
- poj3278 BFS
- POJ3278---bfs
- poj3278 bfs
- poj3278(bfs)
- POJ3278 -- BFS
- POJ3278--BFS
- poj3278-bfs
- POJ3278 BFS
- poj3278 bfs
- POJ3278 BFS
- poj3278 Catch That Cow bfs水题
- 照葫芦画瓢的BFS,poj3278
- 0.8poj3278(bfs)
- poj3278~bfs基础题
- POJ3278 抓奶牛 BFS
- POJ3278 使用BFS解决问题
- 属性动画心得
- socket编程中listen和accpet的理解
- MySQL导入导出数据出现乱码的解决办法
- 快速排序问题
- Unix时间与Win32 FileTime时间
- poj3278 一维bfs 水题
- 合并排序
- 循环赛日程表问题
- File.separator & File.pathSeparator的区别
- 算法之路二:刘汝佳算法竞赛入门经典 计算数组元素和
- libevent for linux building and installation
- 三种CSS写法
- 快速部署一个django app用于信息检索
- icheck插件给checkbox添加自定义效果