Test instructions: There are 4x4 switches, each change the state of a switch, will simultaneously change the same row and the same row of the state of the switch, give the initial state, the minimum number of steps required to turn all the switches into open, and output scheme.
Solution: Enumeration + pruning. Direct violence enumeration unexpectedly t ... Feel not too scientific ... The complexity of 2^16*16 ... Had to add a pruning, record the best answers that have been enumerated, and then just enumerate to the best answers, if the steps that exceed the best answer are definitely not the best solution.
Code:
#include <stdio.h> #include <iostream> #include <algorithm> #include <string> #include < string.h> #include <math.h> #include <limits.h> #include <time.h> #include <stdlib.h># include<map> #include <queue> #include <set> #include <stack> #include <vector> #define LL Long longusing namespace Std;int main () {iOS:: Sync_with_stdio (false); Char maze[4][5]; while (Cin >> Maze[0]) {for (int i = 1; i < 4; i++) cin >> Maze[i]; int MAXN = 1 << 16; int ans = 1000000, pos = 0; for (int i = 0; i < MAXN; i++) {int res = 0; int cnt[4][4] = {0}; int flag = 1; for (int j = 0; J <; J + +) {if (I & (1 << j)) { res++; if (res > ans)//pruning {flag = 0; Break } for (int k = 0; k < 4; k++) {cnt[j/4][k]++; Cnt[k][j% 4]++; } cnt[j/4][j% 4]--; }} for (int j = 0; J < 4 && Flag; j + +) for (int k = 0; k < 4 && Flag k++) {int tmp = maze[j][k] = = ' + '? 1:0; TMP + = Cnt[j][k]; if (TMP & 1) flag = 0; } if (Flag && (res < ans)) {ans = res; pos = i; }} if (ans! = 1000000) cout << ans << endl; else cout << "0" << Endl; for (int i = 0; i < i++) {if (pos & (1 << i)) {cout << I/4 + 1 << ' << i% 4 + 1 << endl; } }} return 0;}
POJ 2965 the pilots ' refrigerator