题目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 ...
随机推荐
- 使用Spring.NET的IoC容器
使用Spring.NET的IoC容器 0. 辅助类库 using System; using System.Collections.Generic; using System.Linq; using ...
- 分享9款最新超酷HTML5/CSS3应用插件
新的一周开始了,小编继续要为大家分享实用超酷的HTML5应用,今天分享的这9款最新HTML5/CSS3应用你一定会很喜欢,一起来看看. 1.HTML5 Canvas模拟衣服撕扯动画 超级逼真 今天又要 ...
- spark shell学习笔记
http://homepage.cs.latrobe.edu.au/zhe/ZhenHeSparkRDDAPIExamples.html
- 关于ARM NEON学习的一些资料
在对基于ARM-v7处理器及以上的程序进行优化时,可以使用neon优化技术来加速程序.不过搞这个的人比较少,所以网上有用的资料很稀少.我翻了半天国内国外的博客,发现还是ARM公司的帮助网站最有用: h ...
- swig和angular双花括号的冲突
swig和angular都用{{name}}来作为模板中变量的取值, 那么要共用的话怎么办: {% raw %}{{ foobar }}{% endraw %} 或者 config(['$interp ...
- mongoose实现批量删除和多id查询的api/方法
删除一条数据:传入id Model.remove({ _id: 传入的id }); 删除多条数据,传入id数组,使用$in方法 Model.remove({ _id: { $in: ['aID', ' ...
- Android Studio配置设置
文章来源:http://www.cnblogs.com/smyhvae/p/4390905.html
- Github上搭建个人博客记录
1.注册,用户名一定要起好,别随便起. 2.登录后,新建一个仓库repositories.new一个. 命名为用户名.github.io.如果发现不一样进Settings修改,rename. 3.仓库 ...
- Android刮刮卡效果
不多说,直接上代码: package com.example.test; import android.app.Activity; import androi ...
- Android播放器推荐:可以播放本地音乐、视频、在线播放音乐、视频、网络收音机等
下载链接:http://www.eoeandroid.com/forum.php?mod=attachment&aid=MTAxNTczfGMyNjNkMzFlfDEzNzY1MzkwNTR8 ...