Pots
Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 14033 | Accepted: 5908 | Special 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
经过多次操作后得到k;
bfs+dfs(查找路径超时)
超时代码
#include <iostream>
#include<queue>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<functional>
using namespace std;
int n,m,k;
struct node
{
int a,b;
int s;
};
struct note
{
char name[30];
} q[50000];
int vis[205][205];
int bfs()
{
memset(vis,0,sizeof(vis));
queue<node>q;
node t;
t.a=0;
t.b=0;
t.s=0;
q.push(t);
vis[0][0]=1;
node now,tmp;
while(!q.empty())
{
now=q.front();
q.pop();
int x=now.a;
int y=now.b;
int s=now.s;
// printf("%d %d %d \n",x,y,s);
if(now.a==k||now.b==k)
return now.s;
tmp.s=now.s+1;
if(x!=n)
{
tmp.a=0;
tmp.b=y;
if(!vis[tmp.a][tmp.b])
{
vis[tmp.a][tmp.b]=1;
q.push(tmp);
}
tmp.a=n;
tmp.b=y;
if(!vis[tmp.a][tmp.b])
{
vis[tmp.a][tmp.b]=1;
q.push(tmp);
}
if(y!=0)
{
if(x+y<n)
{
tmp.a=x+y;
tmp.b=0;
if(!vis[tmp.a][tmp.b])
{
vis[tmp.a][tmp.b]=1;
q.push(tmp);
}
}
else
{
tmp.a=n;
tmp.b=(y-(n-x));
if(!vis[tmp.a][tmp.b])
{
vis[tmp.a][tmp.b]=1;
q.push(tmp);
}
}
}
}
else
{
tmp.a=0;
tmp.b=y;
if(!vis[tmp.a][tmp.b])
{
vis[tmp.a][tmp.b]=1;
q.push(tmp);
}
if(y!=m)
{
if(x+y<m)
{
tmp.a=0;
tmp.b=x+y;
if(!vis[tmp.a][tmp.b])
{
vis[tmp.a][tmp.b]=1;
q.push(tmp);
}
}
else
{
tmp.a=x-(m-y);
tmp.b=m;
if(!vis[tmp.a][tmp.b])
{
vis[tmp.a][tmp.b]=1;
q.push(tmp);
}
}
}
}
if(y!=m)
{
tmp.a=x;
tmp.b=0;
if(!vis[tmp.a][tmp.b])
{
vis[tmp.a][tmp.b]=1;
q.push(tmp);
}
tmp.a=x;
tmp.b=m;
if(!vis[tmp.a][tmp.b])
{
vis[tmp.a][tmp.b]=1;
q.push(tmp);
}
if(x!=0)
{
if(x+y<m)
{
tmp.a=0;
tmp.b=x+y;
if(!vis[tmp.a][tmp.b])
{
vis[tmp.a][tmp.b]=1;
q.push(tmp);
}
}
else
{
tmp.a=x-(m-y);
tmp.b=m;
if(!vis[tmp.a][tmp.b])
{
vis[tmp.a][tmp.b]=1;
q.push(tmp);
}
}
}
}
else
{
tmp.a=x;
tmp.b=0;
if(!vis[tmp.a][tmp.b])
{
vis[tmp.a][tmp.b]=1;
q.push(tmp);
}
if(x!=n)
{
if(x+y<n)
{
tmp.a=x+y;
tmp.b=0;
if(!vis[tmp.a][tmp.b])
{
vis[tmp.a][tmp.b]=1;
q.push(tmp);
}
}
else
{
tmp.a=n;
tmp.b=(y-(n-x));
if(!vis[tmp.a][tmp.b])
{
vis[tmp.a][tmp.b]=1;
q.push(tmp);
}
}
}
}
}
return -1;
}
int ko;
int f;
void dfs(int x,int y,int s)
{
if(x==k||y==k)
{
f=1;
return ;
}
if(s>=ko)
{
return ;
}
//puts(q[s].name);
int xi,yi;
//if(x==0)
if(x!=n)
{
xi=n;
yi=y;
vis[xi][yi]=1;
strcpy(q[s].name,"FILL(1)");
dfs(xi,yi,s+1);
if(f)return ;
vis[xi][yi]=0;
if(y!=m)
{
if(x+y<m)
{
xi=0;
yi=x+y;
vis[xi][yi]=1;
strcpy(q[s].name,"POUR(1,2)");
dfs(xi,yi,s+1);
if(f)return ;
vis[xi][yi]=0;
}
else
{
xi=(x-m-(y));
yi=m;
vis[xi][yi]=1;
strcpy(q[s].name,"POUR(1,2)");
dfs(xi,yi,s+1);
if(f)return ;
vis[xi][yi]=0;
}
}
}
// if(y==0)
if(y!=m)
{
xi=x;
yi=m;
vis[xi][yi]=1;
strcpy(q[s].name,"FILL(2)");
dfs(xi,yi,s+1);
if(f)return ;
vis[xi][yi]=0;
if(x!=n)
{
if(x+y<n)
{
xi=x+y;
yi=0;
vis[xi][yi]=1;
strcpy(q[s].name,"POUR(2,1)");
dfs(xi,yi,s+1);
if(f)return ;
vis[xi][yi]=0;
}
else
{
xi=n;
yi=y-(n-x);
vis[xi][yi]=1;
strcpy(q[s].name,"POUR(2,1)");
dfs(xi,yi,s+1);
if(f)return ;
vis[xi][yi]=0;
}
}
}
if(x!=0)
{
xi=0;
yi=y;
vis[xi][yi]=1;
strcpy(q[s].name,"DROP(1)");
dfs(xi,yi,s+1);
if(f)return ;
vis[xi][yi]=0;
}
if(y!=0)
{
xi=x;
yi=0;
vis[xi][yi]=1;
strcpy(q[s].name,"DROP(2)");
dfs(xi,yi,s+1);
if(f)return ;
vis[xi][yi]=0;
}
if(x!=n&&y!=0)
{
if(x+y<n)
{
xi=x+y;
yi=0;
vis[xi][yi]=1;
strcpy(q[s].name,"POUR(2,1)");
dfs(xi,yi,s+1);
if(f)return ;
vis[xi][yi]=0;
}
else
{
xi=n;
yi=(y-(n-x));
strcpy(q[s].name,"POUR(2,1)");
dfs(xi,yi,s+1);
if(f)return ;
vis[xi][yi]=0;
}
}
if(x!=0&&y!=m)
{
if(x+y<m)
{
xi=0;
yi=x+y;
vis[xi][yi]=1;
strcpy(q[s].name,"POUR(1,2)");
dfs(xi,yi,s+1);
if(f)return ;
vis[xi][yi]=0;
}
else
{
xi=(x-(m-y));
yi=m;
vis[xi][yi]=1;
strcpy(q[s].name,"POUR(1,2)");
dfs(xi,yi,s+1);
if(f)return ;
vis[xi][yi]=0;
}
}
}
int main()
{
while(~scanf("%d%d%d",&n,&m,&k))
{
if(n<k&&m<k)
{
printf("impossible\n");
continue;
}
ko=bfs();
f=0;
if(ko==-1)
{
printf("impossible\n");
continue;
}
printf("%d\n",ko);
memset(vis,0,sizeof(vis));
dfs(0,0,0);
vis[0][0]=1;
// printf("%d\n",f);
for(int i=0; i<ko; i++)
puts(q[i].name);
}
}
AC代码
#include <iostream>
#include<queue>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<string><span style="color:#ff0000;"> //跟string.h不同</span>
#include<functional>
using namespace std;
int n,m,k;
struct node
{
int a,b;
int s;
string str; <span style="color:#ff0000;">//记录路径</span>
};
int vis[205][205];
int bfs()
{
memset(vis,0,sizeof(vis));
queue<node>q;
node t;
t.a=0;
t.b=0;
t.s=0;
q.push(t);
vis[0][0]=1;
node now,tmp;
while(!q.empty())
{
now=q.front();
q.pop();
int x=now.a;
int y=now.b;
int s=now.s;
// printf("%d %d %d \n",x,y,s);
// cout<<now.str<<endl;
if(now.a==k||now.b==k)
{
printf("%d",now.s);
//printf("%d %d\n",now.a,now.b);
cout<<now.str<<endl;
return now.s;
}
tmp.s=now.s+1;
if(x!=n)
{
tmp.a=0;
tmp.b=y;
if(!vis[tmp.a][tmp.b])
{
vis[tmp.a][tmp.b]=1;
tmp.str=now.str+"\nDROP(1)";
q.push(tmp);
}
tmp.a=n;
tmp.b=y;
if(!vis[tmp.a][tmp.b])
{
vis[tmp.a][tmp.b]=1;
tmp.str=now.str+"\nFILL(1)";
q.push(tmp);
}
if(y!=0)
{
if(x+y<n)
{
tmp.a=x+y;
tmp.b=0;
if(!vis[tmp.a][tmp.b])
{
vis[tmp.a][tmp.b]=1;
tmp.str=now.str+"\nPOUR(2,1)";
q.push(tmp);
}
}
else
{
tmp.a=n;
tmp.b=(y-(n-x));
if(!vis[tmp.a][tmp.b])
{
vis[tmp.a][tmp.b]=1;
tmp.str=now.str+"\nPOUR(2,1)";
q.push(tmp);
}
}
}
}
else
{
tmp.a=0;
tmp.b=y;
if(!vis[tmp.a][tmp.b])
{
vis[tmp.a][tmp.b]=1;
tmp.str=now.str+"\nDROP(1)";
q.push(tmp);
}
if(y!=m)
{
if(x+y<m)
{
tmp.a=0;
tmp.b=x+y;
if(!vis[tmp.a][tmp.b])
{
vis[tmp.a][tmp.b]=1;
tmp.str=now.str+"\nPOUR(1,2)";
q.push(tmp);
}
}
else
{
tmp.a=x-(m-y);
tmp.b=m;
if(!vis[tmp.a][tmp.b])
{
vis[tmp.a][tmp.b]=1;
tmp.str=now.str+"\nPOUR(1,2)";
q.push(tmp);
}
}
}
}
if(y!=m)
{
tmp.a=x;
tmp.b=0;
if(!vis[tmp.a][tmp.b])
{
vis[tmp.a][tmp.b]=1;
tmp.str=now.str+"\nDROP(2)";
q.push(tmp);
}
tmp.a=x;
tmp.b=m;
if(!vis[tmp.a][tmp.b])
{
vis[tmp.a][tmp.b]=1;
tmp.str=now.str+"\nFILL(2)";
q.push(tmp);
}
if(x!=0)
{
if(x+y<m)
{
tmp.a=0;
tmp.b=x+y;
if(!vis[tmp.a][tmp.b])
{
vis[tmp.a][tmp.b]=1;
tmp.str=now.str+"\nPOUR(1,2)";
q.push(tmp);
}
}
else
{
tmp.a=x-(m-y);
tmp.b=m;
if(!vis[tmp.a][tmp.b])
{
vis[tmp.a][tmp.b]=1;
tmp.str=now.str+"\nPOUR(1,2)";
q.push(tmp);
}
}
}
}
else
{
tmp.a=x;
tmp.b=0;
if(!vis[tmp.a][tmp.b])
{
vis[tmp.a][tmp.b]=1;
tmp.str=now.str+"\nDROP(2)";
q.push(tmp);
}
if(x!=n)
{
if(x+y<n)
{
tmp.a=x+y;
tmp.b=0;
if(!vis[tmp.a][tmp.b])
{
vis[tmp.a][tmp.b]=1;
tmp.str=now.str+"\nPOUR(2,1)";
q.push(tmp);
}
}
else
{
tmp.a=n;
tmp.b=(y-(n-x));
if(!vis[tmp.a][tmp.b])
{
vis[tmp.a][tmp.b]=1;
tmp.str=now.str+"\nPOUR(2,1)";
q.push(tmp);
}
}
}
}
}
return -1;
}
int ko;
int main()
{
while(~scanf("%d%d%d",&n,&m,&k))
{
if(n<k&&m<k)
{
printf("impossible\n");
continue;
}
ko=bfs();
if(ko==-1)
{
printf("impossible\n");
continue;
}
}
}