题目链接: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)的更多相关文章

  1. 广度优先遍历-BFS、深度优先遍历-DFS

    广度优先遍历-BFS 广度优先遍历类似与二叉树的层序遍历算法,它的基本思想是:首先访问起始顶点v,接着由v出发,依次访问v的各个未访问的顶点w1 w2 w3....wn,然后再依次访问w1 w2 w3 ...

  2. 算法学习 - 图的广度优先遍历(BFS) (C++)

    广度优先遍历 广度优先遍历是非经常见和普遍的一种图的遍历方法了,除了BFS还有DFS也就是深度优先遍历方法.我在我下一篇博客里面会写. 遍历过程 相信每一个看这篇博客的人,都能看懂邻接链表存储图. 不 ...

  3. 图的深度优先遍历(DFS)和广度优先遍历(BFS)

    body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...

  4. 17.广度优先遍历bfs

    #include <iostream> #include <boost/config.hpp> //图(矩阵实现) #include <boost/graph/adjac ...

  5. 图的深度优先遍历(DFS)和广度优先遍历(BFS)算法分析

    1. 深度优先遍历 深度优先遍历(Depth First Search)的主要思想是: 1.首先以一个未被访问过的顶点作为起始顶点,沿当前顶点的边走到未访问过的顶点: 2.当没有未访问过的顶点时,则回 ...

  6. 【C++】基于邻接矩阵的图的深度优先遍历(DFS)和广度优先遍历(BFS)

    写在前面:本博客为本人原创,严禁任何形式的转载!本博客只允许放在博客园(.cnblogs.com),如果您在其他网站看到这篇博文,请通过下面这个唯一的合法链接转到原文! 本博客全网唯一合法URL:ht ...

  7. 图的广度优先遍历(bfs)

    广度优先遍历: 1.将起点s 放入队列Q(访问) 2.只要Q不为空,就循环执行下列处理 (1)从Q取出顶点u 进行访问(访问结束) (2)将与u 相邻的未访问顶点v 放入Q, 同时将d[v]更新为d[ ...

  8. 饥饿的小易(枚举+广度优先遍历(BFS))

    题目描述 小易总是感觉饥饿,所以作为章鱼的小易经常出去寻找贝壳吃.最开始小易在一个初始位置x_0.对于小易所处的当前位置x,他只能通过神秘的力量移动到 4 * x + 3或者8 * x + 7.因为使 ...

  9. 图文详解两种算法:深度优先遍历(DFS)和广度优先遍历(BFS)

    参考网址:图文详解两种算法:深度优先遍历(DFS)和广度优先遍历(BFS) - 51CTO.COM 深度优先遍历(Depth First Search, 简称 DFS) 与广度优先遍历(Breath ...

随机推荐

  1. C# 窗体间传值(使用委托与自定义事件)

    using System; using System.Drawing; using System.Windows.Forms; namespace 跨窗体调用控件 { public partial c ...

  2. 通过tarball形式安装HBASE Cluster(CDH5.0.2)——Hadoop NameNode HA 切换引起的Hbase错误,以及Hbase如何基于NameNode的HA进行配置

    通过tarball形式安装HBASE Cluster(CDH5.0.2)——Hadoop NameNode HA 切换引起的Hbase错误,以及Hbase如何基于NameNode的HA进行配置 配置H ...

  3. ubuntu14.04 允许root用ssh登录

    重启ssh service ssh restart 查看ssh是否已安装 ssh进程 ps -e | grep ssh ssh占用端口 lsof -i:22 ssh服务已启动,但是用root账号无法连 ...

  4. 超强OCR文字识别软件首选ABBYY FineReader

    提到纸质文档—转换—文本格式—可编辑这些字眼,相信大家的第一反映都是OCR文字识别软件,如何排除错误或利用辅助信息提高识别正确率,是OCR最重要的课题,衡量一个OCR系统性能好坏的主要指标无非是精确度 ...

  5. 详解BarTender符号体系特殊选项之“行数”

    上面两篇文章小编和大家分享了BarTender符号体系特殊选项中的“行高”和“列”.此外,某些二维 (2D) 符号体系的结构为多个信息行,每一行看上去都像一个非常窄的条形码. 例如,以下图像是含 3 ...

  6. phpcms v9不限模型全站搜索

    简单修改一下v9默认的搜索功能,可以不按模型搜索全站内容 下面是被修改后的search模块中的index.php文件 <?php defined('IN_PHPCMS') or exit('No ...

  7. C# toolstrip 上添加DateTimePicker Control控件

    private void AddDTPtoToolstrip( n){DateTimePicker dtp = DateTimePicker;dtp.Width = 110;dtp.Format = ...

  8. 2014-07-08 hibernate tenancy

    http://en.wikipedia.org/wiki/Multitenancy http://www.infoq.com/news/2012/01/hibernate-4-released htt ...

  9. SQLServer------备份与还原

    转载: http://www.cnblogs.com/zgqys1980/archive/2012/07/04/2576382.html

  10. iOS 添加Resource bundle target(静态库中使用图片等资源)

    一.首先将资源文件打包成bundle 新建工程:File -> New -> Project... -> OS X -> Framework & Library -&g ...