标签:des style http color java os io for
3 5 4
6
#include <cstdio>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <queue>
using namespace std;
int m,n,c;
typedef struct node
{
int v1,v2,op;
};
bool vis[999][999];
void bfs()
{
node t={0,0,0};
queue <node> Q;
Q.push(t);
vis[0][0]=1;
while(!Q.empty())
{
node f=Q.front();Q.pop();
if(f.v1==c||f.v2==c)
{
cout<<f.op<<endl;
return ;
}
if(f.v1!=m)
{
t.v1=m;
t.op=f.op+1;
t.v2=f.v2;
if(!vis[t.v1][t.v2])
{
vis[t.v1][t.v2]=1;
Q.push(t);
}
}
if(f.v2!=n)
{
t.v2=n;
t.op=f.op+1;
t.v1=f.v1;
if(!vis[t.v1][t.v2])
{
vis[t.v1][t.v2]=1;
Q.push(t);
}
}
if(f.v1!=0)
{
t.v1=0;
t.v2=f.v2;
t.op=f.op+1;
if(!vis[t.v1][t.v2])
{
vis[t.v1][t.v2]=1;
Q.push(t);
}
}
if(f.v2!=0)
{
t.v2=0;
t.v1=f.v1;
t.op=f.op+1;
if(!vis[t.v1][t.v2])
{
vis[t.v1][t.v2]=1;
Q.push(t);
}
}
if(f.v2!=0&&f.v1!=m)
{
t.v2=f.v2-(m-f.v1);if(t.v2<0) t.v2=0;
t.v1=f.v1+f.v2; if(t.v1>m) t.v1=m;
t.op=f.op+1;
if(!vis[t.v1][t.v2])
{
vis[t.v1][t.v2]=1;
Q.push(t);
}
}
if(f.v1!=0&&f.v2!=n)
{
t.v1=f.v1-(n-f.v2);if(t.v1<0) t.v1=0;
t.v2=f.v2+f.v1; if(t.v2>n) t.v2=n;
t.op=f.op+1;
if(!vis[t.v1][t.v2])
{
vis[t.v1][t.v2]=1;
Q.push(t);
}
}
}
puts("impossible");
}
int main()
{
while(cin>>m>>n>>c)
{
memset(vis,0,sizeof(vis));
bfs();
}
return 0;
}SDUT--Pots--BFS,布布扣,bubuko.com
标签:des style http color java os io for
原文地址:http://blog.csdn.net/qq_16255321/article/details/38515767