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代码:

  1. #include<cstdio>
  2. #include<iostream>
  3. #include<cstring>
  4. #include<algorithm>
  5. #include<queue>
  6. using namespace std;
  7. bool vis[][][];
  8. int sx,sy,n;
  9. struct node
  10. {
  11. int x,y;
  12. int c;
  13. int cnt;
  14. };
  15. node cur,nxt;
  16. queue<node> que;
  17. int main()
  18. {
  19. while(~scanf("%d%d%d",&sx,&sy,&n))
  20. {
  21. memset(vis,,sizeof(vis));
  22. while(!que.empty()) que.pop();
  23. cur.x=sx,cur.y=sy;
  24. cur.c=,cur.cnt=;// cur.c = 0 代表左岸 , cur.c = 1 代表右岸 , cur.cnt 代表步数。
  25. vis[sx][sy][] = ;
  26. //vis[x][y][2] 表示船到达0或1岸后,此岸上的羊数x,和狼数y , 标记数组。
  27. que.push(cur);
  28. bool flag=;
  29. while(!que.empty())
  30. {
  31. cur=que.front();
  32. que.pop();
  33. if(cur.c==&&cur.x==sx&&cur.y==sy)
  34. {
  35. flag=true;
  36. printf("%d\n",cur.cnt);
  37. break;
  38. }
  39. nxt.c=!cur.c;
  40. nxt.cnt=cur.cnt+;
  41. for(int i=;i<=cur.x;i++)// i 代表船上羊的数量。
  42. for(int j=;j<=cur.y;j++)// j 代表船上狼的数量。
  43. {
  44. if(i+j==) continue;
  45. if(i+j>n) continue;
  46. if(i<j&&i!=) continue;
  47. if(cur.x-i<cur.y-j&&cur.x-i!=) continue;
  48. nxt.x=sx-cur.x+i,nxt.y=sy-cur.y+j;
  49. if(nxt.x<nxt.y&&nxt.x!=) continue;
  50. if(vis[nxt.x][nxt.y][nxt.c]) continue;
  51. vis[nxt.x][nxt.y][nxt.c]=;
  52. que.push(nxt);
  53. }
  54. }
  55. if(!flag) puts("-1");
  56. }
  57. return ;
  58. }

fzu 2188 过河I的更多相关文章

  1. 过河(bfs)

    Problem 2188 过河I Accept: 112    Submit: 277 Time Limit: 3000 mSec    Memory Limit : 32768 KB  Proble ...

  2. [LeetCode] Frog Jump 青蛙过河

    A frog is crossing a river. The river is divided into x units and at each unit there may or may not ...

  3. FZU 2137 奇异字符串 后缀树组+RMQ

    题目连接:http://acm.fzu.edu.cn/problem.php?pid=2137 题解: 枚举x位置,向左右延伸计算答案 如何计算答案:对字符串建立SA,那么对于想双延伸的长度L,假如有 ...

  4. FZU 1914 单调队列

    题目链接:http://acm.fzu.edu.cn/problem.php?pid=1914 题意: 给出一个数列,如果它的前i(1<=i<=n)项和都是正的,那么这个数列是正的,问这个 ...

  5. ACM: FZU 2105 Digits Count - 位运算的线段树【黑科技福利】

     FZU 2105  Digits Count Time Limit:10000MS     Memory Limit:262144KB     64bit IO Format:%I64d & ...

  6. FZU 2112 并查集、欧拉通路

    原题:http://acm.fzu.edu.cn/problem.php?pid=2112 首先是,票上没有提到的点是不需要去的. 然后我们先考虑这个图有几个连通分量,我们可以用一个并查集来维护,假设 ...

  7. [codevs1105][COJ0183][NOIP2005]过河

    [codevs1105][COJ0183][NOIP2005]过河 试题描述 在河上有一座独木桥,一只青蛙想沿着独木桥从河的一侧跳到另一侧.在桥上有一些石子,青蛙很讨厌踩在这些石子上.由于桥的长度和青 ...

  8. ACM: FZU 2107 Hua Rong Dao - DFS - 暴力

    FZU 2107 Hua Rong Dao Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I6 ...

  9. ACM: FZU 2112 Tickets - 欧拉回路 - 并查集

     FZU 2112 Tickets Time Limit:3000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u P ...

随机推荐

  1. Java笔试面试题二(常考问答)转

    1.说出ArrayList,Vector, LinkedList的存储性能和特性 ArrayList 和Vector都是使用数组方式存储数据,此数组元素数大于实际存储的数据以便增加和插入元素,它们都允 ...

  2. Windows下查看机器监听端口

    1.查看所有端口占用情况 在开始-运行-cmd,输入:netstat –ano可以查看所有进程 2.查看指定端口的占用情况      netstat -an |findstr :21 

  3. java JDK8 学习笔记——第18章 自定义泛型、枚举与注释

    第十八章 自定义泛型.枚举与注释 18.1 自定义泛型 泛型定义: (1)仅定义在方法上的泛型语法 (2)用来限制泛型可用类型的extends与super关键字(3)?类型通配字符的使用 18.1.1 ...

  4. 【Android测试】【第九节】MonkeyRunner—— 初识

    ◆版权声明:本文出自胖喵~的博客,转载必须注明出处. 转载请注明出处:http://www.cnblogs.com/by-dream/p/4836815.html 不得不说两句,过了这么久才再次更新博 ...

  5. 解决Eclipse启动报错Failed to create the Java Virtual Machine

    电脑:2G内存,WIN7 32位. 启动adt-bundle-windows-x86-20140702\eclipse\eclipse.exe时,报错[Failed to create the Jav ...

  6. ArcGIS中添加进自定义的ttf字符标记符号

    原文:ArcGIS中添加进自定义的ttf字符标记符号 ArcGIS系统中的样式可能不能满足实际生产需要,为了实现快速制图,可自定义一些样式,以便重复利用. 1.   制作的符号库 使用 FontCre ...

  7. 通过magento后台的magento connect安装magento extension

    http://magentoinfo.blog.163.com/blog/static/215636160201302272653538/ magento的extension库基本上可以说要什么有什么 ...

  8. CodeTimer

    using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using S ...

  9. python echo服务器和客户端(客户端可以用telnet之类的)

    发上来记录一下,省得下次再写一遍 服务器:server.py #-*- coding:utf-8 -*- from SocketServer import TCPServer, BaseRequest ...

  10. c#上传文件(一)使用 .net 控件上传文件

    1.html代码: <body> <form id="form1" runat="server"> <div> <as ...