poj 3414( 搜索 )

poj 3414( 搜索 ),第1张

概述Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6517   Accepted: 2732   Special Judge Description You are given two pots, having the volume of A and B liters respectively. The follow Pots
Time limit:1000MS Memory limit:65536K
Total Submissions:6517 Accepted:2732 Special Judge

Description

You are given two pots,having the volume ofAandBliters respectively. The following operations can be performed:

FILL(i) fill the poti(1 ≤i≤ 2) from the tap; DROP(i) empty the potito the drain; POUR(i,j) pour from potito potj; after this operation either the potjis full (and there may be some water left in the poti),or the potiis empty (and all its contents have been moved to the potj).

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)

Source

Northeastern Europe 2002,Western Subregion
题目类型:搜索 题目描述:略 题目分析:略 代码如下:
#include <stdio.h>#include <string.h>#define N 101struct Way {    int prex;    int prey;    int kind;} way[N][N];struct Point{    int x;    int y;} queue[N*N*2],c,r;int answer[N*N];int index = -1;int visit[N][N];int va,vb,vc;voID show(){    int i;    char temp[6][10] = { "FILL(1)","FILL(2)","DROP(1)","DROP(2)","POUR(1,2)","POUR(2,1)"};    printf("%d\n",index+1);    for( i = index; i >= 0; i-- ){        printf("%s\n",temp[answer[i]]);    }}int bfs(){    int top = 0,end = 0,i;    index = -1;    memset(visit,sizeof(visit));    visit[0][0] = 1;    queue[0].x = 0;    queue[0].y = 0;    while(top <= end){        c = queue[top];        for( i = 0; i < 6; i++){            switch( i ){                case 0: r.x = va; r.y = c.y;      break; //fill 1                case 1: r.x = c.x; r.y = vb;      break; //fill 2                case 2: r.x = 0; r.y = c.y;       break; //drop 1                case 3: r.x = c.x; r.y = 0;       break; //drop 2                case 4:                                  //pour 1 2                        if( c.x + c.y <= vb) {                            r.x = 0;                            r.y = c.x + c.y;                        } else {                            r.x = c.x - (vb - c.y);                            r.y = vb;                        }                        break;                case 5:                                  //pour 2 1                        if( c.x + c.y <= va){                            r.x = c.x + c.y;                            r.y = 0;                        } else {                            r.x = va;                            r.y = c.y - (va - c.x);                        }                        break;            }            if( visit[r.x][r.y] == 0){                if( r.x == vc || r.y == vc){                    answer[++index] = i;                    int nx = c.x;                    int ny = c.y;                    int prex,prey;                    while( nx != 0 || ny != 0){                        answer[++index] = way[nx][ny].kind;                        prex = way[nx][ny].prex;                        prey = way[nx][ny].prey;                        nx = prex;                        ny = prey;                    }                    return 1;                } else {                    visit[r.x][r.y] = 1;                    queue[++end] = r;                    way[r.x][r.y].prex = c.x;                    way[r.x][r.y].prey = c.y;                    way[r.x][r.y].kind = i;                }            }        }        top++;    }    return -1;}int main(){    while(scanf("%d%d%d",&va,&vb,&vc) != EOF){        if(bfs() == -1){            printf("impossible\n");        } else {            show();        }    }    return 0;}
总结

以上是内存溢出为你收集整理的poj 3414( 搜索 )全部内容,希望文章能够帮你解决poj 3414( 搜索 )所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1285810.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-09
下一篇 2022-06-09

发表评论

登录后才能评论

评论列表(0条)

保存