当前位置:网站首页>The second session of question swiping and clock out activity -- solving the switching problem with recursion as the background (III)
The second session of question swiping and clock out activity -- solving the switching problem with recursion as the background (III)
2022-06-28 23:35:00 【Poplar branch】
AcWing 116. Pilot brother day03 【C++】
Title Description

source :acwing
Original title transmission gate
Problem solving report
Understanding of topic meaning
First of all to see switch Two words , Some friends may wonder if they can solve the problem with the switch model in recursion . Recall the characteristics of previous switch exercises that can use recursion : Whether their switch is on or off is caused by some conditions that they must be pressed .
But for this problem , You can't do this , Because too many switches are involved , There is no such case that the switch pressing method can be uniquely determined .
At this time, observe the data range , I found that it only had 4 individual , Then we can estimate whether the lovely violence enumeration can work , Roughly determine the time complexity by calculating the number of operations .
16 Switches , Only open or not open , So the total is from 0 Enumerate to 2 16 − 1 2^{16}-1 216−1, That is to say 65536 In this case . Remember each of these situations , We all see one 16 Bit binary number , To imitate the chessboard . If the binary number corresponding to a certain position is 0 Just press , yes 1 Just don't press
The first enumeration in the inner layer :
Here 65536 In these cases , You still need to enumerate each time 16 Is the position to be opened or closed , Each switch operates a maximum of seven surrounding bulbs .
The second enumeration in the inner layer :
And enumerate to check whether the status of each bulb is all turned on , And the final recording scheme , So the approximate number of operations is 65536 ∗ ( 16 ∗ 7 + 16 + 16 ) 65536*(16*7+16+16) 65536∗(16∗7+16+16)
About 10 million operations .C++ One second is about 1 100 million times , No problem .
About dictionary order
From top to bottom in the title , The order from left to right is actually the dictionary order .
It's easy to solve this problem , As long as we enumerate in dictionary order , That is to say, the enumeration sequence is from small to large , The final result must be in dictionary order .

Implementation process
First step : enumeration 65536 Kind of plan .
The second step : According to this scheme 16 One bulb for operation .
The third step : Judge whether all bulbs are on after operation , If it's all on , Record the plan . Because the minimum number of steps is required , Then every time you find a solution, you should compare it with the previous solution .
details
One 、 Backup and restore backup
Two 、 In the output answer , The starting point is (1,1). The end is (4,4). Because we created the map from 0 At the beginning , So we need to The output results are +1
Reference code
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#define x first
#define y second
using namespace std;
typedef pair<int,int> PII;
const int N = 5;
int n;
char g[N][N],backup[N][N];
int get(int x,int y)
{
return 4 * x+ y;
}
void turn_one(int x,int y)
{
if(g[x][y] == '+') g[x][y] = '-';
else g[x][y] = '+';
}
void turn_all(int x,int y)
{
for(int i = 0; i < 4;i++)
{
turn_one(x,i);
turn_one(i,y);
}
//(x,y) One more press , Press back
turn_one(x,y);
}
int main()
{
// Enter the chessboard
for(int i = 0; i < 4;i++) cin >> g[i];
// Record the final answer
vector<PII> res;
// enumeration 2^16 Kind of plan
for(int op = 0; op < 1 << 16;op++)
{
vector<PII> temp;
// Make a backup
memcpy(backup,g,sizeof g);
// Enumerate each switch
for(int i = 0; i < 4;i++)
for(int j = 0; j < 4;j++)
if(!(op >> get(i,j)&1)) //get(i,j) This function is used to determine the current position in the map by coordinates 0~15 Which number in
{
temp.push_back({i,j});
turn_all(i,j);// change i That's ok j All of the columns, etc
}
bool has_closed = false;
// Judge whether all the etc. are bright
for(int i = 0; i < 4;i++)
for(int j = 0; j < 4;j++)
{
if(g[i][j] == '+') has_closed = true;
}
// Record the plan
if(!has_closed)
// If the array used to store the answers is empty , Or the number of stored steps is larger than that of the new test , Just update res
if(res.empty() || res.size() > temp.size()) res = temp;
// Restore backup
memcpy(g,backup,sizeof backup);
}
cout << res.size() << endl;
// Note that the title is from 1 At the beginning
for(auto r:res) cout << r.x+1 << ' ' << r.y+1<<endl;
}
summary
So far, , The switch problem with recursion as the background comes to an end .
| As far as my current understanding is concerned , If you encounter switch problems, you can first go to the manual simulation example , See if it can be pushed down . If you can , That's the best , If not , Then you can also consider pulling out a lovely search  |
边栏推荐
- 2022-06-28:以下golang代码输出什么?A:true;B:false;C:panic;D:编译失败。 package main import “fm
- Is it safe to open a stock account on the Internet?
- pymysql. Error get error code and specific error information
- Form verification problem - El select (solution to automatically trigger verification on initialization page)
- 第二章 经典同步练习作业
- Be on the list again! Know that Chuangyu was selected as one of the top 50 competitive enterprises in China's network security industry in 2022
- C語言-單詞分析解析
- YuMinHong set up two funds funded by his hometown
- Online yaml to JSON tool
- stm32F407-------电容触摸按键
猜你喜欢
![[state machine design] Moore, Mealy state machine, three-stage, two-stage and one-stage state machine writing specification](/img/48/e29f34aff7cc437bfb574591d54e3d.png)
[state machine design] Moore, Mealy state machine, three-stage, two-stage and one-stage state machine writing specification

Cs5463 code module analysis (including download link)

mysql-5.7.30-winx64免安装版下载安装教程

Chapter III processor scheduling exercise

第二章 经典同步练习作业

机器学习6-决策树
![[matlab] function definition and use](/img/43/a7970ca8e075151277f7773434f7db.png)
[matlab] function definition and use
![[Electronic Experiment 2] simple electronic doorbell](/img/40/227f9ac1f427c1435e0e3aa02640b1.png)
[Electronic Experiment 2] simple electronic doorbell

Cmake tutorial (I)

ERROR 1067 (42000): Invalid default value for ‘end_time‘ Mysql
随机推荐
mysql-5.7.30-winx64免安装版下载安装教程
PHP利用CURL实现登录网站后下载Excel文件
stm32F407-------LCD
Puma joins hands with 10ktf shop to launch its Web3 cooperation project with the largest scale so far
[state machine design] Moore, Mealy state machine, three-stage, two-stage and one-stage state machine writing specification
Yyds dry inventory solution sword finger offer: maximum sum of continuous subarrays (II)
Junior, it's not easy!
【软件分析】软件分析、设计与建模迭代式详解
Interviewer: what is the internal implementation of strings in redis?
Mysql-5.7.30-winx64 installation free download and installation tutorial
[flutter issues Series title 71] Mutual Conversion between uint8list and Image in flutter
Machine learning 4-dimension reduction technology
ctfshow XSS
Implementation of dynamic timer for quartz
2022 PMP project management examination agile knowledge points (4)
[Chapter 2 of word tutorial series] how to set the table on each page to have a header in word
[stm32 HAL库] RTC和BKP驱动
[matlab]函数定义与使用
TDD和自动化测试
IDC: Alibaba cloud ranks first in the market share of China's data governance platform in 2021