http://acm.fzu.edu.cn/problem.php?pid=2188

过河I

Time Limit:3000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

Description

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

Input

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

Output

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

Sample Input

3 3 2 33 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

分析:

次数   羊    狼   方向   羊     狼

0;  3  3  0  0  0

1;  3  1  1  0  2

2;  3  2  0  0  1

3;  3  0  1  0  3

4;  3  1  0  0  2

5:  1  1  1  2  2

6:    2  2  0  1  1

7;  0  2  1  3  1

8;  0  3  0  3  0

9;  0  1  1  3  2

10;  0  2  0  3  1

11;  0  0  1  3  3

题目中有多个减枝描述;

“船可以容纳n只动物” && “至少有一只动物来陪他” && “不论是船上还是岸上,狼的数量如果超过羊,狼就会把羊吃掉 ”  && “0≤ x, y,n ≤ 200”。

AC代码:

 #include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
bool vis[][][];
int sx,sy,n;
struct node
{
int x,y;
int c;
int cnt;
};
node cur,nxt;
queue<node> que;
int main()
{
while(~scanf("%d%d%d",&sx,&sy,&n))
{
memset(vis,,sizeof(vis));
while(!que.empty()) que.pop();
cur.x=sx,cur.y=sy;
cur.c=,cur.cnt=;// cur.c = 0 代表左岸 , cur.c = 1 代表右岸 , cur.cnt 代表步数。
vis[sx][sy][] = ;
//vis[x][y][2] 表示船到达0或1岸后,此岸上的羊数x,和狼数y , 标记数组。
que.push(cur);
bool flag=;
while(!que.empty())
{
cur=que.front();
que.pop();
if(cur.c==&&cur.x==sx&&cur.y==sy)
{
flag=true;
printf("%d\n",cur.cnt);
break;
}
nxt.c=!cur.c;
nxt.cnt=cur.cnt+;
for(int i=;i<=cur.x;i++)// i 代表船上羊的数量。
for(int j=;j<=cur.y;j++)// j 代表船上狼的数量。
{
if(i+j==) continue;
if(i+j>n) continue;
if(i<j&&i!=) continue;
if(cur.x-i<cur.y-j&&cur.x-i!=) continue;
nxt.x=sx-cur.x+i,nxt.y=sy-cur.y+j;
if(nxt.x<nxt.y&&nxt.x!=) continue;
if(vis[nxt.x][nxt.y][nxt.c]) continue;
vis[nxt.x][nxt.y][nxt.c]=;
que.push(nxt);
}
}
if(!flag) puts("-1");
}
return ;
}

最新文章

  1. 移动端接口:java写get方式访问数据(springmvc+spring。。。)
  2. firefox怎么修改tls协议号
  3. Ubuntu编译源码程序依赖查找方法
  4. Effective C++ -----条款11: 在operator=中处理“自我赋值”
  5. ASM:《X86汇编语言-从实模式到保护模式》5-7章:汇编基础
  6. Java如何将Exception.printStackTrace()转换为String输出
  7. mybatis for .net
  8. 【转】Lua编程规范
  9. sql处理null值
  10. Mybatis入门程序
  11. Oracle解决Ora-01653无法扩展表空间问题
  12. 040 Http与RPC
  13. 杨其菊201771010134《面向对象程序设计(java)》第四周学习总结
  14. 开发vue单页面Demo
  15. 如何解决win10关机状态下,按键盘会自动开机的问题
  16. 创建springboot的聚合工程(一)
  17. css 文档流中块级非替换元素水平区域的计算规则(1)
  18. python 中numpy的var,std及cov
  19. 【转】Angular.js VS. Ember.js:谁将成为Web开发的新宠?
  20. js的alert抛出之后怎么让页面停止执行?

热门文章

  1. Nginx return 关键字配置小技巧
  2. Git and Xcode
  3. library not found for -lPods 的解决办法
  4. Linux的常用基本命令
  5. springmvc返回值、数据写到页面、表单提交、ajax、重定向
  6. JavaScript正则表达式(三)
  7. CS6破解
  8. asp.net操作xml
  9. C#资源释放
  10. 微信公众平台开发(98) UnionID