POJ 2965
Test instructions
Enter a shape such as:
-+-----------+--
4*4 pattern, + means close,-represents open, defines an action: change a cell symbol (+ change-,-+), and all cell symbols in the row and column of the cell will change.
The minimum number of operations allows all cells to be '-'. and output the cells you want to manipulate.
Ideas:
The normal practice is similar to POJ 1573, DFS enumeration can be, see code1.
This provides an efficient approach:
By thinking we can verify that a single cell symbol is ' + ' and that it operates on all the cells in its row with the column (which itself operates only once), the original ' + ' cell element becomes '-' and the remaining cell symbols do not change.
So we can do this, for each ' + ' row of cells to operate (which itself only once), count the number of times each cell is manipulated, output odd number of operands of the cell.
Code See CODE2.
Reference: http://poj.org/showmessage?message_id=156561
AC Code1 (DFS):
/** @author novicer* language:c++/c*/#include <iostream> #include <sstream> #include <fstream># include<vector> #include <list> #include <deque> #include <queue> #include <stack># include<map> #include <set> #include <bitset> #include <algorithm> #include <cstdio># include<cstdlib> #include <cstring> #include <cctype> #include <cmath> #include <ctime># Include<iomanip> #define INF 2147483647#define CLS (x) memset (x,0,sizeof (x)) #define RISE (I,A,B) for (int i = A; I < = b; i++) using namespace Std;const double eps (1e-8); typedef long LONG lint;int ref[6][6] = {};int R[20],c[20];int flag = 0; int step;vector<pair<int,int> > Handle;bool all_open () {for (int i = 1, i <= 4; i++) for (int j = 1; J <= 4; J + +) if (!ref[i][j]) return False;return true; void change (int row, int. col) {for (int i = 1; I <= 4; i++) {Ref[i][col] =!ref[i][col];ref[row][i] =!ref[row][i];} Ref[row][col] =!ref[row][cOL];} void Dfs (int row, int col, int deep) {if (deep = = Step) {flag = All_open (); return;} if (flag) return;if (Row > 4 | | col > 4) return;r[deep] = Row;c[deep] = Col;change (Row,col), if (Row < 4) Dfs (row+1, Col, deep+1); Elsedfs (1, col+1, deep+1); change (row,col); if (Row < 4) Dfs (row+1, col, deep); Elsedfs (1,col+1,deep); Retu RN;} void input () {for (int i = 1, i <= 4; i++) for (int j = 1; J <= 4; j + +) {char tmp;cin >> tmp;ref[i][j] = (tmp == '-')? 1:0;}} void Solve () {for (step = 0; step <=, step++) {DFS (1,1,0); if (flag) break;} cout << Step << endl;for (int i = 0; i < step; i++) cout << r[i] << "<< C[i] << Endl;} int main () {//freopen ("Input.txt", "R", stdin),//freopen ("Output.txt", "w+", stdout); input (); solve (); return 0;}
AC Code2:
/** @author novicer* language:c++/c*/#include <iostream> #include <sstream> #include <fstream># include<vector> #include <list> #include <deque> #include <queue> #include <stack># include<map> #include <set> #include <bitset> #include <algorithm> #include <cstdio># include<cstdlib> #include <cstring> #include <cctype> #include <cmath> #include <ctime># Include<iomanip> #define INF 2147483647#define CLS (x) memset (x,0,sizeof (x)) #define RISE (I,A,B) for (int i = A; I < = b; i++) using namespace Std;const double eps (1e-8); typedef long LONG lint;int ref[6][6] = {};int r[20],c[20];void work (int row , int col) {Ref[row][col] =!ref[row][col];for (int i = 1; I <= 4; i++) {Ref[i][col] =!ref[i][col];ref[row][i] =!ref[ Row][i];}} void input () {for (int i = 1, i <= 4; i++) for (int j = 1; J <= 4; j + +) {char tmp;cin >> tmp;if (tmp = = ' + ') {wor K (i,j);}}} void Solve () {int ans = 0;for (int i = 1; I <= 4; i+ +) for (int j = 1; J <= 4; j + +) if (Ref[i][j]) {R[ans] = I;c[ans] = j;ans++;} cout << ans << endl;for (int i = 0; i < ans; i++) printf ("%d%d\n", R[i],c[i]);} int main () {//freopen ("Input.txt", "R", stdin); input (); solve (); return 0;}
Copyright notice: Bo Master said authorized all reproduced:)
POJ 2965 the pilots ' refrigerator (idea title)