preface
Blue Bridge Cup Official Website: Blue Bridge Cup - National College Students TMT industry competition
✨ This blog explains the algorithm knowledge involved in the Blue Bridge Cup C/C + + preparation. This blog is the second lecture: recursion [exercise]
See blog for details of [example] of recursion: Lecture 2 of Blue Bridge Cup – recursion [example]
The exercises included in this blog are:
👊 Flip a coin
👊 Pilot brother
The content of the blog is replaced by questions. By explaining the topics, we can help readers quickly understand the content of the algorithm. We need to pay attention to: learning the algorithm can't only go through the brain, but also practice. Please be sure to type and write the relevant code of this blog by yourself!!!
Flip a coin
Title Requirements
Title Description:
Xiao Ming is playing a coin flipping game.
There are some coins in a row on the table. We use * for the front and o for the back (lowercase letters, not zero).
For example, the possible situation is: * * oo***oooo
If you flip the two coins on the left at the same time, it becomes: oooo***oooo
Now Xiao Ming's question is: if you know the initial state and the target state to be achieved, you can only flip two adjacent coins at the same time, how many times should you flip at least for a specific situation?
We agreed that flipping two adjacent coins is called one-step operation.
Input format:
Two lines of equal length strings represent the initial state and the target state to be achieved respectively.
Output format:
An integer representing the minimum number of operation steps
Data range:
The length of the input string shall not exceed 100.
The data ensure that the answer must be solved.
Input example 1:
**********
o****o****
Output example 1:
5
Input example 2:
*o**o***o***
*o***o**o***
Output example 2:
1
Train of thought analysis
It can be pushed from left to right. As long as it is found that it is not equal, turn the coin and a coin to the right of the coin
code
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int N = 105; char f[N], g[N]; void turn(int u) { if (f[u] == '*') f[u] = 'o'; else f[u] = '*'; } int main() { scanf("%s%s", f, g); int n = strlen(f); int res = 0; for (int i = 0; i < n; i ++ ) if (f[i] != g[i]) { res ++; turn(i); turn(i + 1); } printf("%d\n", res); return 0; }
Pilot brother
Title Requirements
Title Description:
The game of "pilot brothers" requires players to smoothly open a refrigerator with 16 handles.
It is known that each handle can be in one of the following two states: open or closed.
The refrigerator will only open when all the handles are open.
The handle can be represented as a 4 × 4 matrix, you can change the state of the handle in any position [i,j].
However, this will also change the status of all handles on row i and column j.
Please find out the minimum number of switching handles required to open the refrigerator.
Input format:
The input contains a total of four lines, and each line contains the initial state of four handles.
The symbol + indicates that the handle is closed, while the symbol - indicates that the handle is open.
The initial state of at least one handle is closed.
Output format:
The first line outputs an integer N, indicating the minimum number of handle switching required.
Next, N lines describe the switching order. Each line outputs two integers, representing the row number and column number of the handle in the switched state. The numbers are separated by spaces.
Note: if there are many ways to open the refrigerator, open it from top to bottom and from left to right according to the priority.
Data range:
1 ≤ i, j ≤ 4
Input example:
-+--
----
----
-+--
Output example:
6
1 1
1 3
1 4
4 1
4 3
4 4
Train of thought analysis
This question is somewhat similar Lecture 2 of Blue Bridge Cup – recursion [example] The incomprehensible switches in, but they are not exactly the same. The influence relationship in the incomprehensible switch topic is one-to-one correspondence, but this topic is different. In this topic, for any switch, multiple switches can change its state. This topic adopts the idea of explosive search, that is, violently enumerate each switch, a total of 16 switches, and each switch is nothing more than off and on, Therefore, there are 216 enumerations. We use 0 to indicate no operation and 1 to indicate operation. Then the operation is from 0 to 216 - 1. This problem needs self-study: Bit operation In the matrix of 4 * 4 shown in the figure, our thinking through bit operation is actually to change two-dimensional into one-dimensional: the matrix into 01 string. We number each lattice of the matrix: 0, 1, 2,..., 15 from top to bottom and from left to right. Our get() function is used to obtain the subscript after the matrix in row X and column y becomes one-dimensional 01 string, turn_ The all() function is used to realize the switch change of one row + one column. Note that the point (x, y) has been operated twice, so it needs to be operated separately again. The turn() function is the specific operation: change +, + change-
code
#include <iostream> #include <algorithm> #include <cstring> #include <vector> #define x first #define y second using namespace std; typedef pair<int, int> PII; const int N = 5; char g[N][N], backup[N][N]; vector<PII> res; void turn(int x, int y) { if (backup[x][y] == '-') backup[x][y] = '+'; else backup[x][y] = '-'; } void turn_all(int x, int y) { for (int i = 0; i < 4; i ++ ) { turn(x, i); turn(i, y); } turn(x, y); } int get(int x, int y) { return x * 4 + y; } int main() { for (int i = 0; i < 4; i ++ ) cin >> g[i]; for (int i = 0; i < 1 << 16; i ++ ) { memcpy(backup, g, sizeof g); vector<PII> tem_res; for (int j = 0; j < 4; j ++ ) for (int k = 0; k < 4; k ++ ) if (i >> get(j, k) & 1) { tem_res.push_back({j, k}); turn_all(j, k); } bool flag = false; for (int j = 0; j < 4; j ++ ) for (int k = 0; k < 4; k ++ ) if (backup[j][k] == '+') { flag = true; break; } if (!flag) if (res.empty() || res.size() > tem_res.size()) res = tem_res; } cout << res.size() << endl; for (auto r : res) cout << r.x + 1 << ' ' << r.y + 1 << endl; return 0; }