FZU - 2188过河I【BFS】

FZU - 2188
过河I
Time Limit: 3000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u

Submit Status

Description

一天,小明需要把x只羊和y只狼运输到河对面。船可以容纳n只动物和小明。每次小明划船时,都必须至少有一只动物来陪他,不然他会感到厌倦,不安。不论是船上还是岸上,狼的数量如果超过羊,狼就会把羊吃掉。小明需要把所有动物送到对面,且没有羊被吃掉,最少需要多少次他才可以穿过这条河?

Input

有多组数据,每组第一行输入3个整数想x, y, n (0≤ x, y,n ≤ 200)

Output

如果可以把所有动物都送过河,且没有羊死亡,则输出一个整数:最少的次数。 否则输出 -1 .

Sample Input

3 3 233 33 3

Sample Output

11-1

Hint

第一个样例

次数 船 方向 左岸 右岸(狼 羊)

0: 0 0 3 3 0 0

1: 2 0 > 1 3 2 0

2: 1 0 < 2 3 1 0

3: 2 0 > 0 3 3 0

4: 1 0 < 1 3 2 0

5: 0 2 > 1 1 2 2

6: 1 1 < 2 2 1 1

7: 0 2 > 2 0 1 3

8: 1 0 < 3 0 0 3

9: 2 0 > 1 0 2 3

10: 1 0 < 2 0 1 3

11: 2 0 > 0 0 3 3

Source

FOJ有奖月赛-2015年03月

#include
#include
#include
#include
#include
#include
using namespace std;
int X,Y,n;
int vis[210][210];
struct Node{
	int w,s;
	int step;
}s,e;
int BFS(){
	memset(vis,0,sizeof(vis));
	queueQ;vis[0][0]=1;
	s.s=s.w=s.step=0;Q.push(s); 
	while(!Q.empty()){
		Node now=Q.front();Q.pop();
		for(int i=0;i<=X-now.s;++i){
			for(int j=0;j<=Y-now.w;++j){
				if(i&&j>i)break;
				if(i+j==0)continue;
				if(i+j>n)break;
				Node u;
				u.s=now.s+i;u.w=now.w+j;
				if(u.sn)break;
							Node v;
							v.s=u.s-a;v.w=u.w-b;
							if(v.s


你可能感兴趣的:(搜索,FZU,-,2188过河IBFS)