当前位置:网站首页>The second session of the question swiping and punching activity -- solving the switching problem with recursion as the background (I)
The second session of the question swiping and punching activity -- solving the switching problem with recursion as the background (I)
2022-07-04 07:54:00 【Poplar branch】
AcWing 95. Inexplicable switch day03 【C++】
Title Description

source :acwing
Original title transmission gate
Problem solving report
One 、 Understanding of topic meaning
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 .
Use windows Own drawing , Hold on shift Drag the mouse to draw the following graphics for demonstration .
Sample demonstration :
Two 、 The operation of each line of switch is completely determined by the light on and off of the previous line
- Suppose all the states in the first row are determined by enumeration , Each switch is either pressed , Or don't press , Here it is 2 5 2^5 25 Secondary selection .
- According to the light off state of the previous line , Decide how to press the next line , To ensure that the previous line is bright .

3、 ... and 、 The details are finalized
- How to enumerate the first line 32 In the operation . A five bit binary can just represent this 32 Kind of operation . such as
11010 => Corresponding to the first 26 Operations . that 0 ~ 2 5 2^5 25-1 You can enumerate the first row one by one 32 Kind of plan
Switch on function
After clicking the switch in one position , Its four position switches will be changed . Use four i f if if It seems too troublesome . What is considered here is to add Offset implementation .
Time complexity
32 ∗ 25 ∗ 5 ∗ 500 = 2000000 32 * 25 * 5 * 500 = 2000000 32∗25∗5∗500=2000000, About twomillion calculations . There is no problem at all
Reference code
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 6;
char g[N][N],backup[N][N];
int dx[] = {-1,0,1,0,0} , dy[] = {0,-1,0,1,0};
// completion turn function
void turn(int x,int y)
{
for(int i = 0; i < 5;i++)
{
int a = x+dx[i] , b = y + dy[i];
// Judgement boundary
if(a < 0 || a >= 5 || b < 0 || b>= 5) continue;
// Update status
g[a][b] ^= 1;
}
}
int main()
{
int T;
cin >> T;
while(T--)
{
// Building maps
for(int i = 0; i < 5;i++) cin >> g[i];
int res = 10;// Record answer
// Enumerate the first line of 32 A choice
for(int op = 0; op < 32;op++)
{
memcpy(backup,g,sizeof g);
int step = 0;// Record the steps performed
// Deal with whether there is a light that can be pressed on in the current scheme
for(int i = 0; i < 5; i++)
{
if(!(op >> i &1))
{
step ++;
turn(0,i);
}
}
// According to the current situation in the first line , Deal with the processing of the next four lines
for(int i = 0; i < 4;i++)
{
for(int j = 0; j < 5;j++)
if(g[i][j] == '0')
{
step ++;
turn(i+1,j);
}
}
// Traverse the last line , If there is a black lamp , Then the current scheme fails
bool dark = false;// Mark
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);// Get the data back from the backup
}
if(res > 6) res = -1;//res = -1 Represents no solution
cout << res << endl;
}
return 0;
}
边栏推荐
- Unity opens the explorer from the inspector interface, selects and records the file path
- The cloud native programming challenge ended, and Alibaba cloud launched the first white paper on application liveliness technology in the field of cloud native
- Used on windows Bat file startup project
- 促进OKR落地的工作总结该如何写?
- 时序数据库 InfluxDB 2.2 初探
- JVM -- class loading process and runtime data area
- Types of references in BibTex
- 21个战略性目标实例,推动你的公司快速发展
- Devops Practice Guide - reading notes (long text alarm)
- How to use MOS tube to realize the anti reverse connection circuit of power supply
猜你喜欢

Book list | as the technical support Party of the Winter Olympics, Alibaba cloud's technology is written in these books!

The frost peel off the purple dragon scale, and the xiariba people will talk about database SQL optimization and the principle of indexing (primary / secondary / clustered / non clustered)

zabbix監控系統自定義監控內容
![Sports [running 01] a programmer's half horse challenge: preparation before running + adjustment during running + recovery after running (experience sharing)](/img/c8/39c394ca66348044834eb54c68c2a7.png)
Sports [running 01] a programmer's half horse challenge: preparation before running + adjustment during running + recovery after running (experience sharing)
![[go basics] 1 - go go](/img/e2/d973b9fc9749e1c4755ce7d0ec11a1.png)
[go basics] 1 - go go

What are the work contents of operation and maintenance engineers? Can you list it in detail?

ZABBIX 5.0 monitoring client

Système de surveillance zabbix contenu de surveillance personnalisé

节点基础~节点操作

【Go基础】1 - Go Go Go
随机推荐
Cannot click button when method is running - C #
Handwritten easy version flexible JS and source code analysis
Google's official response: we have not given up tensorflow and will develop side by side with Jax in the future
Comparison between applet framework and platform compilation
Heap concept in JVM
ZABBIX 5.0 monitoring client
线性代数1.1
Go h*ck yourself:online reconnaissance (online reconnaissance)
MySQL中的文本处理函数整理,收藏速查
【Go基础】2 - Go基本语句
21 examples of strategic goals to promote the rapid development of your company
Thesis learning -- time series similarity query method based on extreme point characteristics
How to use C language code to realize the addition and subtraction of complex numbers and output structure
墨者学院-phpMyAdmin后台文件包含分析溯源
[real case] how to deal with the failure of message consumption?
string. Format without decimal places will generate unexpected rounding - C #
It's healthy to drink medicinal wine like this. Are you drinking it right
How to write a summary of the work to promote the implementation of OKR?
运动【跑步 01】一个程序员的半马挑战:跑前准备+跑中调整+跑后恢复(经验分享)
This article is enough for learning advanced mysql