NYOJ 21
法一:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;
int total;
int v1, v2, v3;
bool visit[105][105][105]; //状态是否出现
struct state
{
int a, b, c;
int ceng; //最小步数
state(){
a = b = c = ceng = 0;
}
}b, e;
int BFS(state begin)
{
queue<state> q;
while(!q.empty())
q.pop();
q.push(b);
while(!q.empty())
{
state cur = q.front();
q.pop();
visit[cur.a][cur.b][cur.c] = true;
if(cur.a == e.a && cur.b == e.b && cur.c == e.c) //找到
return cur.ceng;
if(cur.a > 0 && cur.b < v2) //v1->v2
{
state temp = cur;
int tt = min(temp.a, v2 - temp.b);
temp.a -= tt;
temp.b += tt;
if(visit[temp.a][temp.b][temp.c] == false)
{
visit[temp.a][temp.b][temp.c] = true;
temp.ceng++;
q.push(temp);
}
}
if(cur.a > 0 && cur.c < v3) //v1->v3
{
state temp = cur;
int tt = min(temp.a, v3 - temp.c);
temp.a -= tt;
temp.c += tt;
if(visit[temp.a][temp.b][temp.c] == false)
{
visit[temp.a][temp.b][temp.c] = true;
temp.ceng++;
q.push(temp);
}
}
if(cur.b > 0 && cur.a < v1) //v2->v1
{
state temp = cur;
int tt = min(temp.b, v1 - temp.a);
temp.a += tt;
temp.b -= tt;
if(visit[temp.a][temp.b][temp.c] == false)
{
visit[temp.a][temp.b][temp.c] = true;
temp.ceng++;
q.push(temp);
}
}
if(cur.b > 0 && cur.c < v3) //v2->v3
{
state temp = cur;
int tt = min(temp.b, v3 - temp.c);
temp.b -= tt;
temp.c += tt;
if(visit[temp.a][temp.b][temp.c] == false)
{
visit[temp.a][temp.b][temp.c] = true;
temp.ceng++;
q.push(temp);
}
}
if(cur.c > 0 && cur.a < v1) //v3->v1
{
state temp = cur;
int tt = min(temp.c, v1 - temp.a);
temp.c -= tt;
temp.a += tt;
if(visit[temp.a][temp.b][temp.c] == false)
{
visit[temp.a][temp.b][temp.c] = true;
temp.ceng++;
q.push(temp);
}
}
if(cur.c > 0 && cur.b < v2) //v3->v2
{
state temp = cur;
int tt = min(temp.c, v2 - temp.b);
temp.c -= tt;
temp.b += tt;
if(visit[temp.a][temp.b][temp.c] == false)
{
visit[temp.a][temp.b][temp.c] = true;
temp.ceng++;
q.push(temp);
}
}
}
return -1; //没有终状态
}
int main()
{
int ncase;
scanf("%d", &ncase);
while(ncase--)
{
total = 0;
memset(visit, false, sizeof(visit));
scanf("%d %d %d", &v1, &v2, &v3);
b.a = v1, b.b = 0, b.c = 0, b.ceng = 0;
scanf("%d %d %d", &e.a, &e.b, &e.c);
if(v1 < e.a + e.b + e.c)
{
printf("-1\n");
continue;
}
else
printf("%d\n", BFS(b));
}
return 0;
}
法二:
#include <stdio.h>
#include <string.h>
#define MOVE(x) (x=(x+1)%1000)
bool state[100][100];
struct Size
{
int e[3];
}queue[1000];
int V[3],E1,E2,E3;
int BFS()
{
if(E1+E2+E3!=V[0]) return -1;
else if(E1<0 || E2<0 || E3<0) return -1;
else if(V[0]==E1 && 0==E2 && 0==E3) return 0;
else memset(state,false,sizeof(state));
int tpe,cnt=0;
int front,rear;
int num,temp;
Size s;
front=rear=-1; s.e[0]=V[0]; s.e[1]=0; s.e[2]=0;
queue[MOVE(rear)]=s; state[s.e[0]][s.e[1]]=true; num=1; temp=0;
while(front!=rear)
{
s=queue[MOVE(front)];
if(--num==0) cnt++;
for(int i=0;i<3;i++)
{
if(s.e[i])
{
for(int j=(i+1)%3;j!=i;j=(j+1)%3)
{
tpe=(s.e[i]>V[j]-s.e[j])?(V[j]-s.e[j]):s.e[i];
s.e[i]-=tpe; s.e[j]+=tpe;
if(s.e[0]==E1 && s.e[1]==E2 && s.e[2]==E3)
return num?cnt+1:cnt;
if(!state[s.e[0]][s.e[1]])
{
queue[MOVE(rear)]=s;
state[s.e[0]][s.e[1]]=true;
temp++;
}
s.e[i]+=tpe; s.e[j]-=tpe;
}
}
}
if(num==0) num=temp,temp=0;
}
return -1;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d %d %d",&V[0],&V[1],&V[2]);
scanf("%d %d %d",&E1,&E2,&E3);
printf("%d\n",BFS());
}
return 0;
}