Home > database >  Turn to the great god, the novice
Turn to the great god, the novice

Time:10-01

One evening with A, B, C, D four people to go through A bridge, they only had A flashlight, every bridge need torch lighting, bridge every time A maximum of two people, at the same time they all bridge from one side to the other side how many kinds of schemes, please complete and output all solutions with A SQL (lists each scheme everyone walking over the bridge number),
Example:
A B C D
1 2 3 4

Do you have a great god will

CodePudding user response:

 
SQL> As with m (
2 select the 'A' n from dual union all
3 select 'B' n from dual union all
4 select the 'C' n from dual union all
Select 5 'D' n the from dual
6)
7 the select m1. N n1, m2. N, n2, m3. N n3, m4. N n4 interchange
8 the from m m1, m2, m. m m3, m4 m.
9 where regexp_count (m1) n | | m2. N | | m3. The n | | m4. N, 'A')=1
10 and regexp_count (m1) n | | m2. N | | m3. The n | | m4. N, 'B')=1
11 and regexp_count (m1) n | | m2. N | | m3. The n | | m4. N, 'C')=1
12 and regexp_count (m1) n | | m2. N | | m3. The n | | m4. N, 'D')=1;
N1 N2 N3 N4 interchange
-- -- -- --
A B C D
A B D C
A B C D
A B C D
A B C D
A D C B
A B C D
B A D C
B C D
A B C D
B D A C
D C A B
A B C D
C A D B
C B A D
A B C D
A B C D
C D B A
A B C D
D A C B
D B A C
D C B A
D C A B
D C B A
24 rows selected

SQL>

CodePudding user response:

Must be limited to the minimum number of the bridge, or infinitely many solutions,
Should be 3 (2), 2 to 1 (pick), a total of at least the bridge 3 * 2 + 2=8 times
Also is to satisfy the following conditions is solution,
1, the bridge at least once each
2, each bridge an odd number of times will be back (even)
3, the sum is 8 times (personal maximum 5 times)

 
With the TAB as (select 1 as n the from dual union all select 3 from dual union all select 5 from dual)
The select a.n as a, b.n as b, citigroup as c, d.n as d
The from TAB. A, b TAB, the TAB c, TAB d
Where a.n + b.n + offset + d.n=8


A B C D
1 1 1 5
1 1 3 3
1 5 1
3 1 3
3 3 1
5 1 1
3 1 1 3
3 3 1
3 3 1 1
5 1 1 1

CodePudding user response:

Thanks to the upstairs to answer
  • Related