poj 3414 pots

来源:互联网 发布:linux系统启动过程分析 编辑:程序博客网 时间:2024/05/16 12:45

本题的大意是说:给你两个容器a,b,然后给你需要倒出的体积c,问你能几步将所需的体积倒出来。

 

基本的做法就是用BFS,模拟倒酒的每一个过程。

 

在倒酒的过程中,只有六种状态:从外界倒入a;从外界倒入b;将a酒杯倒空;将b酒杯倒空;把酒从a酒杯倒入b;把酒从b酒杯倒入a。

 

判断每一种状态是不是都出现过,如果没有出现就将它写入队列中,并将出现过的状态标记下来。

 

本题主要是打印路径的问题,关于这个问题我困扰了将近一天,最后还是看了别人的思路写下来的,惭愧啊!

 

 

Pots

 

Description

You are given two pots, having the volume of A and B liters respectively. The following operations can be performed:

  1. FILL(i)        fill the pot i (1 ≤ i ≤ 2) from the tap;
  2. DROP(i)      empty the pot i to the drain;
  3. 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

6FILL(2)POUR(2,1)DROP(1)POUR(2,1)FILL(2)POUR(2,1)
下面是我的代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
struct node
{
  int a,b;
  int step;
};
//用来记录每一步过程 
struct N
{
  int pre;
  int a,b;
};
//path[i][j]={pre,a,b}代表i,j的前件是a,b,方法是pre 
struct N path[105][105];
int a,b,c,flag;
int vis[105][105];
struct node now,temp;
int bfs()
{
  queue<struct node> q;
  now.a=0,now.b=0,now.step=0;
  path[now.a][now.b].a=0;
  path[now.a][now.b].b=0;
  path[now.a][now.b].pre=0;
  vis[now.a][now.b]=1;
  q.push(now);
  while(!q.empty())
  {
    now=q.front(); q.pop();
    if(now.a==c||now.b==c)
    {
      //如果现在a容器是满的,则标记为1
      if(now.a==c)
      {
        flag=1;
        return now.b;
      }
     
      //如果b容器是满的,则标记为2
      else if(now.b==c)
      {
        flag=2;
        return now.a;
      }
    }
    int min1=now.a>(b-now.b)?(b-now.b):now.a;
    int min2=(a-now.a)>now.b?now.b:(a-now.a);
    if(!vis[now.a][b])//将酒杯b倒满,标记为状态1
    {
      temp.a=now.a;
      temp.b=b;
      temp.step=now.step+1;
     
      path[temp.a][temp.b].pre=1;
      path[temp.a][temp.b].a=now.a;
      path[temp.a][temp.b].b=now.b;
     
      vis[temp.a][temp.b]=1;
      q.push(temp);
    }
    if(!vis[a][now.b])//将酒杯a倒满,标记为2
    {
      temp.a=a;
      temp.b=now.b;
      temp.step=now.step+1;
      path[temp.a][temp.b].pre=2;
      path[temp.a][temp.b].a=now.a;
      path[temp.a][temp.b].b=now.b;
      vis[temp.a][temp.b]=1;
      q.push(temp);
    }
    if(!vis[0][now.b])//将酒杯a倒空,标记为3
    {
      temp.a=0;
      temp.b=now.b;
      temp.step=now.step+1;
     
      path[temp.a][temp.b].pre=3;
      path[temp.a][temp.b].a=now.a;
      path[temp.a][temp.b].b=now.b;
     
      vis[temp.a][temp.b]=1;
      q.push(temp);
    }
    if(!vis[now.a][0])//将酒杯b倒空,标记为4
    {
      temp.a=now.a;
      temp.b=0;
      temp.step=now.step+1;
     
      path[temp.a][temp.b].pre=4;
      path[temp.a][temp.b].a=now.a;
      path[temp.a][temp.b].b=now.b;
     
      vis[temp.a][temp.b]=1;
      q.push(temp);
    }
    if(!vis[now.a-min1][now.b+min1])//将酒杯a中的酒倒入酒杯b中,标记为5
    {
      temp.a=now.a-min1;
      temp.b=now.b+min1;
      temp.step=now.step+1;
     
      path[temp.a][temp.b].pre=5;
      path[temp.a][temp.b].a=now.a;
      path[temp.a][temp.b].b=now.b;
     
      vis[temp.a][temp.b]=1;
      q.push(temp);
    }
    if(!vis[now.a+min2][now.b-min2])//将酒杯b中的酒倒入酒杯a中,标记为6
    {
      temp.a=now.a+min2;
      temp.b=now.b-min2;
      temp.step=now.step+1;
     
      path[temp.a][temp.b].pre=6;
      path[temp.a][temp.b].a=now.a;
      path[temp.a][temp.b].b=now.b;     
     
      vis[temp.a][temp.b]=1;
      q.push(temp);
    }
  }
  return -1;
}
//递归打印路径 
void print_path(int a,int b)
{
  int s,t;
  if(path[a][b].pre!=0)
  {
    s=path[a][b].a;
    t=path[a][b].b;
    print_path(s,t);
  }
 
  if(path[a][b].pre==1)
    printf("FILL(2)/n");
  else if(path[a][b].pre==2)
    printf("FILL(1)/n");
  else if(path[a][b].pre==3)
    printf("DROP(1)/n");
  else if(path[a][b].pre==4)
    printf("DROP(2)/n");
  else if(path[a][b].pre==5)
    printf("POUR(1,2)/n");
  else if(path[a][b].pre==6)
    printf("POUR(2,1)/n"); 
}
int main()
{
  int i,j,k;
 
  scanf("%d%d%d",&a,&b,&c);
  memset(vis,0,sizeof(vis));
  int ans=bfs();
  if(ans==-1)
    printf("impossible/n");
  else
  {
    j=0;
    printf("%d/n",now.step);
   
    if(flag==1)
      print_path(c,ans);
    else
      print_path(ans,c);
  }
 
  return 0;
}