USACO Section 2.2: Party Lamps
2013-07-07 15:21
351 查看
这题有个小技巧, 按一个键两次等于没按,所以如果depsum > 16的话其实不用做深搜到depsum次,而只要16次就可以了。
/* ID: yingzho1 LANG: C++ TASK: lamps */ #include <iostream> #include <fstream> #include <string> #include <map> #include <vector> #include <set> #include <algorithm> #include <stdio.h> #include <queue> #include <cstring> using namespace std; ifstream fin("lamps.in"); ofstream fout("lamps.out"); int N, depsum; bool check(string s, vector<int> &on, vector<int> &off) { for (int i = 0; i < on.size(); i++) { if (s[on[i]] != '1') return false; } for (int i = 0; i < off.size(); i++) { if (s[off[i]] != '0') return false; } return s.size(); } void dfs(set<string> &res, int c1, int c2, int c3, int c4, string s, vector<int> &on, vector<int> &off, int dep) { if (check(s, on, off)) { if (dep <= depsum && (depsum-dep)%2 == 0) { res.insert(s); //return; } } if (dep == 4 || dep > depsum) return; string pre = s; if (c1 < 1) { dfs(res, c1+1, c2, c3, c4, s, on, off, dep+1); for (int i = 0; i < s.size(); i++) { if (s[i] == '0') s[i] = '1'; else s[i] = '0'; } dfs(res, c1+1, c2, c3, c4, s, on, off, dep+1); } s = pre; if (c2 < 1) { dfs(res, c1, c2+1, c3, c4, s, on, off, dep+1); for (int i = 0; i < s.size(); i++) { if (i % 2 == 1) { if (s[i] == '1') s[i] = '0'; else s[i] = '1'; } } dfs(res, c1, c2+1, c3, c4, s, on, off, dep+1); } s = pre; if (c3 < 1) { dfs(res, c1, c2, c3+1, c4, s, on, off, dep+1); for (int i = 0; i < s.size(); i++) { if (i % 2 == 0) { if (s[i] == '1') s[i] = '0'; else s[i] = '1'; } } dfs(res, c1, c2, c3+1, c4, s, on, off, dep+1); } s = pre; if (c4 < 1) { dfs(res, c1, c2, c3, c4+1, s, on, off, dep+1); for (int i = 0; i < s.size(); i++) { if (i % 3 == 0) { if (s[i] == '1') s[i] = '0'; else s[i] = '1'; } } dfs(res, c1, c2, c3, c4+1, s, on, off, dep+1); } s = pre; } int main() { fin >> N >> depsum; string s(N, '1'); int n; vector<int> on, off; //cout << N << " " << depsum << endl; while (fin >> n && n != -1) on.push_back(n-1); while (fin >> n && n != -1) off.push_back(n-1); //for (int i = 0; i < on.size(); i++) cout << on[i] << endl; //for (int i = 0; i < off.size(); i++) cout << off[i] << endl; set<string> res; dfs(res, 0, 0, 0, 0, s, on, off, 0); vector<string> ret; for (set<string>::iterator it = res.begin(); it != res.end(); it++) ret.push_back(*it); sort(ret.begin(), ret.end()); if (ret.size() == 0) { fout << "IMPOSSIBLE" << endl; return 0; } for (int i = 0; i < ret.size(); i++) { fout << ret[i] << endl; } return 0; }
相关文章推荐
- USACO Section 2.2 Party Lamps - 看清本质就简单!
- USACO-Section2.2 Party Lamps【深度优先搜索】
- USACO - Chapter2 Section 2.2 - Party Lamps
- USACO section2.2 Party Lamps题解&思路
- USACO-Section 2.2 Party Lamps (枚举)
- USACO Section 2.2 Party Lamps
- USACO-Section2.2 Party Lamps [枚举]
- USACO Section 2.2 Party Lamps
- USACO section2.2 Subset Sums题解&代码
- USACO 2.2 Party Lamps
- USACO Section 2.2 Subset Sums
- USACO - Chapter2 Section 2.2 - Hamming Codes
- USACO 之 Section 2.2 (已解决)
- USACO section2.2 Runaround Numbers题解&代码
- USACO 2.2 Party Lamps 派对灯 (lamps)
- USACO - Chapter2 Section 2.2 - Preface Numbering
- [USACO 2.2] Summary of Section 2.2
- USACO - Chapter2 Section 2.2 - Subset Sums
- USACO 2.2 Party Lamps(乱搞)
- USACO-Section2.2 preface[贪心]