1017 非常可乐
来源:互联网 发布:开淘宝店需要注意哪些 编辑:程序博客网 时间:2024/04/29 18:09
题意:
要求将一瓶可乐平均分成份,问能否平均分成两份,如果能输出最少需要几次否则输出NO题目给出三个整数 S N M S表示可乐总量 N M分别为两个杯子的容量 且 S= N + M
思路:
先将M,N按大小排序 小的设为M;由 S 0 0 状态 转换到 0 S/2 S/2 的状态且要找出最优解,用BFS;题目只有6种状态转换方式:S-M,S-N,N-S,N-M,M-S,M-N,建立一格六叉数的模型进行遍历。用结构体记录步数 步数为弹出队头元素的步数+1 遍历完成条件为 找到一个方法能够平分或者遍历所有可能情况仍然无法找到
上课讲过了 不是很难 代码很长 但大部分都重复了 复制粘贴就行。
// ConsoleApplication19.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include<iostream>#include<algorithm>#include<queue>#include<string.h>#include<fstream>#include<iostream>using namespace std;#define maxn 101bool visited[maxn][maxn];int m, n, s, si, sj;struct node{ int x, y, all, t; };void BFS(){ queue<node> que; memset(visited, false, sizeof(visited)); node p, q; p.x = 0, p.y = 0, p.t = 0, p.all = s; que.push(p); visited[p.x][p.y] = true; while (!que.empty()) { p = que.front(); que.pop(); if (p.y == p.all && p.y == s / 2) { cout<<p.t<<endl; return; } if (p.all + p.x>m) { q.x = m ; q.y = p.y; q.all = p.all + p.x - m; q.t = p.t + 1; if (!visited[q.x][q.y]) que.push(q), visited[q.x][q.y] = true; } else { q.x = p.all + p.x; q.y = p.y; q.all = 0 ; q.t = p.t + 1; if (!visited[q.x][q.y]) que.push(q), visited[q.x][q.y] = true; } if (p.all + p.y>n) { q.x = p.x; q.y = n; q.all = p.all + p.y - n; q.t = p.t + 1; if (!visited[q.x][q.y]) que.push(q), visited[q.x][q.y] = true; } else { q.x = p.x; q.y = p.all + p.y; q.all = 0; q.t = p.t + 1; if (!visited[q.x][q.y]) que.push(q), visited[q.x][q.y] = true; } if (p.x + p.y>n) { q.x = p.x + p.y - n; q.y = n; q.all = p.all; q.t = p.t + 1; if (!visited[q.x][q.y]) que.push(q), visited[q.x][q.y] = true; } else { q.x = 0; q.y = p.x + p.y; q.all = p.all; q.t = p.t + 1; if (!visited[q.x][q.y]) que.push(q), visited[q.x][q.y] = true; } q.all = p.all + p.x, q.x = 0, q.y = p.y, q.t = p.t + 1; if (!visited[q.x][q.y]) que.push(q), visited[q.x][q.y] = true; if (p.x + p.y > m) { q.y = p.y + p.x - m; q.x = m; q.all = p.all; q.t = p.t + 1; if (!visited[q.x][q.y]) que.push(q), visited[q.x][q.y] = true; } else { q.x = p.x + p.y, q.y = 0, q.all = p.all, q.t = p.t + 1; if (!visited[q.x][q.y]) que.push(q), visited[q.x][q.y] = true; } q.all = p.all + p.y, q.x = p.x, q.y = 0, q.t = p.t + 1; if (!visited[q.x][q.y]) que.push(q), visited[q.x][q.y] = true; } cout<<"NO"<<endl;}int main(){ while (cin>>s>>m>>n && (s || m || n)) { if (s % 2) { cout<<"NO"<<endl; continue; } if (m > n) swap(m, n); BFS(); } return 0;}
0 0
- 1017 非常可乐
- 1017 非常可乐
- 1017 非常可乐
- 1017-非常可乐
- 1017 非常可乐
- 1017 非常可乐
- acm 2 1017 非常可乐
- 练习二 1017 非常可乐
- 练习二 1017 非常可乐
- 非常可乐
- 非常可乐
- 非常可乐
- 非常可乐
- 非常可乐
- 非常可乐
- 非常可乐
- 非常可乐
- 非常可乐
- 让linux每天定时备份MySQL数据库并删除五天前的备份文件
- SpringMVC过滤器
- POJ-2421Constructing Roads,又是最小生成树,和第八届河南省赛的引水工程惊人的相似,并查集与最小生成树的灵活与能用,水过~~~
- 实习过程中linux相关开发学习总结(四)
- 如何优化 Android Studio 启动、编译和运行速度?
- 1017 非常可乐
- ios 断点续传
- 点击编辑框外,隐藏软键盘-实例代码收藏
- utf-8 转为uncode和解码
- 制作一个自己的linux光盘(使用kickstart)
- 怎样解决iMindMap中导图无法新建、打开
- 打开MySQL数据库远程访问的权限
- 联想E450c笔记本电脑
- http请求的header body