POJ3414(BFS+[手写队列])
贴一发自己写的手写队列…..
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <queue>
#include <set>
#include <stack>
#include <algorithm>
using namespace std;
#define PI acos(-1.0)
#define INF 0x7fffffff
#define N 1010
struct asd
{
int a,b;
int flag;
int step;
} q[N];
int pre[N];
int vis[N][N];
int head,rail;
void pri()
{
printf("impossible\n");
}
void print(int x)
{
int t;
t=pre[x];
if(t==0)
{
if(q[x].flag==0)
{
printf("FILL(1)\n");
}
else if(q[x].flag==1)
{
printf("FILL(2)\n");
}
else if(q[x].flag==2)
{
printf("DROP(1)\n");
}
else if(q[x].flag==3)
{
printf("DROP(2)\n");
}
else if(q[x].flag==4)
{
printf("POUR(2,1)\n");
}
else if(q[x].flag==5)
{
printf("POUR(1,2)\n");
}
return;
}
else
{
print(t);
}
if(q[x].flag==0)
{
printf("FILL(1)\n");
}
else if(q[x].flag==1)
{
printf("FILL(2)\n");
}
else if(q[x].flag==2)
{
printf("DROP(1)\n");
}
else if(q[x].flag==3)
{
printf("DROP(2)\n");
}
else if(q[x].flag==4)
{
printf("POUR(2,1)\n");
}
else if(q[x].flag==5)
{
printf("POUR(1,2)\n");
}
}
void bfs(int n,int m,int c)
{
memset(vis,0,sizeof(vis));
head=0;
rail=1;
q[0].a=0;
q[0].b=0;
q[0].flag=-1;
q[0].step=0;
vis[0][0]=1;
pre[0]=-1;
while(head<rail)
{
//printf("%d\n",head);
int x=q[head].a;
int y=q[head].b;
if(x==c||y==c)
{
printf("%d\n",q[head].step);
print(head);
return;
}
for(int i=0; i<6; i++)
{
if(i==0)
{
q[rail].a=n;
q[rail].b=q[head].b;
q[rail].flag=0;
}
else if(i==1)
{
q[rail].a=q[head].a;
q[rail].b=m;
q[rail].flag=1;
}
else if(i==2)
{
q[rail].a=0;
q[rail].b=q[head].b;
q[rail].flag=2;
}
else if(i==3)
{
q[rail].a=q[head].a;
q[rail].b=0;
q[rail].flag=3;
}
else if(i==4)
{
// printf("aaa\n");
if((q[head].b+q[head].a)>=n)
{
q[rail].a=n;
q[rail].b=q[head].b-(n-q[head].a);
q[rail].flag=4;
}
else if((q[head].b+q[head].a)<n)
{
q[rail].a=q[head].a+q[head].b;
q[rail].b=0;
q[rail].flag=4;
}
// printf("%d %d\n",q[rail].a,q[rail].b);
}
else if(i==5)
{
if((q[head].a+q[head].b)>=m)
{
q[rail].a=q[head].a-(m-q[head].b);
q[rail].b=m;
q[rail].flag=5;
}
else if((q[head].a+q[head].b)<m)
{
q[rail].a=0;
q[rail].b=q[head].b+q[head].a;
q[rail].flag=5;
}
}
//printf("%d %d\n",q[rail].a,q[rail].b);
if(vis[q[rail].a][q[rail].b])
continue;
vis[q[rail].a][q[rail].b]=1;
q[rail].step=q[head].step+1;
pre[rail]=head;
rail++;
if(q[rail].b==c||q[rail].a==c)
{
//printf("aaa\n");
printf("%d\n",q[head].step+1);
print(rail);
return;
}
}
head++;
}
pri();
return;
}
int main()
{
int n,m,c;
while(~scanf("%d%d%d",&n,&m,&c))
{
// printf("%d %d %d\n",n,m,c);
bfs(n,m,c);
}
return 0;
}