枚举——POJ1753
2015-12-03 18:22
267 查看
POJ 1753
最后:
1. 2^16=65536 ,如果设置的数组大小为65535,那么
输入:
bbbb
bbbb
bbbb
bbbb
输出则是 溢出的
2. bitset<> 会比^ | &方便使用
3.广度优先遍历使用队列作为基本的数据结构,深度优先算法依赖于栈(在递归中显示或隐士地使用栈)
4.做ACM..不是为了成为ACMer,只是想对数据结构和算法有一个应用实践
#include<iostream> #include<bitset> #include<queue> using namespace std; queue<bitset<16>> state; //record the state of each situation bool flag[65536] = { false }; <span style="white-space:pre"> </span>//in case repeated search int step[65536] ; void Initialize() { cout << "Please input a 4*4 rectangle while 'b' represents 'black' " << "and 'w' represents 'white':\n"; char c; int i = 0, j = 0; bitset<16> temp; for (i = 0; i < 4; i++) { for (j = 0; j < 4; j++) { cin >> c; if ('b' == c) temp.set(4 * i + j); } } state.push(temp); flag[temp.to_ulong()] = true; } bitset<16> Flip(bitset<16> piece, int i) { if (i % 4 != 0) //not in the first column piece.reset(i - 1); if ((i + 1) % 4 != 0) //not in the fourth column piece.reset(i + 1); if (i > 3) //not in the first row piece.reset(i - 4); if (i < 12) //not in the fourth row piece.reset(i + 4); return piece; } bool breadFirstSearch() { while (!state.empty()) { bitset<16> firstState = state.front(); state.pop(); //queue.pop() do not return the value of the first item! for (int i = 0; i < 16; i++) { bitset<16> temp = Flip(firstState, i); if (firstState.none() || (firstState.count() == 16)) { cout << step[firstState.to_ulong()]; return true; } else if(!flag[temp.to_ulong()]) { state.push(temp); flag[temp.to_ulong()] = true; step[temp.to_ulong()] = step[firstState.to_ulong()] + 1; } } } return false; } int main() { Initialize(); if (!breadFirstSearch()) cout << "Impossible!"; return 0; }
最后:
1. 2^16=65536 ,如果设置的数组大小为65535,那么
输入:
bbbb
bbbb
bbbb
bbbb
输出则是 溢出的
2. bitset<> 会比^ | &方便使用
3.广度优先遍历使用队列作为基本的数据结构,深度优先算法依赖于栈(在递归中显示或隐士地使用栈)
4.做ACM..不是为了成为ACMer,只是想对数据结构和算法有一个应用实践
相关文章推荐
- 枚举的用法详细总结
- c#入门之枚举和结构体使用详解(控制台接收字符串以相反的方向输出)
- 理解C#中的枚举(简明易懂)
- C#枚举中的位运算权限分配浅谈
- C++基础入门教程(四):枚举和指针
- FileShare枚举的使用小结(文件读写锁)
- Java枚举类用法实例
- 枚举窗口句柄后关闭所有窗口示例
- javascript模拟枚举的简单实例
- 深入剖析JavaScript中的枚举功能
- 枚举和宏的区别详细解析
- 深入理解C#中的枚举
- C#实现获取枚举中元素个数的方法
- java中枚举的详细使用介绍
- java枚举的使用示例
- Java枚举详解及使用实例(涵盖了所有典型用法)
- 深入解析Swift编程中枚举类型的相关使用
- 一个运用enum类型的Demo
- 枚举隐式派生于System.Enum
- 枚举的隐士转换跟显示转换