The Pilots Brothers' refrigerator-DFS路径打印

来源:互联网 发布:防水纹身贴淘宝 编辑:程序博客网 时间:2024/06/10 20:13
I - The Pilots Brothers' refrigerator
Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
Submit Status

Description

The game “The Pilots Brothers: following the stripy elephant” has a quest where a player needs to open a refrigerator.

There are 16 handles on the refrigerator door. Every handle can be in one of two states: open or closed. The refrigerator is open only when all handles are open. The handles are represented as a matrix 4х4. You can change the state of a handle in any location [i, j] (1 ≤ i, j ≤ 4). However, this also changes states of all handles in row i and all handles in column j.

The task is to determine the minimum number of handle switching necessary to open the refrigerator.

Input

The input contains four lines. Each of the four lines contains four characters describing the initial state of appropriate handles. A symbol “+” means that the handle is in closed state, whereas the symbol “−” means “open”. At least one of the handles is initially closed.

Output

The first line of the input contains N – the minimum number of switching. The rest N lines describe switching sequence. Each of the lines contains a row number and a column number of the matrix separated by one or more spaces. If there are several solutions, you may give any one of them.

Sample Input

-+-----------+--

Sample Output

61 11 31 44 14 34 4
/*Author: 2486Memory: 144 KBTime: 360 MSLanguage: C++Result: Accepted*/#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxn=1<<18;char maps[5][5];bool vis[maxn];int path[maxn];//记录最终路径int pathvis[maxn];//记录DFS过程中的路径int Min;int binary(int val,int x,int y) {    for(int i=0; i<16; i++) {//选取行列进行取反        if(i/4==x||i%4==y) {            val^=(1<<i);        }    }    return val;}void dfs(int s,int step,int x,int y) {    if(s==0) {        if(Min>step) {            Min=step;            for(int i=0;i<step;i++){                path[i]=pathvis[i];//如果比先前的步数更少就转到存储最终路径的数组中            }        }        return;    }    if(x>=4)return;    int nx,ny;    if(y+1>=4) {//向右走然后向下走        nx=x+1;        ny=0;    } else {        ny=y+1;        nx=x;    }    int fd=x*4+y;    int k=binary(s,x,y);    pathvis[step]=fd;//当前的位置进行反转    dfs(k,step+1,nx,ny);    dfs(s,step,nx,ny);}int main() {    while(~scanf("%s",maps[0])) {        for(int i=1; i<4; i++) {            scanf("%s",&maps[i]);        }        int s=0;        for(int i=3; i>=0; i--) {            for(int j=3; j>=0; j--) {                if(maps[i][j]=='+')s=s<<1|1;                else s<<=1;            }        }        if(s==0) {            printf("0\n");            continue;        }        Min=10000000;        dfs(s,0,0,0);        printf("%d\n",Min);        for(int i=0; i<Min; i++) {            printf("%d %d\n",path[i]/4+1,path[i]%4+1);        }    }    return 0;}


0 0
原创粉丝点击