您的位置:首页 > 其它

poj Pots(BFS)(路径记录)(模拟)

2017-01-05 10:56 232 查看
Pots

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 ≤ ≤ 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 AB, 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,
4000
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)


题意:
有两个水壶,容积分别为A,B,有三种操作:
1.FILL(i)     往水壶i中加满水;
2.DROP(i)  将水壶i中的水全部倒出来;
3.POUR(i,j)  将水壶i中的水倒入j中,不可洒出;
问,最终能不能使其中任意一个水壶中的水的体积为C,若能,则输出最少的倒水次数,若不能,则输出impossible
(两个水壶中的水初始都为0)
思路:
数组模拟倒水过程+回溯输出。
(vis[100][100]记录当前状态是否已被使用)
代码:
#include<stdio.h>
#include<string.h>
int a,b,c,flag;//flag标记是否成功找到结果
struct node
{
char s[20];
int x,y,behind,step;
} w[10100];
void print(int i)//回溯输出
{
if(w[i].behind!=-1)
{
print(w[i].behind);
printf("%s\n",w[i].s);
}
}
int vis[110][110];//记录状态
void bfs()
{
int head=0,tail=1;
w[0].x=0,w[0].y=0,w[0].behind=-1,w[0].step=0;
vis[0][0]=1;
while(head<tail)
{
node now=w[head];
//   printf("now.x=%d now.y=%d now.step=%d \n",now.x,now.y,now.step);
if(now.x==c||now.y==c)
{
flag=0;
printf("%d\n",now.step);
print(now.behind);
printf("%s\n",now.s);
break;
}
if(!vis[a][now.y]&&now.x==0)//FILL  a
{
vis[a][now.y]=1;
w[tail].x=a;
strcpy(w[tail].s,"FILL(1)");
w[tail].behind=head;
w[tail].step=now.step+1;
w[tail].y=now.y;
tail++;
}
if(now.x==a)
{
if(!vis[0][now.y])//DROP a
{
vis[0][now.y]=1;
w[tail].x=0;
strcpy(w[tail].s,"DROP(1)");
w[tail].behind=head;
w[tail].step=now.step+1;
w[tail].y=now.y;
tail++;
}
if(now.y!=b)//POUR(1,2)
{
if(now.x>(b-now.y)&&!vis[now.x-b+now.y][b])
{
vis[now.x-b+now.y][b]=1;
w[tail].x=now.x-(b-now.y);
w[tail].y=b;
w[tail].behind=head;
w[tail].step=now.step+1;
strcpy(w[tail].s,"POUR(1,2)");
tail++;
}
else if(now.x<=(b-now.y)&&!vis[0][now.x+now.y])
{
vis[0][now.x+now.y]=1;
w[tail].x=0;
w[tail].y=now.y+now.x;
w[tail].behind=head;
w[tail].step=now.step+1;
strcpy(w[tail].s,"POUR(1,2)");
tail++;
}
}
}
else if(now.x>0&&now.x<a)
{
if(!vis[a][now.y])//FILL 1
{
vis[a][now.y]=1;
w[tail].x=a;
strcpy(w[tail].s,"FILL(1)");
w[tail].behind=head;
w[tail].step=now.step+1;
w[tail].y=now.y;
tail++;
}
if(!vis[0][now.y])//DROP 1
{
vis[0][now.y]=1;
w[tail].x=0;
strcpy(w[tail].s,"DROP(1)");
w[tail].behind=head;
w[tail].step=now.step+1;
w[tail].y=now.y;
tail++;
}
if(now.y!=b)
{
if(now.x>(b-now.y)&&!vis[now.x-b+now.y][b])//POUR(1,2)
{
vis[now.x-b+now.y][b]=1;
w[tail].x=now.x-(b-now.y);
w[tail].y=b;
w[tail].behind=head;
w[tail].step=now.step+1;
strcpy(w[tail].s,"POUR(1,2)");
tail++;
}
else if(now.x<=(b-now.y)&&!vis[0][now.x+now.y])//POUR(1,2)
{
vis[0][now.x+now.y]=1;
w[tail].x=0;
w[tail].y=now.y+now.x;
w[tail].behind=head;
w[tail].step=now.step+1;
strcpy(w[tail].s,"POUR(1,2)");
tail++;
}
}
}
if(now.y==0&&!vis[now.x][b])//FILL 2
{
vis[now.x][b]=1;
w[tail].y=b;
strcpy(w[tail].s,"FILL(2)");
w[tail].behind=head;
w[tail].step=now.step+1;
w[tail].x=now.x;
tail++;
}
else if(now.y==b)
{
if(!vis[now.x][0])//DROP 2
{
vis[now.x][0]=1;
w[tail].y=0;
strcpy(w[tail].s,"DROP(2)");
w[tail].behind=head;
w[tail].step=now.step+1;
w[tail].x=now.x;
tail++;
}
if(now.x!=a)
{
if(now.y>(a-now.x)&&!vis[a][now.y-a+now.x])//POUR (2,1)
{
vis[a][now.y-a+now.x]=1;
w[tail].y=now.y-(a-now.x);
w[tail].x=a;
w[tail].behind=head;
w[tail].step=now.step+1;
strcpy(w[tail].s,"POUR(2,1)");
tail++;
}
else if(now.y<=(a-now.x)&&!vis[now.x+now.y][0])//POUR (2,1)
{
vis[now.x+now.y][0]=1;
w[tail].y=0;
w[tail].x=now.y+now.x;
w[tail].behind=head;
w[tail].step=now.step+1;
strcpy(w[tail].s,"POUR(2,1)");
tail++;
}
}
}
else if(now.y>0&&now.y<b)
{
if(!vis[now.x][b])//FILL 2
{
vis[now.x][b]=1;
w[tail].y=b;
strcpy(w[tail].s,"FILL(2)");
w[tail].behind=head;
w[tail].step=now.step+1;
w[tail].x=now.x;
tail++;
}
if(!vis[now.x][0])//DROP 2
{
vis[now.x][0]=1;
w[tail].y=0;
strcpy(w[tail].s,"DROP(2)");
w[tail].behind=head;
w[tail].step=now.step+1;
w[tail].x=now.x;
tail++;
}

if(now.x!=a)
{
if(now.y>(a-now.x)&&!vis[a][now.y-a+now.x])//POUR (2,1)
{
vis[a][now.y-a+now.x]=1;
w[tail].y=now.y-(a-now.x);
w[tail].x=a;
w[tail].behind=head;
w[tail].step=now.step+1;
strcpy(w[tail].s,"POUR(2,1)");
tail++;
}
else if(now.y<=(a-now.x)&&!vis[now.x+now.y][0])//POUR (2,1)
{
vis[now.x+now.y][0]=1;
w[tail].y=0;
w[tail].x=now.y+now.x;
w[tail].behind=head;
w[tail].step=now.step+1;
strcpy(w[tail].s,"POUR(2,1)");
tail++;
}
}
}
head++;
}
}
int main()
{
while(~scanf("%d%d%d",&a,&b,&c))
{
memset(vis,0,sizeof(vis));
flag=1;
bfs();
if(flag)
printf("impossible\n");
}
return 0;
}
ps:弄了一上午,复制粘贴的手i好疼~Orz
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: