当前位置:网站首页>Puzzling switch
Puzzling switch
2022-07-20 23:08:00 【Ding Jiaxiong】
subject
You played “ Pull light ” Games ?
25 The lamps are arranged in a row 5×5 The square of .
Every light has a switch , Players can change its state .
Each step , Players can change the state of a certain light .
Players change the state of a light and have a chain reaction : And this lamp up and down adjacent to the left and right lights should also change their state accordingly .
We use Numbers 1
It means an open light , Use numbers 0 It means the light is off .
The following state
10111
01101
10111
10000
11011
After changing the state of the lamp in the top left corner, it will become :
01111
11101
10111
10000
11011
After changing the lamp in the middle, the state will become :
01111
11001
11001
10100
11011
Given some initial state of the game , Write a program to determine if the player is likely to be in 6 Make all the lights on in step .
Input format
Enter a positive integer in the first line n, There are n The initial state of the game to be solved .
The following lines of data are divided into n Group , Each group of data has 5 That's ok , Each row 5 Characters .
Each set of data describes the initial state of a game .
Each group of data is separated by a blank line .
Output format
Total output n Row data , Each line has one less than or equal to 6 The integer of , It means that it takes at least a few steps for the corresponding game state in the input data to make all the lights on .
For the initial state of a game , if 6 You can't turn all the lights on in steps , The output −1.
Data range
0<n≤500
sample input :
3
00111
01011
10001
11010
11100
11101
11101
11110
11111
11111
01111
11111
11111
11111
11111
sample output :
3
2
-1
Thought analysis
Answer key
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
using namespace std;
const int N = 6;
char g[N][N],backup[N][N];
// Define offset
int dx[5] = {
-1,0,1,0,0},dy[5] = {
0,1,0,-1,0};
void turn(int x ,int y ){
for(int i = 0; i < 5; i++){
// The offset position
int a = x + dx[i];
int b = y + dy[i];
if (a < 0 || a >= 5 || b < 0 || b >= 5){
continue; // Direct to ignore
}
g[a][b] ^= 1; // Exclusive or operation , take 0 change 1,1 change 0
}
}
int main(){
int T; // Number of test data
cin >> T;
while(T --){
// Read into chessboard
for(int i = 0; i < 5 ; i++){
cin >> g[i];
}
// Record the steps
int res = 10;
// Enumerate the operations on the first line
for(int op = 0; op < 32;op ++){
// Backup
memcpy(backup , g , sizeof g);
int step = 0;
// Switch light operation
for(int i = 0 ; i < 5 ; i++){
if (op >> i & 1){
step ++;
turn(0,i);
}
}
// The rest of the line
for(int i = 0; i < 4 ;i++){
// That's ok
for(int j = 0; j < 5 ;j ++ ){
// Column
if(g[i][j] == '0'){
step ++;
turn(i + 1,j);
}
}
}
bool dark = false;
// Judge whether there is any light on the last line
for(int i = 0;i < 5 ; i ++){
if(g[4][i] == '0'){
dark = true;
break;
}
}
if(!dark){
res = min(res,step);
}
memcpy(g , backup , sizeof g);
}
if(res > 6){
res = -1;
}
cout << res << endl;
}
return 0;
}
边栏推荐
- I created the Clickhouse engine, but after creating the SQL node, it shows that it has been unbound? What's going on
- Matrikon OPC simulator tutorial
- 柔性数组
- 数字化转型迈向深水区:银行业不断夯实科技根基
- 数组常用方法
- 开户哪家券商比较好?手机上开户安全吗
- Comparison of several popular cross end technology solutions in 2022
- [cloud based co creation] Huawei cloud helps accelerate the construction of enterprise data assets and data governance production lines
- 最新MATLAB下载安装教程(附文件)
- leetcode:460. LFU least commonly used cache
猜你喜欢
Can Tiktok and iqiyi open another new space?
简单斐波那契
[cloud based co creation] Huawei cloud helps accelerate the construction of enterprise data assets and data governance production lines
Deep learning -- neural network, ML strategy
MySQL transaction
C语言------数组
中国式IT运维,趟出自己的“长期主义”河流
Digital transformation towards deep water: the banking industry continues to consolidate its scientific and technological foundation
簡單斐波那契
成功 解决 @keyup.enter=“search()“ 在el-input 组件中不生效的问题
随机推荐
Mysql database and table building (I)
Coinbase上市,将开启牛市下半场上涨 2021-04-03
Deep understanding of TCP protocol
【LeetCode每日一题——消失的数字】
苹果等大厂Hold不住了!裁员和放缓招聘,「双管齐下」出方案
牛市还在,资金往头部集中是全球性规律 2021-04-04
1054 The Dominant Color
Makefile implémente les statistiques de temps de compilation
Take the first place in the International Olympic Games in mathematics, physics and chemistry, and win all the gold medals. Netizen: the Chinese team is too good
Chinese style it operation and maintenance, out of its own "long-term" River
数字化转型迈向深水区:银行业不断夯实科技根基
Introduction to third-party plug-ins of notepad++ Chinese Translation
TIA botu_ Summary of difficulties related to the upgrade and migration of STEP7 version
高数 | 【多元函数微分学 计算篇】链式求导法、隐函数求导法、全微分形式不变性、多元函数求极值
Matrikon OPC simulator tutorial
SQL语句编写的练习(MySQL)
成功 解决 @keyup.enter=“search()“ 在el-input 组件中不生效的问题
Emergency response > > types and disposal methods of network security emergency events
C language ----- array
华泰证券h5开户安全吗?可以开吗?