bfs,主要是用pre记录前驱,输出路径。
代码
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
const int N = 100+10;
using namespace std;
bool mark[N][N];
int ans[999999];
int va ,vb,c;
int cnt=0;
struct state
{
int x,y;
int op;
int d;
int pre;
state(int xx,int yy,int opp,int dd,int pree):x(xx),y(yy),op(opp),d(dd),pre(pree)
{
}
state()
{
}
};
state q[10010];
int res[10010];
void output(int i)
{
int pre = i;
while (pre!=-1)
{
res[cnt++] = pre;
pre = q[pre].pre;
}
printf("%d\n",cnt-1);
for (int i=cnt-1;i>=0;i--)
{
switch (q[res[i]].op)
{
case 0:
printf("FILL(%d)\n",q[res[i]].d);
break;
case 1:
printf("DROP(%d)\n",q[res[i]].d);
break;
case 2:
if (q[res[i]].d==1)
{
printf("POUR(1,2)\n");
}
else
{
printf("POUR(2,1)\n");
}
break;
}
}
}
void bfs()
{
int front=-1,rear=-1;
bool flag = false;
state cur;
memset(mark,false,sizeof(mark));
q[++rear] = state(0,0,-1,-1,-1);
while (front<rear)
{
int tx,ty,tpre;
cur = q[++front];
tx = cur.x;
ty = cur.y;
tpre = front;
if (mark[tx][ty])
{
continue;
}
else
{
mark[tx][ty] = true;
}
if (tx==c||ty==c)
{
flag = true;
break;
}
q[++rear] = state(va,ty,0,1,tpre);
q[++rear] = state(tx,vb,0,2,tpre);
if (tx!=0)
{
q[++rear] = state(0,ty,1,1,tpre);
int t = vb - ty;
if (tx-t<0)
{
q[++rear] = state(0,ty+tx,2,1,tpre);
}
else
{
q[++rear] = state(tx-t,vb,2,1,tpre);
}
}
if (ty!=0)
{
q[++rear] = state(tx,0,1,2,tpre);
int t = va - tx;
if (ty-t<0)
{
q[++rear] = state(tx+ty,0,2,2,tpre);
}
else
{
q[++rear] = state(va,ty-t,2,2,tpre);
}
}
}
if (flag)
{
output(front);
}
else
{
printf("impossible\n");
}
}
int main()
{
scanf("%d%d%d",&va,&vb,&c);
bfs();
return 0;
}
#include <cstdio>
#include <cstring>
#include <queue>
const int N = 100+10;
using namespace std;
bool mark[N][N];
int ans[999999];
int va ,vb,c;
int cnt=0;
struct state
{
int x,y;
int op;
int d;
int pre;
state(int xx,int yy,int opp,int dd,int pree):x(xx),y(yy),op(opp),d(dd),pre(pree)
{
}
state()
{
}
};
state q[10010];
int res[10010];
void output(int i)
{
int pre = i;
while (pre!=-1)
{
res[cnt++] = pre;
pre = q[pre].pre;
}
printf("%d\n",cnt-1);
for (int i=cnt-1;i>=0;i--)
{
switch (q[res[i]].op)
{
case 0:
printf("FILL(%d)\n",q[res[i]].d);
break;
case 1:
printf("DROP(%d)\n",q[res[i]].d);
break;
case 2:
if (q[res[i]].d==1)
{
printf("POUR(1,2)\n");
}
else
{
printf("POUR(2,1)\n");
}
break;
}
}
}
void bfs()
{
int front=-1,rear=-1;
bool flag = false;
state cur;
memset(mark,false,sizeof(mark));
q[++rear] = state(0,0,-1,-1,-1);
while (front<rear)
{
int tx,ty,tpre;
cur = q[++front];
tx = cur.x;
ty = cur.y;
tpre = front;
if (mark[tx][ty])
{
continue;
}
else
{
mark[tx][ty] = true;
}
if (tx==c||ty==c)
{
flag = true;
break;
}
q[++rear] = state(va,ty,0,1,tpre);
q[++rear] = state(tx,vb,0,2,tpre);
if (tx!=0)
{
q[++rear] = state(0,ty,1,1,tpre);
int t = vb - ty;
if (tx-t<0)
{
q[++rear] = state(0,ty+tx,2,1,tpre);
}
else
{
q[++rear] = state(tx-t,vb,2,1,tpre);
}
}
if (ty!=0)
{
q[++rear] = state(tx,0,1,2,tpre);
int t = va - tx;
if (ty-t<0)
{
q[++rear] = state(tx+ty,0,2,2,tpre);
}
else
{
q[++rear] = state(va,ty-t,2,2,tpre);
}
}
}
if (flag)
{
output(front);
}
else
{
printf("impossible\n");
}
}
int main()
{
scanf("%d%d%d",&va,&vb,&c);
bfs();
return 0;
}