POJ 1222 EXTENDED LIGHTS OUT【暴力dfs】
来源:互联网 发布:淘宝卖家快递单打印 编辑:程序博客网 时间:2024/06/02 02:23
点击打开链接
题意:
给你一个5*6 的方格,每个方格里面分别由1or0.
每次操作一个方格,会把该方格and该方格的上下左右四个方向的方格里面的数字取反,
现在给你一个已经有数字的方格,让你给出一个能把方格全部变成0的方案,
题解:
很明显的状态压缩,枚举第一行后面的都可以递推出来,2^6*24的复杂度。
我直接dfs了第一行。
//#include <bits/stdc++.h>#include<stdio.h>#include<iostream>#include<stdlib.h>#include<string.h>#define ll long longusing namespace std;const int maxn=2e5+10;int a[10][10],ans[10][10];int c[10],b[10][10],f;void dfs(int pos){ if(f) return ; if(pos==7){ f=1; memset(ans,0,sizeof(ans)); for(int i=1;i<=5;++i)for(int j=1;j<=6;++j) b[i][j]=a[i][j]; for(int i=1;i<=6;++i){ ans[1][i]=c[i]; if(c[i]==1){ b[1][i]=!b[1][i]; b[2][i]=!b[2][i]; b[1][i-1]=!b[1][i-1]; b[1][i+1]=!b[1][i+1]; } } for(int i=2;i<=5;++i){ for(int j=1;j<=6;++j){ if(b[i-1][j]==1){ b[i-1][j]=!b[i-1][j]; b[i+1][j]=!b[i+1][j]; b[i][j]=!b[i][j]; b[i][j-1]=!b[i][j-1]; b[i][j+1]=!b[i][j+1]; ans[i][j]=1; } } } for(int i=1;i<=6;++i) if(b[5][i]==1){ f=0;return ; } if(f) return ; } if(pos>=7) return; for(int i=1;i<=6;++i){ c[pos]=1; dfs(pos+1);if(f) return ; c[pos]=0; dfs(pos+1);if(f) return ; } return ;}int main(){ int T,ca=0; scanf("%d",&T); while(T--){ f=0; for(int i=1;i<=5;++i) for(int j=1;j<=6;++j) scanf("%d",&a[i][j]); dfs(1); printf("PUZZLE #%d\n",++ca); for(int i=1;i<=5;++i){ printf("%d",ans[i][1]); for(int j=2;j<=6;++j){ printf(" %d",ans[i][j]); }puts(""); } } return 0;}
阅读全文
1 0
- POJ 1222 EXTENDED LIGHTS OUT【暴力dfs】
- POJ-1222 EXTENDED LIGHTS OUT【暴力枚举】
- POJ 1222 EXTENDED LIGHTS OUT 高斯消元?暴力枚举!
- poj 1222 EXTENDED LIGHTS OUT
- POJ 1222 EXTENDED LIGHTS OUT
- POJ 1222 : EXTENDED LIGHTS OUT
- POJ 1222 EXTENDED LIGHTS OUT
- POJ 1222 EXTENDED LIGHTS OUT
- POJ 1222 EXTENDED LIGHTS OUT
- poj 1222:EXTENDED LIGHTS OUT
- POJ 1222 EXTENDED LIGHTS OUT
- POJ 1222 EXTENDED LIGHTS OUT
- poj 1222EXTENDED LIGHTS OUT
- POJ 1222 EXTENDED LIGHTS OUT
- POJ 1222 EXTENDED LIGHTS OUT
- [POJ 1222] EXTENDED LIGHTS OUT
- 【POJ 1222】EXTENDED LIGHTS OUT
- POJ 1222 EXTENDED LIGHTS OUT
- 数据库简单介绍
- 跳动的心
- [51Nod 1110 距离之和最小 V3]三分
- 浅谈Java中的equals和==
- OpenCV入门笔记
- POJ 1222 EXTENDED LIGHTS OUT【暴力dfs】
- 解压版mysql安装
- Bit Manipulation-----190. Reverse Bits&&191. Number of 1 Bits
- 猜扑克牌
- socket学习
- lua table
- 阿里巴巴2017测试题--抽奖
- Java中的四种内部类
- 算法提高 ADV-12 计算时间