Tony's mouth is blowing (. Silanol) zzz

Source: Internet
Author: User
Depth-First Search

Start searching better from one side with fewer start states

Examples

Give you a pair of cards in the \ (n\) card, out of the next card needs to be the number of cards in front of the sum of the number of points, to find a legitimate solution

The forward search code for this topic is as follows:

void dfs(int depth, int sum){    if(depth == n){        output();        return ;        }    REP(i, 1, n){    if(!vis[i] && sum % a[i] == 0){        vis[i] = 1;        dfs(depth + 1, sum + a[i]);        vis[i] = 0;        }    }}

Obviously the initial branch is many, consider the reverse search

void dfs(int depth, int left){    if(depth == 0){output();return ;}    REP(i, 1, n){    if(!vis[i] && (left - a[i]) % a[i] == 0){        vis[i] = 1;        dfs(depth - 1, left - a[i]);        vis[i] = 0;        }    }}//调用dfs(n, sum[a[i]]);

Fewer initial branches, less search volume

Tony's mouth is blowing (. Silanol) zzz

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.