POJ 2965 The Pilots Brothers' refrigerator (DFS)
来源:互联网 发布:rpc java 一般 编辑:程序博客网 时间:2024/06/06 06:16
The Pilots Brothers’ refrigerator
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 23895 Accepted: 9205 Special Judge
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
6
1 1
1 3
1 4
4 1
4 3
4 4
题意:给你4*4个开关,当你点击一个开关时,这个开关所处的行和列的开关都会被点击,问你,需要点击几次开关可以使得所有开关都为-。
思路:直接枚举状态就好了,枚举复杂度为
ac代码:
/* *********************************************** Author : AnICoo1 Created Time : 2016-07-27-14.42 Wednesday File Name : D:\MyCode\2016-7月\2016-7-27-3.cpp LANGUAGE : C++ Copyright 2016 clh All Rights Reserved ************************************************ */#include<stdio.h>#include<math.h>#include<string.h>#include<stack>#include<set>#include<map>#include<queue>#include<vector>#include<iostream>#include<algorithm>#define MAXN 1010000#define LL long long#define ll __int64#define INF 0xfffffff#define mem(x) memset(x,0,sizeof(x))#define PI acos(-1)#define eps 1e-8using namespace std;ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}ll lcm(ll a,ll b){return a/gcd(a,b)*b;}ll powmod(ll a,ll b,ll MOD){ll ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}double dpow(double a,ll b){double ans=1.0;while(b){if(b%2)ans=ans*a;a=a*a;b/=2;}return ans;}//headchar s[5][5];int ans;int num[20];int v[MAXN],vis[30];int A[20];int bz;void DFS(int id,int cnt){ if(bz) return ; if(id==17) { int k=0,kk=1; for(int i=16;i>=1;i--) { int c=num[i]; if(vis[i]%2) c=num[i]?0:1; k+=c*kk;kk*=2; } if(k==0) { if(ans>cnt) { ans=cnt; for(int i=1;i<=16;i++) A[i]=v[i]; } bz=1; } return ; } int k=id%4?id/4+1:id/4; DFS(id+1,cnt); v[id]=1;vis[id]++;for(int i=id+4;i<=16;i+=4) vis[i]++;for(int i=id-4;i>=1;i-=4) vis[i]++;for(int i=k*4;i>=k*4-3;i--) if(i!=id) vis[i]++; DFS(id+1,cnt+1); v[id]=0;vis[id]--;for(int i=id+4;i<=16;i+=4) vis[i]--;for(int i=id-4;i>=1;i-=4) vis[i]--;for(int i=k*4;i>=k*4-3;i--) if(i!=id) vis[i]--;}int main(){ while(scanf("%s",s[0])!=EOF) { for(int i=1;i<4;i++) scanf("%s",s[i]); for(int i=0;i<4;i++) { for(int j=0;j<4;j++) { if(s[i][j]=='+') num[i*4+j+1]=1; else num[i*4+j+1]=0; } } mem(A);mem(vis);mem(v);ans=INF;bz=0; DFS(1,0); printf("%d\n",ans); for(int i=1;i<=16;i++) { if(A[i]) { int x=i%4?i/4+1:i/4; int y=i%4?i%4:i/x; printf("%d %d\n",x,y); } } } return 0;}
- POJ 2965 The Pilots Brothers' refrigerator(DFS)
- POJ 2965 The Pilots Brothers' refrigerator (DFS)
- poj 2965 The Pilots Brothers' refrigerator(dfs)
- poj 2965 The Pilots Brothers' refrigerator(dfs)
- poj The Pilots Brothers' refrigerator(DFS)
- POJ 2965 The Pilots Brothers' refrigerator【枚举+dfs】
- poj 2965 The Pilots Brothers' refrigerator (迭代加深dfs)
- POJ 2965 The Pilots Brothers' refrigerator (DFS)
- POJ 2965 The Pilots Brothers' refrigerator(dfs+记录路径)
- POJ 2965 The Pilots Brothers' refrigerator(dfs+枚举 || 规律)
- POJ 2965 The Pilots Brothers' refrigerator 枚举dfs
- POJ 2965-The Pilots Brothers' refrigerator(枚举&&DFS&&输出过程)
- POJ 2965 The Pilots Brothers' refrigerator(回溯查找,dfs)
- POJ 2965 The Pilots Brothers' refrigerator(dfs or 规律)
- POJ 2965 The Pilots Brothers' refrigerator(枚举+dfs)
- The Pilots Brothers' refrigerator(POJ 2965)(dfs枚举+状态压缩)
- POJ 2965 The Pilots Brothers' refrigerator(枚举+DFS)
- POJ 2965 The Pilots Brothers' refrigerator (DFS + 枚举)
- eclipse svn插件展示作者、修改日期等label
- Hello java web
- 网易彩票,转盘实现
- PHP 入门学习笔记(一)
- 5.3 编写一个程序,显示用户输入的一个日期是该年度的第几天。
- POJ 2965 The Pilots Brothers' refrigerator (DFS)
- Ubuntu下boot分区剩余空间不足解决
- freemarker的应用
- Python用csv写入文件---消除空余行的技巧
- 从 SVN 到 git bucket
- CSS 学习笔记思维导图版
- 前端常用到的颜色值和长度值设置
- CodeForces 691C Exponential notation
- HDU 2188 悼念512汶川大地震遇难同胞——选拔志愿者(巴什博奕)