ToJ 1398 square-DFS

Source: Internet
Author: User

Ask M sticks (4 <= m <= 20) if they can form a square frame. The length of each stick is 1 ~ 10000

This is the enhanced version of 0.00sec running on toJ.

At the beginning, DFS was too violent .. Later, I finally wrote the DFS with three parameters based on others' logs ~

Submit time: 2010-07-06 18:21:49
Language: gnu c ++ result: accepted
PID: 1398 time: 0.03 sec. Memory:
1204 K.

# Include <stdio. h> # include <string. h >#include <iostream >#include <algorithm> using namespace STD; int Len [20], sum, M; bool flag, F [20]; // flag indicates whether bool CMP (int A, int B) {return A> B;} void DFS (int d, int stick, int nowlen) {// I: worker being processed, stick: Number of frames already spelled, nowlen: Int J of the frame currently being spelled; If (D> = M | flag | stick> 2) {If (stick> 2) Flag = 1; // three boxes have been spelled out, and return;} else {for (j = D; j <m; j ++) {If ((! F [J]) & nowlen + Len [J] <= sum) {f [J] = 1; // mark if (nowlen + Len [J] = sum) DFS (0, stick + 1, 0); // you have a final else DFS (J + 1, stick, nowlen + Len [J]). // continue to spell out the current F [J] = 0; // restore tag }}} int main () {int ca, I, j; scanf ("% d ", & Ca); While (ca --) {scanf ("% d", & M); for (I = sum = 0; I <m; I ++) {scanf ("% d", & Len [I]); sum + = Len [I];} If (sum % 4! = 0) {printf ("NO \ n"); continue;} sum/= 4; sort (Len, Len + M, CMP ); if (LEN [0]> sum) {printf ("NO \ n"); continue;} flag = 0; memset (F, 0, sizeof (f )); DFS (0, 0); printf ("% s \ n", flag? "Yes": "no");} // system ("pause"); Return 0 ;}

This took 0.03 seconds. The idea was to splice M sticks into four borders. There are several optimizations:

1. The sum of all sticks should be able to divide 4

2. the maximum length of a stick cannot exceed 1/4 of all sticks.

3. Sorting by the length of the sticks from large to small can quickly eliminate some impossible situations. For example, in the third example, there are 8 sticks, 1 7 2 6 4 4 3 5, and four boxes with a length of 8 must be assembled, after sorting, the first one is 7 and the second one is 6, 7 + 6> 8. If the small data is in front, for example, 1 2 3 4 is in front, the fourth data cannot be found.

4. If three borders have been added, the fourth border will not need to be spelled out. The remaining borders must be the fourth border of the square.

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.