每种状态下,只有六种移动的情况:A->B, A->C, B->A, B->C, C->A, C->B, 一一判断,BFS,直到没有新的状态出现。
/* ID: bbsunch2 PROG: milk3 LANG: C++ */ #include<iostream> #include<cstdio> #include<cstring> #include<string> #include<vector> #include<queue> #include<map> #include<set> #include<cmath> #include<algorithm> #include<fstream> using namespace std; vector<int> aStates; vector<int> bStates; vector<int> cStates; vector<int> cValues; int aCapacity, bCapacity, cCapacity; bool newState = false; bool check_exist(const int a, const int b, const int c){ for(int i = 0; i < aStates.size(); i++){ if((a == aStates[i]) && (b == bStates[i]) && (c == cStates[i])){ //fout << a << " " << aStates[i] << " " << b << " " << bStates[i] << " " << c << " " << cStates[i] << endl; return true; } } return false; } int insert_states(const int as, const int bs, const int cs){ //fout << aStates.size() << ": " << as << " " << bs << " " << cs << endl; aStates.push_back(as); bStates.push_back(bs); cStates.push_back(cs); if (as == 0){ //cout << cs << endl; cValues.push_back(cs); } newState = true; } int main(){ ofstream fout("milk3.out"); ifstream fin("milk3.in"); fin >> aCapacity; fin >> bCapacity; fin >> cCapacity; //cout << cCapacity << endl; //initialize insert_states(0,0,cCapacity); //endStates.push_back(false); int currentStartIndex = 0; int currentEndIndex = aStates.size() - 1; // record current phase's states, so this is basically a bfs while(1){ // iterate phases for(int i = currentStartIndex; i <= currentEndIndex; i++){ //fout << "considering " << i << endl; // for each state in current phase, we have six operations to do, we use if if(aStates[i] > 0 && (cStates[i] < cCapacity)){ //fout << "A->C" << endl; int transValue = min(cCapacity-cStates[i], aStates[i]); int as = aStates[i] - transValue; int cs = cStates[i] + transValue; int bs = bStates[i]; if(!check_exist(as, bs, cs)){ insert_states(as,bs,cs); } } if(aStates[i] > 0 && (bStates[i] < cCapacity)){ //fout << "A->B" << endl; int transValue = min(bCapacity-bStates[i], aStates[i]); int as = aStates[i] - transValue; int bs = bStates[i] + transValue; int cs = cStates[i]; if(!check_exist(as, bs, cs)){ insert_states(as,bs,cs); } } if(bStates[i] > 0 && (cStates[i] < cCapacity)){ //fout << "B->C" << endl; int transValue = min(cCapacity-cStates[i], bStates[i]); int bs = bStates[i] - transValue; int cs = cStates[i] + transValue; int as = aStates[i]; if(!check_exist(as, bs, cs)){ insert_states(as,bs,cs); } } if(bStates[i] > 0 && (aStates[i] < aCapacity)){ //fout << "B->A" << endl; int transValue = min(aCapacity-aStates[i], bStates[i]); int bs = bStates[i] - transValue; int as = aStates[i] + transValue; int cs = cStates[i]; if(!check_exist(as, bs, cs)){ insert_states(as,bs,cs); } } if(cStates[i] > 0 && (aStates[i] < aCapacity)){ //fout << "C->A" << endl; int transValue = min(aCapacity-aStates[i], cStates[i]); int cs = cStates[i] - transValue; int as = aStates[i] + transValue; int bs = bStates[i]; if(!check_exist(as, bs, cs)){ insert_states(as,bs,cs); } } if(cStates[i] > 0 && (bStates[i] < bCapacity)){ //fout << "C->B" << endl; int transValue = min(bCapacity-bStates[i], cStates[i]); int cs = cStates[i] - transValue; int bs = bStates[i] + transValue; int as = aStates[i]; if(!check_exist(as, bs, cs)){ insert_states(as,bs,cs); } } } currentStartIndex = currentEndIndex + 1; currentEndIndex = aStates.size() - 1; if(!newState) break; newState = false; } sort(cValues.begin(), cValues.end()); int previousC = -1; for(int i = 0; i < cValues.size(); i++){ if(cValues[i] != previousC){ if(i != cValues.size()-1){ cout << cValues[i] << " "; fout << cValues[i] << " "; }else{ cout << cValues[i] << endl; fout << cValues[i] << endl; } previousC = cValues[i]; } } fout.close(); return 0; }
相关推荐
本压缩包包含了USACO比赛的题解、源代码以及对应的中文翻译,对于想要参加或者正在准备USACO的同学们来说,无疑是一份宝贵的资源。 首先,让我们来详细了解USACO题解部分。USACO的比赛题目通常涉及各种算法,包括但...
usaco第五章milk4的解题代码,供算法初学者参考
我的USACO题解和程序
USACO 题解 USACO 题解是美国计算机奥林匹克(USACO)竞赛的题解集合,本文档提供了多个题目的解释和解决方案,涵盖了 Greedy Algorithm、Hash 表、动态规划、搜索等多种算法和技术。 Chapter 1 Section 1.1 Your ...
1. 题解:这些题解详细解释了如何理解和解决USACO比赛中的各种问题。通常会涵盖问题分析、算法设计、代码实现和时间复杂度分析等方面,有助于读者理解解决问题的关键思路。 2. 程序:每道题目的解决方案通常会有一...
这份压缩包包含了USACO训练教程的部分题解及中文译题,覆盖了从基础到进阶的多个章节,帮助学习者逐步提升编程和算法技能。 1. **基础篇(1.1.1)** - **数据结构基础**:在这一部分,通常会介绍数组、链表、栈和...
【USACO题解】全集包含了各类不同的编程竞赛题目,旨在帮助参赛者提升算法思维和编程能力。本文主要解析其中三个题目:“Your Ride Is Here (ride)”,“Greedy Gift Givers (gift1)”,以及“Friday the Thirteenth...
USACO(United States of America Computing Olympiad,美国信息学奥林匹克竞赛)是一个面向中学生的计算机编程竞赛,题解整理版中涉及的几个题目,下面将一一介绍它们的解题思路和涉及的关键知识点。 首先,...
"USACO题解(NOCOW整理版).pdf"可能是某个特定用户或团队整理的题解版本,可能包含了一些独特的解题方法或者技巧,或者是对原题解的补充和完善,使得学习者可以从不同的角度理解问题。 最后,"USACO全部测试数据.rar...
这个压缩包包含的是USACO历年月赛的试题,以及部分试题的数据和详细题解,是学习和准备USACO比赛的重要资源。 在C++编程语言的学习中,USACO的试题提供了丰富的实践机会,涵盖了基础数据结构(如数组、链表、栈、...
usaco全部题解。 网址:blog.csdn.net/jiangshibiao
### USACO Chap3 题解概览 #### Agri-Net (agrinet) - **知识点**:本题是一道经典的最小生成树问题。最小生成树问题是指在一个连通带权图中找到一棵包含所有顶点的树,使得这棵树上的所有边的权重之和最小。 - *...
《USACO题解》是针对美国计算机奥赛(USACO, USA Computing Olympiad)的一份详尽解析集,涵盖了从基础到高级的各种算法和编程技巧。USACO是一项旨在提升高中生计算机科学技能的国际竞赛,对于参赛者来说,理解和掌握...
usaco的某道题的题解
【标题】"usaco2.rar_milk" 是一个与USACO(美国计算机奥林匹克)相关的压缩文件,其中包含了几个编程竞赛题目。USACO是一个面向全球高中生的在线编程竞赛,旨在提升参赛者的算法和编程技能。这个压缩包特别关注的是...
"USACO题集及答案"这个资源包含了两部分关键内容:一是题解文档,如"USACO题解(NOCOW整理版).doc",这很可能是参赛者或教练整理的一份详尽的题目解析,其中可能包括了对每个问题的描述、数据范围、预期输出以及解决...
资源包包括USACO 2001-2007年月赛的测试数据;usaco月赛十年题典(2000-2009),usaco月赛2002-2008题解。单独下载需资源分30分以上。为了方便编程爱好者,我这边统一下载打包。欢迎下载。
USACO(即美国高中生的编程竞赛网站)中的 pprime 题的题解