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 ...
随机推荐
- 【2019 Multi-University Training Contest 4】
01:https://www.cnblogs.com/myx12345/p/11644076.html 02: 03:https://www.cnblogs.com/myx12345/p/116478 ...
- C#读取csv、xls、sql数据库的实现
using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Secu ...
- git 小错误
(一)在本地直接修改文件,提交后出现(master|REBASE 1/2).由于文件冲突所以导致各种报错. 在git pull --rebase origin master后 error: Pulli ...
- 建站手册-浏览器信息:Mozilla Firefox 浏览器
ylbtech-建站手册-浏览器信息:Mozilla Firefox 浏览器 1.返回顶部 1. http://www.w3school.com.cn/browsers/browsers_firefo ...
- linux svn 服务器搭建问题
我的svn版本 svn, version 1.7.14 (r1542130) compiled Nov 20 2015, 19:25:09 Copyright (C) 2013 The Apache ...
- left join right inner join 区别
连表查询 select a, b, c from table_a tb_a left (right) join table_b tb_b on tb_a.id = tb_b.id left : tab ...
- Java + selenium 元素定位(4)之By CSS
这篇我要介绍元素定位的倒数第二个方法啦,就是基于CSS的元素定位.关于一些CSS的知识,我这里就不累赘的讲了,以后可能会单独写一篇关于CSS的介绍.当然个人推荐如果之前完全没有CSS只是储备的,可以选 ...
- LeetCode 最短无序连续子数组
题目链接:https://leetcode-cn.com/problems/shortest-unsorted-continuous-subarray/ 题目大意: 略. 分析: 如果排序区间为 [L ...
- 兼容ie浏览器的方法
让IE6 IE7 IE8 IE9 IE10 IE11支持Bootstrap的解决方法 最近做一个Web网站,之前一直觉得bootstrap非常好,这次使用了bootstrap3,在chrome,f ...
- C语言博客作业04
问题|答案 -|:-:|-: 这个作业属于哪个课程|c语言程序设计I 这个作业的要求在哪里|https://edu.cnblogs.com/campus/zswxy/CST2019-4/homewor ...