POJ 2965 The Pilots Brothers' refrigerator 枚举
2013-08-30 14:32
483 查看
Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 15784 | Accepted: 5950 | Special Judge |
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 rowi and all handles in columnj.
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
这道题和上面我发的那道POJ1753很像,应该都算枚举吧,和POJ1753一样,这道题如果一个冰箱门被打开大于2次是没有意义的,具体做法就是先直接判断,当前冰箱门是否全开,如果是,输出0,否则就要利用全组合模板进行运算了,充分运用全组合的模板可以有效节省时间和内存,防止TLE,在每次全组合模板得到结果的时候对当前序列进行开门关门处理,然后判断是否全开,全开的时候记录当前序列,然后退出全组合函数,输出当前判断的开门数,以及在全组合里面记录下的数列对应的坐标,当然,毕竟是简单粗暴,依然还是很费时间的,时间达到了719MS,再有300MS也TLE了。
具体的AC代码如下:
#include<cstdio> #include<iostream> using namespace std; struct st { int x; int y; }point[25]; int pa[20]; char a[5][5],b[5][5]; void init() { int i,j,k=1; for(i=1;i<=4;i++) for(j=1;j<=4;j++) { point[k].x=i; point[k].y=j; k++; } } int judge() { int i,j; for(i=1;i<=4;i++) for(j=1;j<=4;j++) if(b[i][j]=='+') return 0; return 1; } void change(int x,int y) { int i; for(i=1;i<=4;i++) { if(b[x][i]=='+') b[x][i]='-'; else b[x][i]='+'; if(b[i][y]=='+') b[i][y]='-'; else b[i][y]='+'; } if(b[x][y]=='+') b[x][y]='-'; else b[x][y]='+'; } void ret() { int i,j; for(i=1;i<=4;i++) for(j=1;j<=4;j++) b[i][j]=a[i][j]; } int createfab(int m,int n) //全组合 { int i,j,lcount,wa[20]; for(i=1;i<=n;i++) wa[i]=i; wa[n+1]=m+1; for(i=1;i<=n;i++) change(point[wa[i]].x,point[wa[i]].y); if(judge()==1) { for(i=1;i<=n;i++) pa[i]=wa[i]; return n; } ret(); lcount=1; while(wa[1]<m-n+1) { for(i=n;i>0;i--) { if(wa[i]<wa[i+1]-1) { wa[i]++; for(j=i;j<n;j++) wa[j+1]=wa[j]+1; for(i=1;i<=n;i++) change(point[wa[i]].x,point[wa[i]].y); if(judge()==1) { for(i=1;i<=n;i++) pa[i]=wa[i]; return n; } ret(); lcount++; break; } } } return -1; } int main() { int i,j,k; char ch; init(); for(i=1;i<=4;i++) scanf("%s",a[i]+1); ret(); if(judge()==1) { printf("0\n"); return 0; } for(i=1;i<=16;i++) { j=createfab(16,i); if(j!=-1) { printf("%d\n",j); for(k=1;k<=j;k++) printf("%d %d\n",point[pa[k]].x,point[pa[k]].y); return 0; } } return 0; }
相关文章推荐
- poj 2965 The Pilots Brothers' refrigerator(dfs 枚举 +打印路径)
- poj2965 The Pilots Brothers' refrigerator(直接计算或枚举Enum+dfs)
- POJ 2965-The Pilots Brothers' refrigerator(贪心+枚举)
- POJ:2965 The Pilots Brothers' refrigerator(二进制枚举)
- POJ 2965 The Pilots Brothers' refrigerator (枚举)
- POJ2965,The Pilots Brothers' refrigerator,枚举,DFS
- poj 2965 The Pilots Brothers' refrigerator (枚举)
- POJ 2965 The Pilots Brothers' refrigerator 枚举
- poj 2965 The Pilots Brothers' refrigerator(dfs 枚举 +打印路径)
- POJ 2965 The Pilots Brothers' refrigerator(dfs+枚举 || 规律)
- ACM--枚举+DFS/巧妙思想--POJ--2965--The Pilots Brothers' refrigerator--初期算法
- poj 2965 The Pilots Brothers' refrigerator
- POJ 2965 The Pilots Brothers' refrigerator (想法题)
- poj 2965.The Pilots Brothers' refrigerator
- POJ - 2965 - The Pilots Brothers' refrigerator (高效贪心!!)
- POJ 2965 (超简单代码47毫秒AC) The Pilots Brothers' refrigerator
- The Pilots Brothers' refrigerator 【POJ--2965】
- 【POJ 2965】 The Pilots Brothers' refrigerator
- poj 2965 The Pilots Brothers' refrigerator 高斯消元法 暑假第二题
- poj 2965 The Pilots Brothers' refrigerator