poj 2965 The Pilots Brothers' refrigerator 枚举+组合 暑假第二题
来源:互联网 发布:单片机舵机正反转 编辑:程序博客网 时间:2024/06/04 18:44
其实和 poj 1753样,甚至更简单,套用1753代码打的,并优化了一部分;
具体参考:http://blog.csdn.net/sholck222/article/details/46695087
但在这道题中,可能限制时间交紧,相同代码提交两次才过,第一次超时,第二次险过,953ms;
这道题还有其他解法,如高斯解法,枚举+dfs,这两份代码及简析明天会发上来
枚举+组合的代码:
#include<iostream>#include<cstdio>#include<cstring>using namespace std;char a[5][5];char b[25];char c[25];int num[25];int guanlian=0;int check=0;int tell()//判断是否全关上{ int i,j; int k1=1; for(i=0; i<16; i++) { if(b[i]=='0') k1=0; } if(k1==0)//没关上 return -1; else return 0;//关上}void gaibian(int p){ if(b[p]=='+') b[p]='0';//关 else b[p]='1';//开}void gaibian2(int p){ if(b[p]=='1') b[p]='0';//关 else b[p]='1';//开}int fanzhuan(){ int j; for(j=0; j<guanlian; j++) { int p=num[j]; int x=p/4; int y=p%4; int i; int k1=p-y; gaibian2(p); for(i=k1; i<=k1+3; i++) { gaibian2(i); } int k2=p-4*x; for(i=0; i<4; i++) { gaibian2(k2+i*4); } } if(tell()==0) return 1;//已全部关上 else return -1;//没关上}void huanyuan(){ int i,j; for(i=0;i<16;i++) { b[i]=c[i]; }}void select_com(int l,int p){ int i; if(l==guanlian) { int g=fanzhuan(); if(g==1) { check=1; printf("%d\n",guanlian); //cout<<guanlian<<endl; for(i=0;i<guanlian;i++) { int x=num[i]/4; int y=num[i]%4; printf("%d %d\n",x+1,y+1); //cout<<x+1<<' '<<y+1<<endl; } return ; } else { huanyuan(); } } for(i=p; i<16; i++) { if(check==0) { num[l]=i; select_com(l+1,i+1); } }}int main(){ int start=0; int i,j; for(i=0; i<4; i++) { for(j=0; j<5; j++) // !!!!!!!!!!!!!!!! j == 4时输入 '\n' { scanf("%c",&a[i][j]); if (j == 4) continue; // !!!!!!!!!! b[start]=a[i][j]; gaibian(start); c[start]=b[start]; start++; } } int m=1; while(m<=16) { guanlian=m; select_com(0,0); if(check==1) break; m++; } check=0; return 0;}
0 0
- 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 The Pilots Brothers' refrigerator (枚举)
- poj 2965 The Pilots Brothers' refrigerator[ 枚举 ]
- poj-2965 The Pilots Brothers' refrigerator -- 枚举
- poj 2965 The Pilots Brothers' refrigerator【枚举】
- The Pilots Brothers' refrigerator 枚举 Poj 2965
- poj 2965 The Pilots Brothers' refrigerator 普通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【枚举+dfs】
- POJ 2965 The Pilots Brothers' refrigerator (枚举)
- POJ:2965 The Pilots Brothers' refrigerator(二进制枚举)
- POJ 2965 The Pilots Brothers' refrigerator 搜索+枚举
- JavaScript & JQuery 交互式Web前端开发
- 在LAMP环境下搭建PHP动态网页
- 学习使用chromium里的线程类(1)
- css小知识
- iOS开发最常用的第三方库
- poj 2965 The Pilots Brothers' refrigerator 枚举+组合 暑假第二题
- Java学习笔记九(泛型)
- iOS开发 APP如何实现检测更新(百度)
- CentOS 修改主机名
- meta标签 -- 移动端
- SPDIF S24_LE S24_3LE调试小结
- Android.mk简介
- 虚继承与虚基类的内存布局
- Android 开源框架Universal-Image-Loader完全解析(二)--- 图片缓存策略详解