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 ;
}
POJ 3414 Pots (dfs,这个代码好长啊QAQ)的更多相关文章
- BFS POJ 3414 Pots
		
题目传送门 /* BFS:六种情况讨论一下,BFS轻松解决 起初我看有人用DFS,我写了一遍,TLE..还是用BFS,结果特判时出错,逗了好长时间 看别人的代码简直是受罪,还好自己终于发现自己代码的小 ...
 - POJ 3414 Pots(罐子)
		
POJ 3414 Pots(罐子) Time Limit: 1000MS Memory Limit: 65536K Description - 题目描述 You are given two po ...
 - poj 3414 Pots 【BFS+记录路径 】
		
//yy:昨天看着这题突然有点懵,不知道怎么记录路径,然后交给房教了,,,然后默默去写另一个bfs,想清楚思路后花了半小时写了120+行的代码然后出现奇葩的CE,看完FAQ改了之后又WA了.然后第一次 ...
 - 广搜+输出路径 POJ 3414 Pots
		
POJ 3414 Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13547 Accepted: 5718 ...
 - POJ 3414 Pots (BFS/DFS)
		
Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7783 Accepted: 3261 Special Ju ...
 - POJ 3414 Pots
		
Pots Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Status ...
 - poj 3414 Pots【bfs+回溯路径 正向输出】
		
题目地址:http://poj.org/problem?id=3414 Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions ...
 - poj 3414 Pots(广搜BFS+路径输出)
		
转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:id=3414">http://poj.org/probl ...
 - POJ 3414 Pots【bfs模拟倒水问题】
		
链接: http://poj.org/problem?id=3414 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22009#probl ...
 
随机推荐
- php strtok()函数 语法
			
php strtok()函数 语法 作用:逐一分割字符串大理石构件 语法:strtok(string,split) 参数: 参数 描述 string 必需.规定要分割的字符串. split 必需.规定 ...
 - php fmod()函数 语法
			
php fmod()函数 语法 作用:fmod()函数的作用是两个数值做除法运算后的余数 语法:fmod(X,Y).大理石平台哪家好 参数: 参数 描述 X 必须,X为除数 Y 必须,被除数,如果Y为 ...
 - 9.28 linux系统基础优化
			
关闭SELinux(是美国安全局对强制访问的实现)功能 [root@wen ~]# sed -i 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/selin ...
 - delphi 异形窗体可半透明
			
unit xDrawForm; interface uses Windows, Messages, SysUtils, Classes, Controls, Forms, Menus, Graphic ...
 - appium常见问题04_查看andriod内置浏览器webview版本
			
方法一:手机上设置中查看 设置-->应用程序管理-->全部-->Android System WebView 方法二:adb指令查看(前提,已安装android sdk环境) 1,w ...
 - USACO 6.4 章节
			
The Primes 题目大意 5*5矩阵,给定左上角 要所有行,列,从左向右看对角线为质数,没有前导零,且这些质数数位和相等(题目给和) 按字典序输出所有方案... 题解 看上去就是个 无脑暴搜 题 ...
 - WireShark 自带工具 editcap 和 text2pcap 配合完成改包操作
			
一.拆包 首先声明这种方法比较复杂而且需要点技术水平,不建议菜鸟尝试(可以使用WireEdit编辑pcap包,不过要联网)其实在熟练这种方法后也可以很快的,但这种方法主要还是方便吧,不用下载其他什么软 ...
 - luogu P4183 Cow at Large P (暴力吊打点分治)(内有时间复杂度证明)
			
题面 贝茜被农民们逼进了一个偏僻的农场.农场可视为一棵有N个结点的树,结点分别编号为 1,2,-,N .每个叶子结点都是出入口.开始时,每个出入口都可以放一个农民(也可以不放).每个时刻,贝茜和农民都 ...
 - ubuntu中下载pycharm并添加到桌面
			
方法一:下载Pycharm与安装 下载地址:https://www.jetbrains.com/pycharm/ Pycharm专业版和社区版对大多数人来说差别不大,区别如下: 我们下载Linux的社 ...
 - spring集成rabbitMq(非springboot)
			
首先 , pom文件需要加入spring集成rabbitMq的依赖: <dependency> <groupId>org.springframework.amqp</gr ...