您的位置:首页 > 其它

20124330102 【 搜索 -- 深度优先搜索 】 POJ 3414 Pots

2014-08-28 13:41 691 查看



Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board

Home Page

F.A.Qs

Statistical Charts
Problems

Submit Problem

Online Status

Prob.ID:
Register

Update your info

Authors ranklist

Current Contest

Past Contests

Scheduled Contests

Award Contest
wilson1068 Log
Out

Mail:0(0)

Login Log Archive
Language:
Default

Pots

Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 9997Accepted: 4198Special Judge
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)

Source

Northeastern Europe 2002, Western Subregion
[Submit] [Go Back] [Status]
[Discuss]


Home Page

Go
Back

To top

All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di

Any problem, Please Contact Administrator

深度优先搜索,只是多了个记录状态。

这里用 字符串 记录每次状态。。

#include <stdio.h>
#include <string.h>
#include <string>
#include <queue>
using namespace std;
#define MAXN 111
int m[MAXN][MAXN];
int a, b, c;

struct P{
int x, y;
string s;
};

P point(int x, int y, string s){
P ret;
ret.x = x,	ret.y = y,	ret.s = s;
return ret;
}

string bfs(){
queue<P> q;
P k = point(0, 0, ""), t;
memset(m, 0, sizeof(m));
m[k.x][k.y] = 1;
q.push(k);
while( !q.empty() ){
k = q.front();	q.pop();
if( !m[a][k.y] )	m[a][k.y] = 1,	q.push( point(a, k.y, k.s+'a') );
if( !m[k.x][b] )	m[k.x][b] = 1,	q.push( point(k.x, b, k.s+'b') );
if( !m[0][k.y] )	m[0][k.y] = 1,	q.push( point(0, k.y, k.s+'A') );
if( !m[k.x][0] )	m[k.x][0] = 1,	q.push( point(k.x, 0, k.s+'B') );
int kk = k.x+k.y;
int tx = kk < a ? kk : a;
int ty = kk < b ? kk : b;
if( !m[tx][kk-tx] )
if( tx==c || kk-tx==c )	return k.s+'1';
else			m[tx][kk-tx]=1,	q.push( point(tx, kk-tx, k.s+'1') );
if( !m[kk-ty][ty] )
if( kk-ty==c || ty==c )	return k.s+'2';
else			m[kk-ty][ty]=1,	q.push( point(kk-ty, ty, k.s+'2') );
}
return "";
}

int main(){
while( EOF != scanf("%d%d%d", &a, &b, &c) ){
if( !c ){	printf("1\n");	continue;	}
if( a==c ){	printf("1\nFILL(1)");	continue;	}
if( b==c ){	printf("1\nFILL(2)");	continue;	}
string ans = bfs();
if( ans.length() ){
printf("%d\n", ans.length());
for(int i=0; ans[i]; i++){
if( 'a'==ans[i] || 'b'==ans[i] )
printf("FILL(%d)\n", ans[i]-'a'+1);
else if( 'A'==ans[i] || 'B'==ans[i] )
printf("DROP(%d)\n", ans[i]-'A'+1);
else
printf("POUR(%d,%c)\n", '3'-ans[i], ans[i]);
}
}
else	printf("impossible\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: