uva524 (Dfs water problem)

Source: Internet
Author: User

Test instructions

The beads numbered 1 to N, strung into a bracelet, require any two adjacent beads and are prime numbers;


Ideas:

Data volume only 16, hit a quality tables, direct DFS search on the line;


#include <cstdio> #include <cstring> #include <algorithm>using namespace Std;int vis[32];int viss[20] ; int res[20],n;void init () {vis[1] = 1;for (int i = 2; I <= 31;i++) {if (!vis[i]) {for (int j = i + i; j <=; J + = i) VIS[J] = 1;}} return;} void dfs (int pre,int sum) {if (sum = = N) {if (!vis[pre + 1]) {printf ("%d", res[1]), for (int i = 2; I <= n;i++) {printf ("%d ", Res[i]);} printf ("\ n");} return;}  for (int i = 1; I <= n; i++) {if (!viss[i] &&!vis[i + pre]) {Viss[i] = 1;res[sum + 1] = I;dfs (i,sum + 1); Viss[i] = 0;}} return;} int main () {int cas = 1;init (); bool F = 0;while (scanf ("%d", &n) = = 1) {if (f) printf ("\ n"); Elsef = 1;memset (Viss, 0,sizeo F (viss));p rintf ("Case%d:\n", cas++); viss[1] = 1;res[1] = 1;dfs (all);}}


uva524 (Dfs water problem)

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.