USACO Combination Lock 解题报告
2014-10-04 04:45
344 查看
新加的一道题,标准答案为把所有的数都试一遍。我的解法是把所有的可能性都加到一个set里面,这样避免了两个key允许的数重叠的情况,是一种偷懒的做法。
/* ID: thestor1 LANG: C++ TASK: combo */ #include <iostream> #include <fstream> #include <cmath> #include <cstdio> #include <cstring> #include <climits> #include <cassert> #include <string> #include <vector> #include <set> #include <map> #include <queue> #include <stack> #include <algorithm> using namespace std; set<int> adjs(int d, int N) { std::set<int> fours; fours.insert(d); fours.insert((d - 1 + N - 1) % N + 1); fours.insert((d - 1 + N - 2) % N + 1); fours.insert((d - 1 + N + 1) % N + 1); fours.insert((d - 1 + N + 2) % N + 1); return fours; } int tohash(int d1, int d2, int d3, int N) { return d1 * (N + 1) * (N + 1) + d2 * (N + 1) + d3; } int main() { ifstream fin("combo.in"); ofstream fout("combo.out"); int N; fin>>N; std::vector<vector<int> > keys(2, std::vector<int>(3, 0)); for (int i = 0; i < 2; ++i) { for (int j = 0; j < 3; ++j) { int dial; fin>>dial; keys[i][j] = dial; } } // for (int i = 0; i < 2; ++i) // { // cout<<"key "<<i<<":"; // for (int j = 0; j < 3; ++j) // { // cout<<keys[i][j]<<"\t"; // } // cout<<endl; // } set<int> validkeys; for (int k = 0; k < 2; ++k) { set<int> firstadjs = adjs(keys[k][0], N); set<int> secondadjs = adjs(keys[k][1], N); set<int> thirdadjs = adjs(keys[k][2], N); for (set<int>::iterator iter1 = firstadjs.begin(); iter1 != firstadjs.end(); ++iter1) { for (set<int>::iterator iter2 = secondadjs.begin(); iter2 != secondadjs.end(); ++iter2) { for (set<int>::iterator iter3 = thirdadjs.begin(); iter3 != thirdadjs.end(); ++iter3) { // cout<<*iter1<<", "<<*iter2<<", "<<*iter3<<endl; int h = tohash(*iter1, *iter2, *iter3, N); validkeys.insert(h); } } } } fout<<validkeys.size()<<endl; fin.close(); fout.close(); return 0; }
相关文章推荐
- USACO Combination Lock 解题日志
- BZOJ 4102 [Usaco2015 Open] 图上DP 解题报告
- USACO Section 1.4 Arithmetic Progressions 解题报告
- USACO 2016 JAN——Angry Cows套题的解题报告
- USACO Snail Trails 解题报告
- bzoj 1631: [Usaco2007 Feb]Cow Party 解题报告
- 【LeetCode】216.Combination Sum III(Medium)解题报告
- USACO Ski Course Design 解题报告
- USACO Wisconsin Squares 解题报告
- usaco垃圾陷阱解题报告
- USACO 3.1解题思路报告
- USACO 3.3解题思路报告
- USACO 1.3... 虫洞 解题报告(搜索+强大剪枝+模拟)
- USACO Drainage Ditches解题报告
- Usaco 4.3.1 Buy Low, Buy Lower 逢低吸纳详细解题报告
- USACO Shuttle Puzzle 解题报告
- USACO Fencing the Cows 解题报告
- USACO Section2.1 Ordered Fractions 解题报告
- USACO历年比赛的数据和解题报告
- USACO Picture 解题报告