当前位置:网站首页>2811:熄灯问题
2811:熄灯问题
2022-07-21 05:21:00 【来斤山楂】
/*
2811:熄灯问题
这个枚举好难啊,说说思路吧
注意枚举第一行的状态,之后依据第一行确定下一行状态,最终得出答案。
*/
#include<iostream>
#include<cstring>
using namespace std;
//作用是将字符c的第i位置位状态s,注意,应该使用引用类型用于改变字符的值
void setBit(char &c,int s,int i)
{
if(s){
c |= (1 << i);
}
else{
c &= ~(1 << i);
}
}
//返回字符的第i位值
int getBit(char c,int i)
{
return (c >> i) & 1;
}
//翻转字符第i位的值
void flipBit(char &c,int i)
{
c ^= (1 << i);
}
void print(char *light)
{
for(int i = 0;i < 5;++i){
for(int j = 0;j < 6;++j){
cout << getBit(light[i],j) << " ";
}
cout << endl;
}
}
int main()
{
//输入,利用一个char字符记录一行灯的状态
//ori用于记录初始状态,lights用于记录变化中的状态,res记录输出方案,switchs
//用于记录当前行的方案
//freopen("C:\\Users\\ASUS\\Desktop\\test.txt","r",stdin);
char ori[8],lights[8],res[8],switchs;
memset(ori,0,sizeof(ori));
for(int i = 0;i < 5;++i){
for(int j = 0;j < 6;++j){
int n;
cin >> n;
setBit(ori[i],n,j);
}
}
//print(ori);
//逐个尝试第一行的64种方案
for(int n = 0;n < 64;++n){
memcpy(lights,ori,sizeof(ori));
//print(lights);
switchs = n; //第1行采取的开关方案
//用它确定其他行状态,因为第二行所执行的开关方案必定需要将第一行的
//所有灯灭掉,故而第二行的开关状态是确定的,同理每一行的开关状态
//都是确定的,故而最后只需要看最后一行是否是全灭便可知方案是否成功
for(int i = 0;i < 5;++i){
res[i] = switchs;
for(int j = 0;j < 6;++j){
//翻转本行内容
if(getBit(switchs,j)){
if(j > 0)
flipBit(lights[i],j-1);
flipBit(lights[i],j);
if(j < 5)
flipBit(lights[i],j+1);
}
}
//开始把这一行放到j 的循环里了!!!!!气死我了
//翻转下一行
if(i < 4)
lights[i+1] ^= switchs;
switchs = lights[i];
}
if(lights[4] == 0){
print(res);
break;
}
} //end for(int n = 0;n < 64;++n)
return 0;
}
边栏推荐
- Solr deployment and IK Chinese word segmentation cases
- 3.使用Ansible中的playbook
- How are web interface resources saved?
- JProfiler installation and use tutorial
- 高软课程总结
- @Correct use of requestparam annotation
- 95. 费解的开关
- Newly released yolov5 visual actual combat project (1)
- What is hash conflict? How to resolve hash conflicts?
- 文件特殊的权限
猜你喜欢
Analysis of JVM operation principle
2.Ansible中常用模塊
Installation and implementation of zabbix5.0
Database dormitory management system
Federated learning: spatiotemporal data mining in federated scenarios
Peoplecode operator
Lamp Architecture - mysq cluster and master-slave replication (2)
What is hash conflict? How to resolve hash conflicts?
I spent 6000 to sign up for the test training class. Three months later, I successfully "cheated" into Tencent with a monthly salary of 17k
虚拟机的封装
随机推荐
高软课程总结
Let interns set up a redis cluster, which almost got me in~~~
Assembly realizes circular judgment on whether it is a square number by means of conditional transfer
Stata used in jupyter
JVM运行原理解析
vim编辑器
12. User microservice
(C language) flexible array
TZC 1283: simple sort merge sort
Example of Cisco configuring VLAN
Atomic quotation to solve ABA problem
Four stages of writing C language
Peoplecode objects and classes
Implementation of static address book
Peoplecode variable
Dry goods sharing - what knowledge points did you take in the soft test high items in the first half of 2022?
2.Ansible中常用模塊
Zabbix+ distributed database tidb realizes distributed database monitoring
Installation and implementation of zabbix5.0
Reverse proxy haproxy