博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
九度1457...
阅读量:4586 次
发布时间:2019-06-09

本文共 1963 字,大约阅读时间需要 6 分钟。

#include 
#include
#include
#include
using namespace std;const int MAXN = 101;typedef struct { int c[3]; int cnt;}state;queue
q;int s[3];bool vis[MAXN][MAXN][MAXN];int dir[6][2] = { {
0,1}, {
1,0}, {
0,2}, {
2,0}, {
1,2}, {
2,1} };bool ok(state x){ if ( x.c[0] == x.c[1] && x.c[0] * 2 == s[0] ) return true; if ( x.c[1] == x.c[2] && x.c[1] * 2 == s[0] ) return true; if ( x.c[0] == x.c[2] && x.c[0] * 2 == s[0] ) return true; return false;}void pour(state& x, int from, int to){ if ( x.c[from] > 0 ) { int amount = min(x.c[from], s[to] - x.c[to]); x.c[from] -= amount; x.c[to] += amount; x.cnt++; }}int BFS(){ if ( s[0] & 1 == 1 ) return -1; while ( !q.empty() ) q.pop(); state first; first.c[0] = s[0]; first.c[1] = 0; first.c[2] = 0; first.cnt = 0; vis[first.c[0]][first.c[1]][first.c[2]] = true; q.push(first); while ( !q.empty() ) { state curr = q.front(); q.pop(); if ( ok(curr) ) return curr.cnt; for (int i = 0; i < 6; i++) { state next = curr; pour(next, dir[i][0], dir[i][1]); if ( !vis[next.c[0]][next.c[1]][next.c[2]] ) { vis[next.c[0]][next.c[1]][next.c[2]] = true; q.push(next); } } } return -1;}int main(){// freopen("1.txt","r",stdin); while ( scanf("%d%d%d", &s[0], &s[1], &s[2]) == 3 ) { if ( s[0] == 0 && s[1] == 0 && s[2] == 0 ) break; memset(vis, 0, sizeof(vis)); int ans = BFS(); if ( ans == -1 ) printf("NO\n"); else printf("%d\n",ans); } return 0;}

 

转载于:https://www.cnblogs.com/jiongjiong-mengmeng/p/3327102.html

你可能感兴趣的文章
Python装饰器
查看>>
Java String format 对%的处理
查看>>
跨平台移动应用开发AppCan开发文档阅读指南
查看>>
Lind.DDD敏捷领域驱动框架~介绍
查看>>
PHP自带函数给数字前补0或补位(转)
查看>>
iOS runtime实用篇--和常见崩溃say good-bye!
查看>>
细说Cookie
查看>>
Javascript 第二章
查看>>
几个常用算法及反射+多线程调用
查看>>
ubuntu12.04 上面配置blogilo的博客园客户端的步骤
查看>>
Codeforces Gym101170I:Iron and Coal(建多幅图+多次BFS)***
查看>>
Python杂俎 —— 自动压缩指定格式文件&自动删除
查看>>
2017年01月。。
查看>>
bcmath(精准数学的计算)
查看>>
ASP.NET的路由系统:根据路由规则生成URL
查看>>
ASP.NET Core Razor 视图起始页 - ASP.NET Core 基础教程 - 简单教程,简单编程
查看>>
从PRISM开始学WPF(四)Prism-Module?
查看>>
解决session阻塞的问题
查看>>
SQL Server 触发器
查看>>
css优先级计算规则
查看>>