Nyoj 21 三个水杯
2014-04-03 21:06
507 查看
题目来源:http://acm.nyist.net/JudgeOnline/problem.php?pid=21
用搜索来模拟每一种状态,直到满足目标状态。
用搜索来模拟每一种状态,直到满足目标状态。
#include <iostream> #include <cstring> #include <cstdio> #include <queue> #include <cmath> #include <algorithm> using namespace std; typedef struct State_News State; struct State_News { int a, b, c;//每种状态中每个水杯中的水量 int step; State_News() { a = 0; b = 0; c = 0; step = 0; } }; int v1, v2, v3;//表示三个水杯的容量 bool Is_Visit[110][110][110];//记录每个状态是否到达过 State TargetState;//目标状态 int BFS(State CurState)//搜索所有倒水状态中每一种状态,看是否满足目标状态100 * 100 * 100 = 1000000种情况,可以一一枚举出 { queue <State> Que; while(!Que.empty()) Que.pop(); Que.push(CurState); while(!Que.empty()) { State CurState_News = Que.front(); Que.pop(); Is_Visit[CurState_News.a][CurState_News.b][CurState_News.c] = true; if(CurState_News.a == TargetState.a && CurState_News.b == TargetState.b && CurState_News.c == TargetState.c) return CurState_News.step; if(CurState_News.a > 0 && CurState_News.b < v2)//v1->v2 表示从v1中向v2中倒水 { State temp = CurState_News; int tmp = min(temp.a, v2 - temp.b); temp.a -= tmp; temp.b += tmp; if(Is_Visit[temp.a][temp.b][temp.c] == false) { Is_Visit[temp.a][temp.b][temp.c] = true; temp.step++; //这一句的等价于temp.step = CurState_News.step + 1;改变了水量step但是还是前一种状态中的step,因此自加1即可 Que.push(temp); } } if(CurState_News.a > 0 && CurState_News.c < v3)//v1->v3 { State temp = CurState_News; int tmp = min(temp.a, v3 - temp.c); temp.a -= tmp; temp.c += tmp; if(Is_Visit[temp.a][temp.b][temp.c] == false) { Is_Visit[temp.a][temp.b][temp.c] = true; temp.step++; Que.push( temp ); } } if(CurState_News.b > 0 && CurState_News.a < v1)//v2->v1 { State temp = CurState_News; int tmp = min(temp.b, v1 - temp.a); temp.b -= tmp; temp.a += tmp; if(Is_Visit[temp.a][temp.b][temp.c] == false) { Is_Visit[temp.a][temp.b][temp.c] = true; temp.step++; Que.push( temp ); } } if(CurState_News.b > 0 && CurState_News.c < v3)//v2->v3 { State temp = CurState_News; int tmp = min(temp.b, v3 - temp.c); temp.b -= tmp; temp.c += tmp; if(!Is_Visit[temp.a][temp.b][temp.c]) { Is_Visit[temp.a][temp.b][temp.c] = true; temp.step++; Que.push( temp ); } } if(CurState_News.c > 0 && CurState_News.a < v1)//v3 -> v1 { State temp = CurState_News; int tmp = min(temp.c, v1 - temp.a); temp.c -= tmp; temp.a += tmp; if(!Is_Visit[temp.a][temp.b][temp.c]) { Is_Visit[temp.a][temp.b][temp.c] = true; temp.step++; Que.push( temp ); } } if(CurState_News.c > 0 && CurState_News.b < v2)//v3 -> v2 { State temp = CurState_News; int tmp = min(temp.c, v2-temp.b); temp.b += tmp; temp.c -= tmp; if(!Is_Visit[temp.a][temp.b][temp.c]) { Is_Visit[temp.a][temp.b][temp.c] = true; temp.step++; Que.push( temp ); } } } return -1; } int main() { State InitialState;//初始状态 int T; scanf("%d", &T); while(T--) { memset(Is_Visit, 0, sizeof(Is_Visit)); scanf("%d %d %d", &v1, &v2, &v3); InitialState.a = v1, InitialState.b = 0, InitialState.c = 0, InitialState.step = 0; scanf("%d %d %d", &TargetState.a, &TargetState.b, &TargetState.c); if(v1 < TargetState.a + TargetState.b + TargetState.c) { printf("-1\n"); continue ; } printf("%d\n", BFS( InitialState )); } return 0; }
相关文章推荐
- 第4章: jQuery中的事件和动画---目录
- 财经法规二-2
- c语言基础-----printf 的使用(格式控制符整理)
- sed 用法
- How to Create Rich Edit Controls
- Android设计模式系列(5)--备忘录模式
- -----------android QQ截图 开发--------------growBy-----------
- java动态数组、集合类、以及数组辅助工具的简单使用
- windows7 sqlserver2012 无法写入受保护的内存 解决办法
- Ubuntu12.04 apt-get 安装mysql
- 《基于ORACLE的SQL优化读书》笔记 绑定变量窥探
- 【P000-003】交易费计算系统,从股票信息网络接口获取信息
- 使用kingEditor实现图片上传
- eclipse编辑器,怎么创建PHP和JAVA的工程项目?
- SQL临时表
- java中的堆栈
- SQL临时表
- Skyscrapers 组合数学
- 解决apache服务器本地可以访问,同局域网内他人不能访问的问题(转)
- Android设计模式——模板方法模式