POJ 3414 Pots (dfs,这个代码好长啊QAQ)
Description
You are given two pots, having the volume of A and B liters respectively. The following operations can be performed:
- FILL(i) fill the pot i (1 ≤ i ≤ 2) from the tap;
- DROP(i) empty the pot i to the drain;
- POUR(i,j) pour from pot i to pot j; after this operation either the pot j is full (and there may be some water left in the pot i), or the pot i is empty (and all its contents have been moved to the pot j).
Write a program to find the shortest possible sequence of these operations that will yield exactly C liters of water in one of the pots.
Input
On the first and only line are the numbers A, B, and C. These are all integers in the range from 1 to 100 and C≤max(A,B).
Output
The first line of the output must contain the length of the sequence of operations K. The following K lines must each describe one operation. If there are several sequences of minimal length, output any one of them. If the desired result can’t be achieved, the first and only line of the file must contain the word ‘impossible’.
Sample Input
3 5 4
Sample Output
6
FILL(2)
POUR(2,1)
DROP(1)
POUR(2,1)
FILL(2)
POUR(2,1) 给你两个壶,容量a,b,让你三种操作 1.接满 2.一个壶倒到另一个壶(这个壶倒空了或者那个壶满了 才停止) 3.把一个壶倒空 问你几步两个壶中能有一个壶容量是能c。
这个题显然bfs,就是node结构体里面在加上一个queue和string保存下之前的状态。没啥了,代码好长啊QAQ。
代码如下:
#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <cmath>
#include <cstdio>
#include <queue>
#include <vector>
using namespace std;
struct node
{
int na,nb,step;
vector<int> num;
string ns;
};
bool vis [][];
int a,b,c;
bool iffind;
void init ()
{
memset(vis,false,sizeof vis);
iffind=false;
}
void bfs(node now)
{
queue<node>q;
q.push(now);
while (!q.empty())
{
node temp=q.front();
q.pop();
if (vis[temp.na][temp.nb])
continue ;
vis[temp.na][temp.nb]=true;
if (temp.na==c||temp.nb==c)//success
{
iffind=true;
printf("%d\n",temp.step);
//printf("=== %d %d\n",sizeof(temp.ns),temp.num.size());
for (int i=;i<temp.num.size();++i)
{
if (temp.ns[i]=='F')
{
printf("FILL");
if (temp.num[i]==)
printf("(1)\n");
else
printf("(2)\n");
}
else if (temp.ns[i]=='P')
{
printf("POUR");
if (temp.num[i]==)
printf("(1,2)\n");
else
printf("(2,1)\n");
}
else
{
printf("DROP");
if (temp.num[i]==)
printf("(1)\n");
else
printf("(2)\n");
}
}
return ;
}
if (temp.na!=a)//fill a
{
node now=temp;
now.step++;
now.na=a;
now.ns+="F";
now.num.push_back();
q.push(now);
}
if (temp.nb!=b)//fill b
{
node now=temp;
now.step++;
now.nb=b;
now.ns+="F";
now.num.push_back();
q.push(now);
}
if (temp.na>&&temp.nb<b)//pour a to b
{
node now=temp;
now.step++;
now.na-=min(temp.na,b-temp.nb);
now.nb+=min(temp.na,b-temp.nb);
now.ns+="P";
now.num.push_back();
q.push(now);
}
if (temp.nb>&&temp.na<a)//pour b to a
{
node now=temp;
now.step++;
now.nb-=min(temp.nb,a-temp.na);
now.na+=min(temp.nb,a-temp.na);
now.ns+="P";
now.num.push_back();
q.push(now);
}
if(temp.na!=)// drop a
{
node now=temp;
now.step++;
now.na=;
now.ns+='D';
now.num.push_back();
q.push(now);
}
if(temp.nb!=)//drop b
{
node now=temp;
now.step++;
now.nb=;
now.ns+='D';
now.num.push_back();
q.push(now);
}
}
return ;
}
int main()
{
while (~scanf("%d%d%d",&a,&b,&c))
{
init();
node x;
x.na=,x.nb=,x.ns="",x.step=,x.num.clear();
bfs(x);
if (iffind==false){
printf("impossible\n");
}
}
return ;
}
最新文章
- C语言 &#183; 乘法表
- Hadoop入门学习笔记---part1
- css中单位em和rem
- next_permutation 和 一个不成功的案例
- DataGridView 控件用法(可能不是很全面,因为这是自己常常用到的一些小总结):
- 降低IIScup使用率,提高性能
- 得到bundle seed id
- 【Linux】鸟哥的Linux私房菜基础学习篇整理(九)
- wordpress参考网站
- DEV GridControl导出到Excel或打印
- 转换器2:ThinkPhp模板转Django模板
- CodeForces 242E二维线段树
- 前端Web开发MVC模式-入门示例
- 标准的SQL语句类型
- MySQL8.0.x免安装配置
- B/S架构与C/S架构
- NPOI 设置样式为粗体
- javac编译提示错误需要为 class、interface 或 enum
- SED命令用法整理
- banner 跟随鼠标呈现视差效果