FUZoj 2188:过河I【bfs】

来源:互联网 发布:tv直播软件下载 编辑:程序博客网 时间:2024/04/28 08:48
 过河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

AC-code:

<span style="font-family:FangSong_GB2312;font-size:14px;"> #include<cstdio> #include<cstring> #include<queue> using namespace std; struct node { int x,y,step,mark; }p,now; void bfs(int a,int b,int n) { int i,j; int vis[3][205][205]; memset(vis,0,sizeof(vis));now.x=a;now.y=b;now.step=0;now.mark=0;  queue<node>q;q.push(now);vis[now.mark][now.x][now.y]=1;while(!q.empty()){p=q.front();if(p.x==a&&p.y==b&&p.mark==1){printf("%d\n",p.step);return ;}q.pop();for(i=0;i<=p.y;i++)//狼 {for(j=0;j<=p.x;j++)//羊 {if(i+j==0)continue;if(j<i&&j)continue;if(i+j>n)continue; now.x=a-p.x+j;now.y=b-p.y+i;now.step=p.step+1;now.mark=!p.mark;if((now.x<now.y&&now.x!=0)||(a-now.x<b-now.y&&a-now.x!=0))continue;if(!vis[now.mark][now.x][now.y]){vis[now.mark][now.x][now.y]=1;q.push(now);}}}}printf("-1\n"); } int main() { int x,y,n; while(scanf("%d%d%d",&x,&y,&n)!=EOF)bfs(x,y,n);return 0;  } </span>


0 0
原创粉丝点击