Bit operation introduction and practical Skills (iv): actual combat
The following is my own written three code, some of the topics are also my own out. The code was written in my Pascal era, and I don't have a C language. The code is not well written, I just want to tell you about the application of bit arithmetic in actual combat, including the search and state compression DP topics. In fact, we can find more on the Internet to optimize the use of the topic, here to sort out some of their own code, just for the integrity of the original series of articles. This series of articles to the end of this, I hope you can have something to gain.
Matrix67 original, please indicate the source.
Problem: convoluted switch
Source of the topic
06 Noip Simulation (i) by Matrix67 fourth question
Problem description
Have you ever played "pull the Lights" game? 25 lights are lined up in a 5x5 square. Each lamp has a switch, and the player can change its state. At each step, the player can change the state of a particular lamp. Players changing the state of a lamp will have a ripple effect: and the lamp next to and around the lamp should also change its state accordingly.
We use the number "1" to indicate an open light, with the number "0" to indicate the lights off. The following state
10111
01101
10111
10000
11011
After changing the status of the lamp in the top left corner, it will become:
01111
11101
10111
10000
11011
Then change it in the middle of the light after the state will become:
01111
11001
11001
10100
11011
Given the initial state of some games, the program is written to determine if the player is likely to brighten all the lights within 6 steps.
Input format
The first line has a positive integer n, which represents the initial state of n pending games for the data.
The following rows of data are divided into n groups, each with 5 rows and 5 characters per line. Each set of data describes the initial state of a game. Each group of data is separated by a blank line.
For 30% of data,n<=5;
For 100% of data, n<=500.
Output format
The output data has a total of n rows, each line has an integer less than or equal to 6, which indicates that the corresponding game state in the input data takes at least a few steps to lighten all the lights.
For the initial state of a game, if all lights are not lit within 6 steps, output "1".
Sample input
3
00111
01011
10001
11010
11100
11101
11101
11110
11111
11111
01111
11111
11111
11111
11111
Sample output
3
2
-1
Program code
Const
bigprime=3214567;
maxstep=6;
Type
Pointer=^rec;
Rec=record
V:longint;
Step:integer;
Next:pointer;
End
Var
Total:longint;
Hash:array[0..bigprime-1]of pointer;
Q:array[1..400000]of Rec;
function Update (A:longint;p:integer): Longint;
Begin
A:=a xor (1 SHL p);
If P mod 5<>0 then A:=a xor (1 SHL (p-1));
if (p+1) mod 5<>0 then A:=a xor (1 SHL (p+1));
If P<20 then A:=a xor (1 SHL (p+5));
If P>4 then A:=a xor (1 SHL (p-5));
Exit (a);
End
function Find (A:longint;step:integer): boolean;
Var
Now:pointer;
Begin
Now:=hash[a MoD Bigprime];
While Now<>nil do
Begin
If Now^.v=a then exit (true);
Now:=now^.next;
End
New (now);
Now^.v:=a;
Now^.step:=step;
Now^.next:=hash[a MoD Bigprime];
Hash[a MoD Bigprime]:=now;
total:=total+1;
Exit (FALSE);
End
procedure solve;
Var
P:integer;
close:longint=0;
Open:longint=1;
Begin
Find (1 shl 25-1,0);
Q[1].v:=1 SHL 25-1;
q[1].step:=0;
Repeat
Inc (Close);
For P:=0 to
If not find (update (Q[CLOSE].V,P), q[close].step+1) and (Q[close].step+1<maxstep) then
Begin
open:=open+1;
Q[open].v:=update (Q[CLOSE].V,P);
q[open].step:=q[close].step+1;
End
Until close>=open;
End
Procedure print (a:longint);
Var
Now:pointer;
Begin
Now:=hash[a MoD Bigprime];
While Now<>nil do
Begin
If Now^.v=a Then
Begin
Writeln (Now^.step);
Exit
End
Now:=now^.next;
End
Writeln (-1);
End
Procedure main;
Var
Ch:char;
I,j,n:integer;
T:longint;
Begin
READLN (n);
For I:=1 to N do
Begin
t:=0;
For J:=1 to
Begin
Read (CH);
T:=t*2+ord (CH)-48;
If J MoD 5=0 then readln;
End
Print (t);
If I<n then readln;
End
End
Begin
Solve
Main
End.
======================= Sexy Split-line =======================
Problem:garden/and mm stroll around the garden
Source of the topic
The fourth question of the 07 Matrix67 birthday Tournament
Problem description
The garden design emphasizes that simplicity is beauty. The Matrix67 garden has a very simple layout: the location of all the sights of the garden is "aligned", and these sights can be seen as lattice points on the plane coordinates. Adjacent sights are connected by a small path, all parallel to the axis. The sights and paths make up an "incomplete grid".
A typical garden layout is shown in the diagram on the left. Garden Layout on a grid of 6 rows and 4 columns, the location of the 16 attractions in the garden is highlighted in red. The black lines indicate the path between the sights, and the rest of the gray ones do not actually exist.
Matrix67 's birthday, he will take his mm in the garden to play. MATRIX67 does not take mm two times through the same attraction, so each attraction is visited at most. He and his
Binary <4>