POJ 2965 The Pilots Brothers' refrigerator(枚举+dfs)

来源:互联网 发布:淘宝 中药材 编辑:程序博客网 时间:2024/06/07 10:13




http://poj.org/problem?id=2965





分析:

和 POJ 1573 一样的题目  加一下路径记录   翻时候是它所在的行列都要翻 






AC代码:

#include <stdio.h>#include <string.h>char map[20][20];int temp[20][20];int step;int flag;int judge(){    for (int i=0;i<4;i++){        for (int j=0;j<4;j++){            if(map[i][j]!='-')                return 0;        }    }    return 1;}void oper(int x,int y){    if (map[x][y]=='+')        map[x][y]='-';    else    map[x][y]='+';    for(int i=0;i<4;i++){    if (map[x][i]=='+')    map[x][i]='-';    else  map[x][i]='+';} for (int j=0;j<4;j++){if (map[j][y]=='+')    map[j][y]='-';    else  map[j][y]='+';}}void dfs(int x,int y,int count){    if(count==step){    if((flag=judge())){    printf ("%d\n",step);    for (int i=0;i<4;i++){    for (int j=0;j<4;j++){    if(temp[i][j])    printf ("%d %d\n",i+1,j+1);}}}    return ;}if(flag||x==4)return;oper(x,y);temp[x][y]=1;    if(y<3)    dfs(x,y+1,count+1);    else    dfs(x+1,0,count+1); temp[x][y]=0;       oper(x,y);    if(y<3)    dfs(x,y+1,count);    else    dfs(x+1,0,count);}int main (){    memset(map,0,sizeof(map));    for (int i=0;i<4;i++){        scanf ("%s",map[i]);    }     for(step=0;step<=16;step++){    flag=0;memset(temp,0,sizeof(temp));    dfs(0,0,0);    if(flag)    break;}      return 0;}


原创粉丝点击