[ZOJ 1005] Jugs (dfs倒水问题)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5
题目大意:给你6种操作,3个数a,b,n,代表我有两个杯子,第一个杯子的容量是a,第二个杯子容量是b,问能否通过6种操作,使得第二个杯子里装水量为n
dfs搜搜搜!!
状态dfs(x,y) 代表第一个杯子里有水量x,第二个杯子里有水量y。
代码:
#include <cstdio>
#include <cstdlib>
#include <string>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cctype>
#include <vector>
#include <map>
#include <set>
#include <iterator>
#include <functional>
#include <cmath>
#include <numeric>
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
typedef vector<int> VI;
#define PB push_back
#define MP make_pair
#define SZ size()
#define CL clear()
#define AA first
#define BB second
#define EPS 1e-8
#define ZERO(x) memset((x),0,sizeof(x))
const int INF = ~0U>>;
const double PI = acos(-1.0); int a,b,n;
bool vis[][];
VI ans;
bool flag; void dfs(int x,int y){
// printf("[dfs]:x=%d,y=%d\n",x,y);
if( x<||y< ) return;
if( x>a||y>b ) return;
if( flag ) return;
if( vis[x][y] ) return;
vis[x][y] = true;
if( y==n ){
for(int i=;i<ans.SZ;i++){
if( ans[i]== ) puts("fill A");
else if( ans[i]== ) puts("fill B");
else if( ans[i]== ) puts("empty A");
else if( ans[i]== ) puts("empty B");
else if( ans[i]== ) puts("pour A B");
else if( ans[i]== ) puts("pour B A");
}
puts("success");
flag = true;
return;
}
for(int i=;i<=;i++){
ans.PB(i);
if( i== ) dfs(a,y);
else if( i== ) dfs(x,b);
else if( i== ) dfs(,y);
else if( i== ) dfs(x,);
else if( i== ){
int rb = b - y;
int pa = min(rb,x);
dfs(x-pa,y+pa);
}
else if( i== ){
int ra = a - x;
int pb = min(ra,y);
dfs(x+pb,y-pb);
}
ans.pop_back();
}
vis[x][y] = false;
} int main(){
while(scanf("%d%d%d",&a,&b,&n)!=EOF){
ZERO(vis);
flag = false;
dfs(,);
}
return ;
}
[ZOJ 1005] Jugs (dfs倒水问题)的更多相关文章
- ZOJ 1005 Jugs(BFS)
Jugs In the movie "Die Hard 3", Bruce Willis and Samuel L. Jackson were confronted with th ...
- ZOJ 1005 Jugs
原题链接 题目大意:有一大一小两个杯子,相互倒水,直到其中一个杯子里剩下特定体积的水.描述这个过程. 解法:因为两个杯子的容积互质,所以只要用小杯子不断往大杯子倒水,大杯子灌满后就清空,大杯子里迟早会 ...
- ZOJ 1005:Jugs(思维)
Jugs Time Limit: 2 Seconds Memory Limit: 65536 KB Special Judge In the movie "Die Har ...
- A - Jugs ZOJ - 1005 (模拟)
题目链接:https://cn.vjudge.net/contest/281037#problem/A 题目大意:给你a,b,n.a代表第一个杯子的容量,b代表第二个杯子的容量,然后一共有6种操作.让 ...
- ZOJ - 4045District Division dfs划分子树
ZOJ - 4045District Division 题目大意:给你n个节点的树,然后让你划分这棵数使得,每一块都恰好k个节点并且两两间是连通的,也就是划分成n/k个连通集,如果可以输出YES,并输 ...
- [ZOJ 1011] NTA (dfs搜索)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1011 题目大意:在一棵树上,给你起始状态,问你能否到达终止状态. ...
- Farm Irrigation ZOJ 2412(DFS连通图)
Benny has a spacious farm land to irrigate. The farm land is a rectangle, and is divided into a lot ...
- 1005 Jugs
辗转相减,新手入门题.两个容量的灌水题,无所谓最优解. #include<stdio.h> int main(){ int A,B,T,sA,sB; ){ sA=sB=; ){ ){ pr ...
- Jugs(回溯法)
ZOJ Problem Set - 1005 Jugs Time Limit: 2 Seconds Memory Limit: 65536 KB Special Judge In ...
随机推荐
- html之label标签
label标签为input元素定义标注,label标签与相关元素通过id属性绑定在一起. 相关属性: for:规定label绑定到哪个表单元素 form:规定label字段所属的一个或多个表单 示例代 ...
- FileDataSource java的文件操作
FileDataSource:(javax.activation.FileDataSource.FileDataSource(File file)) FileDataSource 类实现一个封装文件的 ...
- python psutil 模块
一.获取系统性能信息 1 .CPU信息 User time,执行用户进程的时间百分比 System time,执行内核进程和中断的百分比 Wait IO,由于IO等待而使CPU处于idle(空闲)状态 ...
- ActionScript ArrayCollection sort
var sortByOrderId:Sort = new Sort; sortByOrderId.fields = [new SortField("orderId")]; orde ...
- 【freemaker】之循环,判断,对象取值
entity: public class Employee { private Integer id; private String name; private Integer age; privat ...
- cent os下面的基本配置操作
二,修改Linux分辨率命令行 在root用户模式下,输入$ vi /boot/grub/grub.conf(路径可能会不一样,也可以是 /etc/grub.conf),打开grub.conf文件 我 ...
- 返回顶部(解决IE6固定定位)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- tarjan算法 POJ3177-Redundant Paths
参考资料传送门 http://blog.csdn.net/lyy289065406/article/details/6762370 http://blog.csdn.net/lyy289065406/ ...
- 黄聪:MYSQL提交一批ID,查询数据库中不存在的ID并返回
假设你数据库有个A表: ID NAME 1 aaa 2 bbb 3 ccc 4 ddd 需求:给你几个ID,返回A表中不存在的ID? 例如提交1,2,8,9 返回8,9 sel ...
- System.InvalidOperationException: Sequence contains no elements
foreach (var keyCode in unexpectedKeyCodesDetected) { string unexpected = expectedCapturedKeyCodes.W ...