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;}