题目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 ...
随机推荐
- C#页面前台<%%><%#%><%=%>
ASP.net前台绑定用的最多,今天小小总结一下. 1:<%#Eval("")%> 2:<%#Bind("")%> 3:<%=变量 ...
- HTTP 错误 500.19 配置文件错误 ( 0x8007000d,0x80070032)
HTTP 错误 500.19 - Internal Server Error无法访问请求的页面,因为该页的相关配置数据无效. 详细错误信息模块 IIS Web Core 通知 未知 处理程序 尚未确定 ...
- GridView动态添加列并判断绑定数据DataTable的列类型控制展示内容
此篇随笔是2013年根据项目需求开发记录的,不一定符合大众口味,只需了解开发思路,毕竟解决方案多种多样. 下面简单说说需求点吧: (1)通过下拉列表可以选择一个DataSet(数据集),一个DataS ...
- SharePoint 2013 隐藏左边快速启动菜单栏(Hiding the Quick Launch Bar)
在SharePoint 2013默认网站页面中,很多时候,我们需要隐藏左边快速启动菜单栏,这时我们可以通过下面的样式来实现隐藏它. 和SharePoint 2010不太一样,方法改了,不过性质是一样的 ...
- vuex在页面中以对象展开运算符形式引入报错解决
当页面中以mapGetters等对象扩展运算符写的时候,会报错 unexpected token 解决如下: babel还要加插件才能解释这个写法 npm install --save-dev bab ...
- MTK 永不熄屏
步骤一: 源码/frameworks/base/packages/SettingsProvider/res/values/defaults.xml 修改<integername=</int ...
- Oracle批量执行SQL语句
SQLServer的场合,用";"分割SQL语句即可正常执行. Oracle的场合,会报ORA-00911错误.Oracle中需要加上begin end才正确. Dim Sql A ...
- Android学习——在Android中使用OpenCV的第一个程序
刚開始学习Android,因为之前比較熟悉OpenCV,于是就想先在Android上执行OpenCV试试 =============================================== ...
- Quatz入门
Demo SchedulerFactory schedFact = new org.quartz.impl.StdSchedulerFactory(); Scheduler sched = sched ...
- Import VMware ESXi from VirtualBox
VirtualBox can export appliance VMs to OVF format. And you can import the ovf format to VMware ESXi, ...