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 ;
}

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. spm完成dmp在windows系统上导入详细过程

    --查询dmp字符集 cat spmprd_20151030.dmp ','xxxx')) from dual; spm完成dmp在windows系统上导入详细过程 create tablespace ...

  2. hadoop与云技术、云计算混肴澄清

    本文引用自:http://www.aboutyun.com/blog-61-248.html 一.初学者问题: 请教个问题在实际的生成环境里面,数据源产生的地方部署Hadoop,还是需要程序把数据给迁 ...

  3. VirtualBox 使用技巧

    一.VirtualBox 的快捷键 VirtualBox 默认的 Host 键是 Right Ctrl Host 键可以点击 Oracle VM VirtualBox 管理器的左上角 “管理”-> ...

  4. centos 安装 openerp

    遇到问题:近日公司提出openerp的搭建,觉得openerp里的有些模块比较适合公司,openerp的运作,估计会有利于公司系统化的管理.于是我就去了解openrp,然后来搭建这套强大的系统. 解决 ...

  5. Linux 性能工具 - sar学习

    简介 sar是一款在linux下的性能工具,可以观察到CPU,内存,IO,运行队列,每秒上下文切换等信息. 软件工具安装 #Ubuntu sudo apt-get install sysstat # ...

  6. Eclipse 常用整理

    1.编译 eclipse默认是修改程序后自动编译的,如果不能自动编译,你可以查看project->build automatically选项是否被选中. 手动编译整个工程,可以使用Project ...

  7. (转载)C++文件读写函数之——fopen、fread和fwrite、fgetc和fputc、fgets和fputs、ftellf和fseek、rewind

    http://blog.sina.com.cn/s/blog_61437b3b0102v0bt.html http://blog.csdn.net/chenwk891/article/details/ ...

  8. Java学习-037-JavaWeb_006 -- JSP 动作标识 - include

    这个动作是指在当前的页面中包含一个或多个 JSP 页面或者 HTML 文件,语法:<jsp:include file="../jsp/login.jsp" flush=&qu ...

  9. 热门WEB前端职业你只需要掌握这些

    在知名的互联网企业里工作是一件很美好的事情,有很多的工作机会,而且企业们通过高薪以及令人羡慕的福利来争夺最优秀的人才.但是如果你花了大量的时间在招聘网站上和公司的帖子上,你可能会注意到在网页设计这个工 ...

  10. mouseenter(fn)和mouseleave、mouseover和mouseout的的区别

    $('.box1').mouseenter(function(){//穿入事件mouseenter $(this).css('background','red'); }).mouseleave(fun ...