题目1457:非常可乐(广度优先遍历BFS)
题目链接:http://ac.jobdu.com/problem.php?pid=1457
详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus
参考代码:
//
// 1457 非常可乐.cpp
// Jobdu
//
// Created by PengFei_Zheng on 22/04/2017.
// Copyright © 2017 PengFei_Zheng. All rights reserved.
// #include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <cmath>
#include <queue>
#define MAX_SIZE 101
using namespace std; int s, n, m; struct N{
int a;
int b;
int c;
int t;
}; queue<N> myQueue;
bool visit[MAX_SIZE][MAX_SIZE][MAX_SIZE]; void x2y(int &x,int size_x, int &y,int size_y){
if(size_y - y >= x){
y+=x;
x = ;
}else{
x -=(size_y-y);
y = size_y;
}
} int BFS(int s, int n, int m){
while(!myQueue.empty()){
N nowP = myQueue.front();
myQueue.pop();
int a, b, c;
a = nowP.a;
b = nowP.b;
c = nowP.c;
x2y(a,s,b,n);// a--->b
if(visit[a][b][c]==false){
visit[a][b][c]=true;
N tmp;
tmp.a = a;
tmp.b = b;
tmp.c = c;
tmp.t = nowP.t+;
if((a==s/ && b==s/) || (a==s/ &&c==s/) || (b==s/ && c==s/) ) return tmp.t;
myQueue.push(tmp);
}
a = nowP.a;
b = nowP.b;
c = nowP.c;
x2y(a,s,c,m);// a--->c
if(visit[a][b][c]==false){
visit[a][b][c]=true;
N tmp;
tmp.a = a;
tmp.b = b;
tmp.c = c;
tmp.t = nowP.t+;
if((a==s/ && b==s/) || (a==s/ &&c==s/) || (b==s/ && c==s/) ) return tmp.t;
myQueue.push(tmp);
}
a = nowP.a;
b = nowP.b;
c = nowP.c;
x2y(b,n,a,s);// b--->a
if(visit[a][b][c]==false){
visit[a][b][c]=true;
N tmp;
tmp.a = a;
tmp.b = b;
tmp.c = c;
tmp.t = nowP.t+;
if((a==s/ && b==s/) || (a==s/ &&c==s/) || (b==s/ && c==s/) ) return tmp.t;
myQueue.push(tmp);
}
a = nowP.a;
b = nowP.b;
c = nowP.c;
x2y(b,n,c,m);// b--->c
if(visit[a][b][c]==false){
visit[a][b][c]=true;
N tmp;
tmp.a = a;
tmp.b = b;
tmp.c = c;
tmp.t = nowP.t+;
if((a==s/ && b==s/) || (a==s/ &&c==s/) || (b==s/ && c==s/) ) return tmp.t;
myQueue.push(tmp);
}
a = nowP.a;
b = nowP.b;
c = nowP.c;
x2y(c,m,a,s);// c--->a
if(visit[a][b][c]==false){
visit[a][b][c]=true;
N tmp;
tmp.a = a;
tmp.b = b;
tmp.c = c;
tmp.t = nowP.t+;
if((a==s/ && b==s/) || (a==s/ &&c==s/) || (b==s/ && c==s/) ) return tmp.t;
myQueue.push(tmp);
}
a = nowP.a;
b = nowP.b;
c = nowP.c;
x2y(c,m,b,n);// c--->b
if(visit[a][b][c]==false){
visit[a][b][c]=true;
N tmp;
tmp.a = a;
tmp.b = b;
tmp.c = c;
tmp.t = nowP.t+;
if((a==s/ && b==s/) || (a==s/ &&c==s/) || (b==s/ && c==s/) ) return tmp.t;
myQueue.push(tmp);
}
}
return -;
} int main(){
while(scanf("%d%d%d",&s,&n,&m)!=EOF){
if(s==) break;
if(s%==){
printf("NO\n");
continue;
}
for(int i = ; i <= s ; i++){
for(int j = ; j <= n ; j++){
for(int k = ; k <= m ; k++){
visit[i][j][k]=false;
}
}
}
while(!myQueue.empty()) myQueue.pop(); N tmp;
tmp.a = s;
tmp.b = tmp.c = tmp.t = ;
myQueue.push(tmp);
visit[s][][]=true;
int ans = BFS(s,n,m);
ans == - ? printf("NO\n") : printf("%d\n",ans);
}
}
/**************************************************************
Problem: 1457
User: zpfbuaa
Language: C++
Result: Accepted
Time:10 ms
Memory:2528 kb
****************************************************************/
题目1457:非常可乐(广度优先遍历BFS)的更多相关文章
- 广度优先遍历-BFS、深度优先遍历-DFS
广度优先遍历-BFS 广度优先遍历类似与二叉树的层序遍历算法,它的基本思想是:首先访问起始顶点v,接着由v出发,依次访问v的各个未访问的顶点w1 w2 w3....wn,然后再依次访问w1 w2 w3 ...
- 算法学习 - 图的广度优先遍历(BFS) (C++)
广度优先遍历 广度优先遍历是非经常见和普遍的一种图的遍历方法了,除了BFS还有DFS也就是深度优先遍历方法.我在我下一篇博客里面会写. 遍历过程 相信每一个看这篇博客的人,都能看懂邻接链表存储图. 不 ...
- 图的深度优先遍历(DFS)和广度优先遍历(BFS)
body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...
- 17.广度优先遍历bfs
#include <iostream> #include <boost/config.hpp> //图(矩阵实现) #include <boost/graph/adjac ...
- 图的深度优先遍历(DFS)和广度优先遍历(BFS)算法分析
1. 深度优先遍历 深度优先遍历(Depth First Search)的主要思想是: 1.首先以一个未被访问过的顶点作为起始顶点,沿当前顶点的边走到未访问过的顶点: 2.当没有未访问过的顶点时,则回 ...
- 【C++】基于邻接矩阵的图的深度优先遍历(DFS)和广度优先遍历(BFS)
写在前面:本博客为本人原创,严禁任何形式的转载!本博客只允许放在博客园(.cnblogs.com),如果您在其他网站看到这篇博文,请通过下面这个唯一的合法链接转到原文! 本博客全网唯一合法URL:ht ...
- 图的广度优先遍历(bfs)
广度优先遍历: 1.将起点s 放入队列Q(访问) 2.只要Q不为空,就循环执行下列处理 (1)从Q取出顶点u 进行访问(访问结束) (2)将与u 相邻的未访问顶点v 放入Q, 同时将d[v]更新为d[ ...
- 饥饿的小易(枚举+广度优先遍历(BFS))
题目描述 小易总是感觉饥饿,所以作为章鱼的小易经常出去寻找贝壳吃.最开始小易在一个初始位置x_0.对于小易所处的当前位置x,他只能通过神秘的力量移动到 4 * x + 3或者8 * x + 7.因为使 ...
- 图文详解两种算法:深度优先遍历(DFS)和广度优先遍历(BFS)
参考网址:图文详解两种算法:深度优先遍历(DFS)和广度优先遍历(BFS) - 51CTO.COM 深度优先遍历(Depth First Search, 简称 DFS) 与广度优先遍历(Breath ...
随机推荐
- zhuzher日志log
val log=sc.textFile("/data/logstash/data/*.log") val rowRDD2=log2.map(line=>(line.split ...
- kafka学习之-雅虎开源管理工具Kafka Manager
http://blog.csdn.net/lizhitao/article/details/44523663
- QT编译错误:invalid application of 'sizeof' to incomplete type 'Qt3DRender::QPickEvent'
执行3D常将中实体的pick操作,结果出现了编译错误:invalid application of 'sizeof' to incomplete type 'Qt3DRender::QPickEven ...
- 开源一个简易轻量的reactor网络框架
github https://github.com/sea-boat/net-reactor net-reactor it's a simple and easy net framework with ...
- nginx: [error] open() "/run/nginx.pid" failed (2: No such file or directory)
当你执行sudo nginx -s reload时出现nginx: [error] open() "/run/nginx.pid" failed (2: No such file ...
- 【Android】amr播放
http://download.csdn.net/download/r8hzgemq/4877495 http://www.cnblogs.com/fengzhblog/archive/2013/08 ...
- jquery 复制文本到剪切板插件(非 flash)
原创插件,转载请声明出处!!! jquery.copy.js 内容如下: /*! * jQuery Copy Plugin * version: 1.0.0-2018.01.23 * Requires ...
- Ansible Playbook 简介
我们去远程执行命令时要使用 command 模块,拷贝文件时要使用 copy 模块,如果我们要操作的东西很多,那就要执行很多条不同模块的命令Playbook 是一个 yaml 配置文件,我们可以把不同 ...
- Vim 手记:语法高亮
本文覆盖范围: Vim 的着色方案 设置高亮 选择颜色 语法高亮除错 每个程序员的文本编辑器缺少了语法高亮.特殊关键字和短语着色,都是不完整的.语法高亮突出了文档的结构,帮助发现排字错误,利于调试,整 ...
- React Native(九)——实现仿微信朋友圈发表动态功能
好像很久都没写博客了,罪过罪过~ 许是因为刚接触App有点生疏不得窍门吧,这个月还没有更新过文章.小个把月下来,还是觉得应该边学边总结,这样才能像大神靠近(最近刚接触同业的大牛级人物,还是从中学到了很 ...