本文主要是介绍poj 2965 The Pilots Brothers' refrigerator 普通dfs 超时 暑假第二题,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
代码:
//#include<iostream>
#include<string.h>
#include<stdio.h>
//using namespace std;
int step=0;
int visit[5][5]= {0};
char a[5][5];
bool b[5][5];
bool c[5][5];
int k=0;
struct Str
{int x;int y;
};
Str d[50];
void roll(int row,int col)
{int i,j;for(i=1; i<=4; i++){b[row][i]=!b[row][i];b[i][col]=!b[i][col];}b[row][col]=!b[row][col];
}
bool Judge()
{int i,j;for(i=1; i<=4; i++)for(j=1; j<=4; j++)if(b[i][j]==0)return 0;return 1;
}
void huanyuan()
{int i,j;for(i=1;i<=4;i++){for(j=1;j<=4;j++){b[i][j]=c[i][j];}}
}
void dfs(int depth)
{int i,j;if(depth==step){if(Judge()==1){k=1;printf("%d\n",step);//cout<<step<<endl;for(i=0; i<depth; i++){printf("%d %d\n",d[i].x,d[i].y);//cout<<d[i].x<<' '<<d[i].y<<endl;}return ;}return ;}for(i=1; i<=4; i++){for(j=1; j<=4; j++){if(visit[i][j]==0&&k==0){visit[i][j]=1;d[depth].x=i;d[depth].y=j;roll(i,j);dfs(depth+1);visit[i][j]=0;roll(i,j);}}}
}
int main()
{int i,j;memset(visit,0,sizeof(visit));for(i=1; i<=4; i++){for(j=1; j<=4; j++){scanf("%c",&a[i][j]);// cin>>a[i][j];if(a[i][j]=='+')b[i][j]=0;elseb[i][j]=1;//开c[i][j]=b[i][j];}getchar();}for(step=1; step<=16; step++){dfs(0);if(k)break;}return 0;
}
这篇关于poj 2965 The Pilots Brothers' refrigerator 普通dfs 超时 暑假第二题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!