本文主要是介绍熄灯问题POJ1222,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
// 熄灯问题POJ1222
#include <iostream>
#include <cstring>
using namespace std;const int ROW = 5;
const int COL = 6;void change(char lamp[], int row, int col) {if (row >= 0 && row < ROW && col >= 0 && col < COL)lamp[row] ^= (1 << col);
}void press(char lamp[], int row, int col) {change(lamp, row, col); change(lamp, row-1, col); change(lamp, row+1, col); change(lamp, row, col-1); change(lamp, row, col+1);
}void pressline(char lamp[], int row, int bits) {for (int i = 0; i < COL; i++)if ((1 << i) & bits)press(lamp, row, i);
}void input(char lamp[]) {for (int i = 0; i < ROW; i++) {lamp[i] = 0;for (int j = 0; j < COL; j++) {int n;cin >> n;if (n)lamp[i] |= 1 << j;}}
}void output(char state[]) {for (int i = 0; i < ROW; i++) {for (int j = 0; j < COL; j++)if (state[i] & (1 << j))cout << "1 ";elsecout << "0 ";cout << endl;}
}void separator(char ch) {for (int i = 0; i < COL * 2 - 1; i++)cout << ch;cout << endl;
}void process(char lamp[], char turn[]) {char light[ROW];memcpy(light, lamp, sizeof(char) * ROW);for (int i = 0; i < ROW; i++) {pressline(light, i, turn[i]);separator('-');output(light);}
}bool solve(char lamp[], char turn[]) {for (int i = 0; i < (1 << COL); i++) {char light[ROW];memcpy(light, lamp, sizeof(char) * ROW);turn[0] = i;pressline(light, 0, i);for (int j = 1; j < ROW; j++) {turn[j] = light[j-1]; pressline(light, j, turn[j]);}if (light[ROW-1] == 0)return true;}return false;
}int main() {char lamp[ROW];char turn[ROW];freopen("熄灯问题.in", "r", stdin);input(lamp);output(lamp);solve(lamp, turn);#ifdef DEBUGprocess(lamp, turn);#endifseparator('=');output(turn);return 0;
}
这篇关于熄灯问题POJ1222的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!