Original title Link: http://poj.org/problem?id=2965
Simple DFS, because it is required minimum value, enumeration depth to get the recursive termination condition, and poj1753 very much like, after all, is put together.
- #include <iostream>
- #include <cstdio>
- #include <queue>
- #include <cstring>
- #include <cmath>
- #include <cstdlib>
- #include <vector>
- #include <string>
- #define CLR (arr) memset (arr,0,sizeof(arr))
- #define N 100001
- #define MOD 1e9+7
- //#define LOCAL
- typedef LONG long ll;
- using namespace std;
- int cmp (const void*c,const void*d) {return 1;} //above a lot of basic no eggs, just to use the time no more dozen, anyway compile too long and not suffer
- Char handle[4][4];
- int h[6][6];
- int step,flag,fa[2][50],top;
- void Flip (int row,int col) {
- for (int i=1;i<=4;i++) h[i][col]^=1;
- for (int j=1;j<=4;j++) if (J!=col) H[row][j]^=1; //Make the row flip with the column
- return;
- }
- bool isopen () {
- for (int i=1;i<=4;i++)
- for (int j=1;j<=4;j++)
- if (H[i][j]) return false;
- return true; //Determine if it is open
- }
- void dfs (int row,int col,int deep) {
- if (Deep==step) {
- if (IsOpen ()) {
- printf ("%d\n", step);
- for (int i=0;i<top;i++) printf ("%d%d\n", Fa[0][i],fa[1][i]);
- flag=1;
- }
- return;
- }
- if (Flag| | row==5)return ;
- Fa[0][top]=row;
- Fa[1][top++]=col; //Save the path, in fact, you can change the DFS to take the int return value, add judgment to save, perhaps also can reduce time
- Flip (Row,col);
- if (col<4)
- DFS (ROW,COL+1,DEEP+1);
- Else
- DFS (ROW+1,1,DEEP+1);
- top--;
- Flip (Row,col);
- if (col<4)
- DFS (ROW,COL+1,DEEP);
- Else
- DFS (ROW+1,1,DEEP);
- return;
- }
- int Main ()
- {
- #ifdef LOCAL
- Freopen ("input.txt", "R", stdin);
- //freopen ("Output.txt", "w", stdout);
- #endif
- for (int i=0;i<4;i++) {
- scanf ("%s", Handle[i]);
- for (int j=1;j<=4;j++) h[i+1][j]= (handle[i][j-1]== ' + ');
- }
- for (Step=1;; step++) {
- DFS (1,1,0);
- if (flag) break;
- }
- return 0;
- }
I can spit groove I actually do not understand what is the content of the surface? In fact, this water problem is not too shy to put up, but as a souvenir, after all, POJ training first gun.
POJ 2965 the pilots ' refrigerator