Poj 3414 Pots (BFS) (simple question)

Source: Internet
Author: User

Poj 3414 Pots (BFS) (simple question)

 

Pots
Time Limit:1000 MS   Memory Limit:65536 K
Total Submissions:11551   Accepted:4900   Special Judge

 

Description

You are given two pots, having the volumeAAndBLiters respectively. The following operations can be saved med:

FILL (I) fill the pot I(1 ≤ I≤ 2) from the tap; DROP (I) empty the pot ITo the drain; POUR (I, j) pour from pot ITo pot J; After this operation either the pot JIs full (and there may be some water left in the pot I), Or the pot IIs 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 exactlyCLiters of water in one of the pots.

Input

On the first and only line are the numbersA,B, AndC. These are all integers in the range from 1 to 100 andC≤ Max (A,B).

Output

The first line of the output must contain the length of the sequence of operationsK. The followingKLines 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)

Question:

Two cups, one spoon, the amount of water in the two cups changes through the spoon or water pouring operation, find the minimum number of steps required to change the initial state to the final state.

Ideas:

Bfs traversal, each time there are six changes, from the initial state change to the minimum number of steps required for the final state.

Code:

 

#include 
 
  #include 
  
   #define MAX 10001typedef struct Node{int a,b,step,pre,flag;};int A,B,C;Node queue[MAX];bool visit[101][101];int path[MAX],index;void bfs(){memset(visit,false,sizeof(visit));int front=0,rear=0;Node cur,next;cur.a=0,cur.b=0,cur.pre=-1,cur.step=0;visit[0][0]=true;queue[rear++]=cur;while(front!=rear){cur=queue[front++];if(cur.a==C||cur.b==C) break;for(int i=0;i<6;i++){switch(i){case 0: next.a=A; next.b=cur.b; next.flag=0; break;case 1:next.a=cur.a; next.b=B; next.flag=1; break;case 2:next.a=0; next.b=cur.b; next.flag=2; break;case 3:next.a=cur.a; next.b=0; next.flag=3; break;case 4:if(B-cur.b
   
    =0;){path[index++]=i;i=queue[i].pre;}printf(%d,queue[front-1].step);for(int i=index-1;i>=0;i--){switch(queue[path[i]].flag){case 0:printf(FILL(1));break;case 1:printf(FILL(2)); break;case 2:printf(DROP(1)); break;case 3:printf(DROP(2)); break;case 4:printf(POUR(1,2)); break;case 5:printf(POUR(2,1)); break;}}}int main(){scanf(%d %d %d,&A,&B,&C);bfs();return 0;}
   
  
 


 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.