1. 程式人生 > >POJ-3414-Pots(BFS 模擬)

POJ-3414-Pots(BFS 模擬)

H - Pots
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Submit

Status

Practice

POJ 3414
Appoint description:
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)

程式碼

#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<string.h>
#include<queue>
#include<math.h>
using namespace std;
struct node
{
    int A;
    int B;
    int steap;
    char map[10000
][10];//記錄操作過程 }; int is_vis[105][105];//已出現過的組合標記為1 int A; int B;//已有杯子的容量 int C;//目標水量 void BFS() { memset(is_vis,0,sizeof(is_vis)); is_vis[0][0]=1;//初始化標記為1 queue<node>q; node star; star.A=0; star.B=0; star.steap=0; q.push(star); while(!q.empty()) { star=q.front(); q.pop(); if(star.A==C||star.B==C) { //printf("%d %d\n",star.A,star.B); printf("%d\n",star.steap); for(int i=1; i<=star.steap; i++) printf("%s\n",star.map[i]); return; } node end; if(star.A==0)//如果A是空的,那隻能把它填滿了 { end=star; end.A=A; end.steap++; strcpy(end.map[end.steap],"FILL(1)");//注意從1開始輸出字串 if(is_vis[end.A][end.B]==0)//如果這種狀態沒出現過,那就可以入隊 { is_vis[end.A][end.B]=1; q.push(end);//建立一個子節點 } } if(star.A>0)//如果A有剩餘,或者壓根就是滿的 { end=star; end.A=0;//清空A作為一個結點 end.steap++; strcpy(end.map[end.steap],"DROP(1)"); if(is_vis[end.A][end.B]==0)//如果這種狀態沒出現過,那就可以入隊 { // printf("***\n"); is_vis[end.A][end.B]=1; q.push(end);//建立一個子節點 } if(star.B<B)//B不滿,那就可以把A倒給B { end=star;//下面A和B的計算需要注意 if(star.A<=B-star.B)//如果A的水在B中倒不滿 { end.A=0; end.B=star.A+star.B; } else//A的水無法全部倒入B { end.A=star.A-(B-star.B); end.B=B; } end.steap++; strcpy(end.map[end.steap],"POUR(1,2)"); if(is_vis[end.A][end.B]==0)//如果這種狀態沒出現過,那就可以入隊 { is_vis[end.A][end.B]=1; q.push(end);//建立一個子節點 } } } if(star.B==0)//如果B是空的,那也只能把它填滿並建立一個子節點 { end=star; end.B=B;//充滿B end.steap++; strcpy(end.map[end.steap],"FILL(2)"); if(is_vis[end.A][end.B]==0)//如果這種狀態沒出現過,那就可以入隊 { is_vis[end.A][end.B]=1; q.push(end);//建立一個子節點 } } if(star.B>0) { end=star; end.B=0;//清空B作為一個結點 end.steap++; strcpy(end.map[end.steap],"DROP(2)"); if(is_vis[end.A][end.B]==0)//如果這種狀態沒出現過,那就可以入隊 { is_vis[end.A][end.B]=1; q.push(end);//建立一個子節點 } if(star.A<A)//A不滿,那就可以把B倒給A { end=star;//下面A和B的計算需要注意 if(star.B<=A-star.A)//如果B的水在A中倒不滿 { end.A=star.B+star.A; end.B=0; } else//B的水無法全部倒入A { end.A=A; end.B=star.B-(A-star.A); } end.steap=star.steap+1; strcpy(end.map[end.steap],"POUR(2,1)"); if(is_vis[end.A][end.B]==0)//如果這種狀態沒出現過,那就可以入隊 { is_vis[end.A][end.B]=1; q.push(end);//建立一個子節點 } } } } printf("impossible\n"); } int main() { while(~scanf("%d%d%d",&A,&B,&C)) { BFS(); } return 0; } /* 簡單點 出題的方式簡單點 遞進的大題請省略 你又不是教研員 別設計那些情節 沒意見 我只想看看你怎麼編 你出的題太表面 像有套路的考卷 考生一眼能看見 該配合你得分的我演視而不見 在逼一個最愛你的學生撕掉試卷 什麼時候我們開始收起及格線 順應時代的謊言做那些沒用的試卷 可你曾經那麼愛我幹嘛加難試卷 我該考成什麼樣子才能快點畢業 原來當我交出考卷後的這些那些 才是考驗 */