poj 1753
来源:互联网 发布:mysql 修复 编辑:程序博客网 时间:2024/05/20 05:59
熄灯问题
需要进行 2^4 次枚举即可
不用递归
#include <iostream>#include <vector>#include <map>#include <list>#include <set>#include <deque>#include <stack>#include <queue>#include <algorithm>#include <cmath>#include <cctype>#include <cstdio>#include <iomanip>#include <cmath>#include <cstdio>#include <iostream>#include <string>#include <sstream>#include <cstring>#include <queue>using namespace std;///宏定义const int INF = 20000000;const int MAXN = 500050;///全局变量 和 函数int maze[10][10];int maze1[10][10];int temp[10][10];bool flag;bool flip(int ×, int maze[][10]){ int c, r; for(r = 1; r < 4; r++) { for(c = 1; c < 5; c++) { temp[r + 1][c] = (maze[r][c] + temp[r][c] + temp[r - 1][c] + temp[r][c - 1] + temp[r][c + 1]) % 2; //开关状态决定方式 } } for(c = 1; c < 5; c++) { if( (temp[4][c - 1] + temp[4][c] + temp[4][c + 1] + temp[3][c]) % 2 != maze[4][c] ) //判断是否可行 return false; } int sum = 0; for(r = 1; r < 5; r++) { for(c = 1; c < 5; c++) { if(temp[r][c] != 0) sum++; } } flag = true; times = sum; return true;}int main(){freopen("D:\\input.txt", "r", stdin);///变量定义int i, j, k;///操作执行flag = false; for(i = 1; i <= 4; i++) { string tmp; cin >> tmp; for(j = 1; j <= 4; j++) { if(tmp[j - 1] == 'b') { maze[i][j] = 0; maze1[i][j] = 1; } else { maze[i][j] = 1; maze1[i][j] = 0; } } } //模拟2^4方种可能的情况 //对于全黑 0表示黑 1表示白 int c, r; int minsteps; for(i = 0; i < (1 << 4); i++) { //初始化temp第一行 for(c = 1; c < 5; c++) { temp[1][c] = i & (1 << (c - 1)); } int tmpNum; if(flip(tmpNum, maze)) { if(tmpNum < minsteps) minsteps = tmpNum; } } //另一种情况 for(i = 0; i < (1 << 4); i++) { //初始化temp第一行 for(c = 1; c < 5; c++) { temp[1][c] = 0; temp[1][c] = (i & (1 << (c - 1)) ) >> (c - 1); //这一句开始没写对,位运算。。。 } int tmpNum; if(flip(tmpNum, maze1)) { if(tmpNum < minsteps) minsteps = tmpNum; } } if(flag) cout << minsteps << endl; else cout << "Impossible" << endl; ///结束return 0;}
- poj 3279 poj 1753
- POJ 1753
- poj&&1753
- poj 1753
- poj 1753
- POJ-1753
- poj-1753
- poj 1753
- poj 1753
- poj 1753
- POJ 1753
- POJ 1753
- poj 1753
- poj-1753
- POJ 1753
- POJ 1753
- POJ 1753
- poj 1753
- 面向对象的程序设计-9-赋值运算符重载
- ..vimrc 2
- gbk与utf-8的区别
- js加密
- 用Js的eval解析JSON
- poj 1753
- 京沪粤方案同日出炉 比其它省份“门槛”高
- USB设备的probe是如何引发的?
- centralWidget中显示多个widget的实现与细节
- 字符串3
- 使用Orange进行数据挖掘之分类(2)------KNN分类
- 【生活日记】最近心态不好啊,沉不住气
- WIN 7 X64位在右键中添加UltraEdit-32
- Android的3D旋转