poj1222

/*比较简单的搜索,对第一行进行穷举,然后根据第一行确定剩下的格子状态*/

#include <iostream>

using namespace std;

int n = 0;

int a[5][6];

int temp[5][6];

int res[5][6];

void InitFistLine(int num);

void Solve();

bool IsSuccessful();

void OutPrint();

void Press(int r,int c);

void InitTial();

int main()  

{  

cin >> n;

for (int i = 0; i < n; ++i)

{

for (int j = 0; j < 5; ++j)

for (int k = 0; k < 6; ++k)

{

cin >> a[j][k];

temp[j][k] = a[j][k];

}

InitTial();

for (int j = 0; j < 128; ++j)

{

InitFistLine(j);

Solve();

if (IsSuccessful())

{

cout << "PUZZLE #" << i+1 << endl;

OutPrint();

break;

}

InitTial();

}

}

return 0;

}

 

void InitTial()

{

for (int i = 0; i < 5; ++i)

for (int j = 0; j < 6; ++j)

{

temp[i][j] = a[i][j];

res[i][j] = 0;

}

}

 

void Solve()

{

for (int i = 0; i < 6; ++i)

if (res[0][i])

{

Press(0,i);

}

for (int i = 0; i < 4; ++i)

for (int j = 0; j < 6; ++j)

{

if (temp[i][j] == 1)

{

res[i+1][j] = 1;

Press(i+1,j);

}

}

}

 

void Press(int r,int c)

{

if (r-1 >= 0)

temp[r-1][c] = 1-temp[r-1][c];

if (r+1 < 5)

temp[r+1][c] = 1-temp[r+1][c];

if (c-1 >= 0)

temp[r][c-1] = 1-temp[r][c-1];

if (c+1 < 6)

temp[r][c+1] = 1-temp[r][c+1];

temp[r][c] = 1-temp[r][c];

}

void InitFistLine(int num)

{

for (int i = 5; i >= 0; --i)

{

res[0][i] = num%2;

num /= 2; 

}

}

 

void OutPrint()

{

for (int i = 0; i < 5; ++i)

for (int j = 0; j < 6; ++j)

{

cout << res[i][j] << (j == 5?"/n":" ");

}

}

 

 

 

bool IsSuccessful()

{

for (int i = 0; i < 5; ++i)

for (int j = 0; j < 6; ++j)

{

if (temp[i][j])

{

return false;

}

}

return true;

}

你可能感兴趣的:(poj1222)