一个搜索,求bug
from http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1005
In the movie "Die Hard 3", Bruce Willis and Samuel L. Jackson were confronted with the following puzzle. They were given a 3-gallon jug and a 5-gallon jug and were asked to fill the 5-gallon jug with exactly 4 gallons. This problem generalizes that puzzle.
You have two jugs, A and B, and an infinite supply of water. There are three types of actions that you can use: (1) you can fill a jug, (2) you can empty a jug, and (3) you can pour from one jug to the other. Pouring from one jug to the other stops when the first jug is empty or the second jug is full, whichever comes first. For example, if A has 5 gallons and B has 6 gallons and a capacity of 8, then pouring from A to B leaves B full and 3 gallons in A.
A problem is given by a triple (Ca,Cb,N), where Ca and Cb are the capacities of the jugs A and B, respectively, and N is the goal. A solution is a sequence of steps that leaves exactly N gallons in jug B. The possible steps are
fill A
fill B
empty A
empty B
pour A B
pour B A
success
where "pour A B" means "pour the contents of jug A into jug B", and "success" means that the goal has been accomplished.
You may assume that the input you are given does have a solution.
Input
Input to your program consists of a series of input lines each defining one puzzle. Input for each puzzle is a single line of three positive integers: Ca, Cb, and N. Ca and Cb are the capacities of jugs A and B, and N is the goal. You can assume 0 < Ca <= Cb and N <= Cb <=1000 and that A and B are relatively prime to one another.
Output
Output from your program will consist of a series of instructions from the list of the potential output lines which will result in either of the jugs containing exactly N gallons of water. The last line of output for each puzzle should be the line "success". Output lines start in column 1 and there should be no empty lines nor any trailing spaces.
Sample Input
3 5 4
5 7 3
Sample Output
fill B
pour B A
empty A
pour B A
fill B
pour B A
success
fill A
pour A B
fill A
pour A B
empty B
pour A B
success
In the movie "Die Hard 3", Bruce Willis and Samuel L. Jackson were confronted with the following puzzle. They were given a 3-gallon jug and a 5-gallon jug and were asked to fill the 5-gallon jug with exactly 4 gallons. This problem generalizes that puzzle.
You have two jugs, A and B, and an infinite supply of water. There are three types of actions that you can use: (1) you can fill a jug, (2) you can empty a jug, and (3) you can pour from one jug to the other. Pouring from one jug to the other stops when the first jug is empty or the second jug is full, whichever comes first. For example, if A has 5 gallons and B has 6 gallons and a capacity of 8, then pouring from A to B leaves B full and 3 gallons in A.
A problem is given by a triple (Ca,Cb,N), where Ca and Cb are the capacities of the jugs A and B, respectively, and N is the goal. A solution is a sequence of steps that leaves exactly N gallons in jug B. The possible steps are
fill A
fill B
empty A
empty B
pour A B
pour B A
success
where "pour A B" means "pour the contents of jug A into jug B", and "success" means that the goal has been accomplished.
You may assume that the input you are given does have a solution.
Input
Input to your program consists of a series of input lines each defining one puzzle. Input for each puzzle is a single line of three positive integers: Ca, Cb, and N. Ca and Cb are the capacities of jugs A and B, and N is the goal. You can assume 0 < Ca <= Cb and N <= Cb <=1000 and that A and B are relatively prime to one another.
Output
Output from your program will consist of a series of instructions from the list of the potential output lines which will result in either of the jugs containing exactly N gallons of water. The last line of output for each puzzle should be the line "success". Output lines start in column 1 and there should be no empty lines nor any trailing spaces.
Sample Input
3 5 4
5 7 3
Sample Output
fill B
pour B A
empty A
pour B A
fill B
pour B A
success
fill A
pour A B
fill A
pour A B
empty B
pour A B
success
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
#define MAX 10
int ca, cb, cn;
int mask[1000][1000];
bool dfs(int a, int b, int count){
if(b == cn)
return true;
if(count >= MAX)
return false;
if(mask[a][b])
return false;
mask[a][b] = 1;
if(a<ca && dfs(ca, b, count+1))
printf("fill A\n");
else if(b<cb && dfs(a, cb, count+1))
printf("fill B\n");
else if(a>0 && dfs(0, b, count+1))
printf("empty A\n");
else if(b>0 && dfs(a, 0, count+1))
printf("empty B\n");
else if(a>=cb-b && dfs(a-(cb-b), cb, count+1))
printf("pour A B\n");
else if(a<cb-b && dfs(0, b+a, count+1))
printf("pour A B\n");
else if(b>=ca-a && dfs(ca, b-(ca-a), count+1))
printf("pour B A\n");
else if(b<ca-a && dfs(a+b, 0, count+1))
printf("pour B A\n");
mask[a][b] = 0;
return false;
}
int main(){
freopen("in.txt", "r", stdin);
while(cin>>ca>>cb>>cn){
memset(mask, 0, sizeof(mask));
if(dfs(0, 0, 1))
printf("success\n");
}
fclose(stdin);
}
...全文
相关推荐
实验5-二叉搜索树的基本操作(大作业).pdf
实验5-二叉搜索树的基本操作(大作业).pdf实验5-二叉搜索树的基本操作(大作业).pdf
Win11任务栏搜索Bug修复程序升级版
Win11任务栏搜索Bug修复程序升级版,这次它有一个小界面。原版无界面下载地址:https://download.csdn.net/download/m0_58767764/80037869
Bug-Hunter:chrome扩展程序,供开发人员轻松搜索堆栈溢出!
Bug猎人 chrome扩展,适用于开发人员寻找错误。
jira bug系统
jira6。0的bug方面的管理系统的使用说明文档。java开发的开源系统。
QuantumGrid4原版中不能不能搜索汉字的Bug
QuantumGrid4 cxGustomData.pas ->修正了原版中不能Incsearch不能搜索汉字的Bug


