SQL competition-5x5 checkboard puzzle

Source: Internet
Author: User

Q & A of the first phase of the SQL competition "Shengtuo Media Cup,


Question: 5x5 checkboard Problems


In the 5x5 checkboard (), each row, column, and diagonal line (the diagonal line includes not only the diagonal line) can have up to two balls. How can we place the most balls, how many pendulum methods are there? Output all la S.


Requirements:
Use an SQL statement.
Output Format:
From the first row of the checker board to the second row, each row is output in sequence from the first column to the second column. 0 indicates no ball, and 1 indicates no ball. For example, 1001000000000000000000000. A row outputs a row number and a solution, which are sorted in ascending order of the column strings where the solution is located.

Database platform:
Applicable to Oracle, ms SQL Sever, and version (10 GR2 or above are recommended for Oracle and 2008 is recommended for ms SQL Sever)

My personal answer is as follows:
-- Construct options 0 and 1
With test
(Select '1' bit from dual Union select '0' from dual ),
-- Construct the arrangement and combination of each row
Combostring
(
Select Replace (sys_connect_by_path (bit, '#'), '#') combo
From Test
Where level = 5
Connect by level <= 5
),
-- Construct a string of "10001" into a computable number.
Combo
(Select substr (combo,) B1, substr (combo,) B2, substr (combo,) B3, substr (combo,) B4, substr (combo) b5
From combostring ),
-- Logical solution based on question
Allcombo
(
Select c1.b1 | c1.b2 | c1.b3 | c1.b4 | c1.b5 | ''| c2.b1 | c2.b2 | c2.b3 | c2.b4 | c2.b5 |'' |
C3.b1 | c3.b2 | c3.b3 | c3.b4 | c3.b5 | ''| c4.b1 | c4.b2 | c4.b3 | c4.b4 | c4.b5 |'' | c5.b1 | | c5.b2 | c5.b3 | c5.b4 | c5.b5 combo,
Rank () over (order
C1.b1 + c1.b2 + c1.b3 + c1.b4 + c1.b5 + c2.b1 + c2.b2 + c2.b3 + c2.b4 + c2.b5 +
C3.b1 + c3.b2 + c3.b3 + c3.b4 + c3.b5 + c4.b1 + c4.b2 + c4.b3 + c4.b4 + c4.b5 +
C5.b1 + c5.b2 + c5.b3 + c5.b4 + c5.b5 DESC) rank
From combo C1, Combo C2, Combo C3, Combo C4, Combo C5
Where c1.b1 + c2.b1 + c3.b1 + c4.b1 + c5.b1 <= 2
And c1.b2 + c2.b2 + c3.b2 + c4.b2 + c5.b2 <= 2
And c1.b3 + c2.b3 + c3.b3 + c4.b3 + c5.b3 <= 2
And c1.b4 + c2.b4 + c3.b4 + c4.b4 + c5.b4 <= 2
And c1.b5 + c2.b5 + c3.b5 + c4.b5 + c5.b5 <= 2
And c1.b1 + c1.b2 + c1.b3 + c1.b4 + c1.b5 <= 2
And c2.b1 + c2.b2 + c2.b3 + c2.b4 + c2.b5 <= 2
And c3.b1 + c3.b2 + c3.b3 + c3.b4 + c3.b5 <= 2
And c4.b1 + c4.b2 + c4.b3 + c4.b4 + c4.b5 <= 2
And c5.b1 + c5.b2 + c5.b3 + c5.b4 + c5.b5 <= 2
And c1.b1 + c2.b2 + c3.b3 + c4.b4 + c5.b5 <= 2
And c1.b5 + c2.b4 + c3.b3 + c4.b2 + c5.b1 <= 2
And c1.b2 + c2.b3 + c3.b4 + c4.b5 <= 2
And c2.b1 + c3.b2 + c4.b3 + c5.b4 <= 2
And c1.b3 + c2.b4 + c3.b5 <= 2
And c3.b1 + c4.b2 + c5.b3 <= 2
And c1.b4 + c2.b3 + c3.b2 + c4.b1 <= 2
And c2.b5 + c3.b4 + c4.b3 + c5.b2 <= 2
And c1.b3 + c2.b2 + c3.b1 <= 2
And c3.b5 + c4.b4 + c5.b3 <= 2
)
-- List compliant records
Select
Combo
From allcombo
Where Rank = 1

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.