HDOJ1495(倒水BFS)
非常可乐
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 10442 Accepted Submission(s): 4193
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
const int MAXN=;
struct Node{
int s,n,m;
int step;
Node(){}
Node(int cs,int cn,int cm,int cstep)
{
s=cs;
n=cn;
m=cm;
step=cstep;
}
};
int vis[MAXN][MAXN][MAXN];
int S,N,M;
void bfs()
{
queue<Node> que;
que.push(Node(S,,,));
vis[S][N][M]=;
while(!que.empty())
{
Node now=que.front();que.pop();
if((now.s==&&now.n==now.m)||(now.n==&&now.s==now.m)||(now.m==&&now.s==now.n))
{
printf("%d\n",now.step);
return ;
}
int nes,nen,nem,mn;
mn=min(N-now.n,now.s);
nes=now.s-mn;
nen=now.n+mn;
nem=now.m;
if(!vis[nes][nen][nem])
{
vis[nes][nen][nem]=;
que.push(Node(nes,nen,nem,now.step+));
} mn=min(M-now.m,now.s);
nes=now.s-mn;
nen=now.n;
nem=now.m+mn;
if(!vis[nes][nen][nem])
{
vis[nes][nen][nem]=;
que.push(Node(nes,nen,nem,now.step+));
} mn=min(S-now.s,now.n);
nes=now.s+mn;
nen=now.n-mn;
nem=now.m;
if(!vis[nes][nen][nem])
{
vis[nes][nen][nem]=;
que.push(Node(nes,nen,nem,now.step+));
} mn=min(M-now.m,now.n);
nes=now.s;
nen=now.n-mn;
nem=now.m+mn;
if(!vis[nes][nen][nem])
{
vis[nes][nen][nem]=;
que.push(Node(nes,nen,nem,now.step+));
} mn=min(S-now.s,now.m);
nes=now.s+mn;
nen=now.n;
nem=now.m-mn;
if(!vis[nes][nen][nem])
{
vis[nes][nen][nem]=;
que.push(Node(nes,nen,nem,now.step+));
} mn=min(N-now.n,now.m);
nes=now.s;
nen=now.n+mn;
nem=now.m-mn;
if(!vis[nes][nen][nem])
{
vis[nes][nen][nem]=;
que.push(Node(nes,nen,nem,now.step+));
}
}
printf("NO\n");
}
int main()
{
while(scanf("%d%d%d",&S,&N,&M)!=EOF&&S&&N&&M)
{
memset(vis,,sizeof(vis));
if(S%!=)
{
printf("NO\n");
}
else bfs();
}
return ;
}
HDOJ1495(倒水BFS)的更多相关文章
- hdu1495 倒水bfs
题目链接:http://icpc.njust.edu.cn/Problem/Hdu/1495/ 题意:给定三个杯子S,M,N,满足S=M+N,现在要求用最短的次数将S杯中的饮倒平分到两个杯子中.我们首 ...
- poj3414Pots(倒水BFS)
Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13231 Accepted: 5553 Special J ...
- hdoj1495简单BFS
#include <stdio.h> #include <string.h> #include <math.h> #include <algorithm> ...
- POJ 3414 Pots【bfs模拟倒水问题】
链接: http://poj.org/problem?id=3414 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22009#probl ...
- codevs1226倒水问题(Bfs)
/* 首先建立模型 可以看成是三个水杯 第三个无穷大 (这里看成是201足够了) 最少步数 想到Bfs 维护队列里的状态:要有个步数 还要有v :此时刻三个杯子有多少水 然后倒水:因为没有刻度 所以有 ...
- HDU 1495 非常可乐(BFS倒水问题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1495 题目大意:只有两个杯子,它们的容量分别是N 毫升和M 毫升 可乐的体积为S (S<101) ...
- HDU 1495 非常可乐【BFS/倒水问题】
非常可乐 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submissi ...
- CodeVS 1226 倒水问题【DFS/BFS】
题目描述 Description 有两个无刻度标志的水壶,分别可装 x 升和 y 升 ( x,y 为整数且均不大于 100 )的水.设另有一水 缸,可用来向水壶灌水或接从水壶中倒出的水, 两水壶间,水 ...
- POJ - 3414 Pots BFS(著名倒水问题升级版)
Pots You are given two pots, having the volume of A and B liters respectively. The following operati ...
随机推荐
- unity里standard pbr(一)
关注forwardbase下的 standard.shader #pragma vertex vertBase #pragma fragment fragBase #include "Uni ...
- RZ11 系统配置参数
SAP系统配置参数详解[转] SAP 系统参数设置 path: /usr/sap/PRD/SYS/profile profile: PRD_DVEBMGS00_sapapp 如果您想查看所有的参数及当 ...
- CentOS iSCSI服务器搭建------LUN篇
先上服务器信息(你懂得) [root@node ~]# cat /etc/redhat-release CentOS release 6.6 (Final) [root@node ~]# uname ...
- fields_for
1 一对多 Using Strong Parameters With Fields For & Nested Forms in Rails 4 http://www.rubyexperimen ...
- 项目中对模板和js,css文件进行压缩的处理类
我们知道,在html的页面中,所有空格和换行符其实都会占据一定的空间,即使使用了gzip压缩,在传输过程中依然会浪费用户的流量和我们自己服务器的带宽,此脚本就是为了解决这个问题而诞生的. 请自行下载G ...
- samsung n143 brightness on linux mint
sudo vi /etc/default/grub Find the line which says GRUB_CMDLINE_LINUX="" enter acpi_backli ...
- Data Structure Binary Tree: Convert a given tree to its Sum Tree
http://www.geeksforgeeks.org/convert-a-given-tree-to-sum-tree/ #include <iostream> #include &l ...
- 一组带Tooltip提示框的CSS3图标按钮
在线演示 本地下载
- Vim 替换命令
一,":substitute"的使用 :substitute 命令可以对一个指定范围的区域执行替换操作,可以简写为:s ,它的通用形式如下: :[range]substitute/ ...
- 好久没更了,确实太忙了--dedecms篇
最近写了一个地方新闻网站,可以看看:www.qiluhuabao.com.模仿www.bashan.com写的 用的是dedecms,只听过,没用过.终于在上周来了一个必须用到cms的项目,现学现卖, ...