zoukankan      html  css  js  c++  java
  • 哈密顿绕行世界问题、n皇后问题

    哈密顿绕行世界问题

    Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 8325    Accepted Submission(s): 4892

    Problem Description

    一个规则的实心十二面体,它的 20个顶点标出世界著名的20个城市,你从一个城市出发经过每个城市刚好一次后回到出发的城市。 
     

    Input

    前20行的第i行有3个数,表示与第i个城市相邻的3个城市.第20行以后每行有1个数m,m<=20,m>=1.m=0退出.
     
    Output
    输出从第m个城市出发经过每个城市1次又回到m的所有路线,如有多条路线,按字典序输出,每行1条路线.每行首先输出是第几条路线.然后个一个: 后列出经过的城市.参看Sample output
     

    Sample Input

    2 5 20 1 3 12 2 4 10 3 5 8 1 4 6 5 7 19 6 8 17 4 7 9 8 10 16 3 9 11 10 12 15 2 11 13 12 14 20 13 15 18 11 14 16 9 15 17 7 16 18 14 17 19 6 18 20 1 13 19 5 0
     

    Sample Output

    1: 5 1 2 3 4 8 7 17 18 14 15 16 9 10 11 12 13 20 19 6 5 2: 5 1 2 3 4 8 9 10 11 12 13 20 19 18 14 15 16 17 7 6 5 3: 5 1 2 3 10 9 16 17 18 14 15 11 12 13 20 19 6 7 8 4 5 4: 5 1 2 3 10 11 12 13 20 19 6 7 17 18 14 15 16 9 8 4 5 5: 5 1 2 12 11 10 3 4 8 9 16 15 14 13 20 19 18 17 7 6 5 6: 5 1 2 12 11 15 14 13 20 19 18 17 16 9 10 3 4 8 7 6 5 7: 5 1 2 12 11 15 16 9 10 3 4 8 7 17 18 14 13 20 19 6 5 8: 5 1 2 12 11 15 16 17 18 14 13 20 19 6 7 8 9 10 3 4 5 9: 5 1 2 12 13 20 19 6 7 8 9 16 17 18 14 15 11 10 3 4 5 10: 5 1 2 12 13 20 19 18 14 15 11 10 3 4 8 9 16 17 7 6 5 11: 5 1 20 13 12 2 3 4 8 7 17 16 9 10 11 15 14 18 19 6 5 12: 5 1 20 13 12 2 3 10 11 15 14 18 19 6 7 17 16 9 8 4 5 13: 5 1 20 13 14 15 11 12 2 3 10 9 16 17 18 19 6 7 8 4 5 14: 5 1 20 13 14 15 16 9 10 11 12 2 3 4 8 7 17 18 19 6 5 15: 5 1 20 13 14 15 16 17 18 19 6 7 8 9 10 11 12 2 3 4 5 16: 5 1 20 13 14 18 19 6 7 17 16 15 11 12 2 3 10 9 8 4 5 17: 5 1 20 19 6 7 8 9 10 11 15 16 17 18 14 13 12 2 3 4 5 18: 5 1 20 19 6 7 17 18 14 13 12 2 3 10 11 15 16 9 8 4 5 19: 5 1 20 19 18 14 13 12 2 3 4 8 9 10 11 15 16 17 7 6 5 20: 5 1 20 19 18 17 16 9 10 11 15 14 13 12 2 3 4 8 7 6 5 21: 5 4 3 2 1 20 13 12 11 10 9 8 7 17 16 15 14 18 19 6 5 22: 5 4 3 2 1 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 23: 5 4 3 2 12 11 10 9 8 7 6 19 18 17 16 15 14 13 20 1 5 24: 5 4 3 2 12 13 14 18 17 16 15 11 10 9 8 7 6 19 20 1 5 25: 5 4 3 10 9 8 7 6 19 20 13 14 18 17 16 15 11 12 2 1 5 26: 5 4 3 10 9 8 7 17 16 15 11 12 2 1 20 13 14 18 19 6 5 27: 5 4 3 10 11 12 2 1 20 13 14 15 16 9 8 7 17 18 19 6 5 28: 5 4 3 10 11 15 14 13 12 2 1 20 19 18 17 16 9 8 7 6 5 29: 5 4 3 10 11 15 14 18 17 16 9 8 7 6 19 20 13 12 2 1 5 30: 5 4 3 10 11 15 16 9 8 7 17 18 14 13 12 2 1 20 19 6 5 31: 5 4 8 7 6 19 18 17 16 9 10 3 2 12 11 15 14 13 20 1 5 32: 5 4 8 7 6 19 20 13 12 11 15 14 18 17 16 9 10 3 2 1 5 33: 5 4 8 7 17 16 9 10 3 2 1 20 13 12 11 15 14 18 19 6 5 34: 5 4 8 7 17 18 14 13 12 11 15 16 9 10 3 2 1 20 19 6 5 35: 5 4 8 9 10 3 2 1 20 19 18 14 13 12 11 15 16 17 7 6 5 36: 5 4 8 9 10 3 2 12 11 15 16 17 7 6 19 18 14 13 20 1 5 37: 5 4 8 9 16 15 11 10 3 2 12 13 14 18 17 7 6 19 20 1 5 38: 5 4 8 9 16 15 14 13 12 11 10 3 2 1 20 19 18 17 7 6 5 39: 5 4 8 9 16 15 14 18 17 7 6 19 20 13 12 11 10 3 2 1 5 40: 5 4 8 9 16 17 7 6 19 18 14 15 11 10 3 2 12 13 20 1 5 41: 5 6 7 8 4 3 2 12 13 14 15 11 10 9 16 17 18 19 20 1 5 42: 5 6 7 8 4 3 10 9 16 17 18 19 20 13 14 15 11 12 2 1 5 43: 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 1 2 3 4 5 44: 5 6 7 8 9 16 17 18 19 20 1 2 12 13 14 15 11 10 3 4 5 45: 5 6 7 17 16 9 8 4 3 10 11 15 14 18 19 20 13 12 2 1 5 46: 5 6 7 17 16 15 11 10 9 8 4 3 2 12 13 14 18 19 20 1 5 47: 5 6 7 17 16 15 11 12 13 14 18 19 20 1 2 3 10 9 8 4 5 48: 5 6 7 17 16 15 14 18 19 20 13 12 11 10 9 8 4 3 2 1 5 49: 5 6 7 17 18 19 20 1 2 3 10 11 12 13 14 15 16 9 8 4 5 50: 5 6 7 17 18 19 20 13 14 15 16 9 8 4 3 10 11 12 2 1 5 51: 5 6 19 18 14 13 20 1 2 12 11 15 16 17 7 8 9 10 3 4 5 52: 5 6 19 18 14 15 11 10 9 16 17 7 8 4 3 2 12 13 20 1 5 53: 5 6 19 18 14 15 11 12 13 20 1 2 3 10 9 16 17 7 8 4 5 54: 5 6 19 18 14 15 16 17 7 8 9 10 11 12 13 20 1 2 3 4 5 55: 5 6 19 18 17 7 8 4 3 2 12 11 10 9 16 15 14 13 20 1 5 56: 5 6 19 18 17 7 8 9 16 15 14 13 20 1 2 12 11 10 3 4 5 57: 5 6 19 20 1 2 3 10 9 16 15 11 12 13 14 18 17 7 8 4 5 58: 5 6 19 20 1 2 12 13 14 18 17 7 8 9 16 15 11 10 3 4 5 59: 5 6 19 20 13 12 11 10 9 16 15 14 18 17 7 8 4 3 2 1 5 60: 5 6 19 20 13 14 18 17 7 8 4 3 10 9 16 15 11 12 2 1 5
    代码:
     1 #include<stdio.h>
     2 using namespace std;
     3 
     4 int map[25][4];            //记录每个城市的相邻的三个城市
     5 int visited[21];        //记录某个城市是否被访问过
     6 int route[21];            //记录某条路线经过的城市顺序
     7 
     8 int cnt = 0;
     9 
    10 //某一次探路
    11 void dfs(int a, int len, int origin)        //三个参数分别为此次探路的起始城市, 距离最初起点的距离, 和最初的起点
    12 {
    13     visited[a] = 1;            //将a城市标记为访问过
    14     route[len] = a;            //并将a放入路线中
    15 
    16     for (int i = 1; i < 4; i++)
    17     {
    18         int t = map[a][i];
    19         if (t == origin && len == 19)        //如果邻接点是原点且从原点出发经过19段路程,说明找到了一条路径
    20         {
    21             printf("%d:  ", ++cnt);
    22             for (int j = 0; j <= len; j++)    //打印这条路线
    23                 printf("%d ", route[j]);
    24             printf("%d
    ", origin);
    25         }
    26 
    27         if (!visited[t])
    28             dfs(t, len + 1, origin);
    29     }
    30 
    31     visited[a] = 0;        //将a城市重新标记为未访问,方便下次探路
    32 }
    33 
    34 
    35 int main()
    36 {
    37     int city;
    38 
    39     for (int i = 1; i <= 20; i++)
    40         scanf_s("%d %d %d", &map[i][1], &map[i][2], &map[i][3]);
    41     while (scanf_s("%d", &city), city)
    42     {
    43         dfs(city, 0, city);
    44     }
    45 
    46 }

    以上转自:https://blog.csdn.net/libin56842/article/details/15028427

    N皇后问题

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 38709    Accepted Submission(s): 16447

    Problem Description

    在N*N的方格棋盘放置了N个皇后,使得它们不相互攻击(即任意2个皇后不允许处在同一排,同一列,也不允许处在与棋盘边框成45角的斜线上。
    你的任务是,对于给定的N,求出有多少种合法的放置方法。

     Input
    共有若干行,每行一个正整数N≤10,表示棋盘和皇后的数量;如果N=0,表示结束。
     

    Output

    共有若干行,每行一个正整数,表示对应输入行的皇后的不同放置数量。
     

    Sample Input

    1 8 5 0
     
    Sample Output
    1 92 10
     1 #include <iostream>
     2 #include <algorithm>
     3 
     4 using namespace std;
     5 
     6 int column[11];        //每行的皇后的列号
     7 int total = 0;
     8 
     9 /*假设前k个皇后已经放好位置,放入第k+ 1个皇后*/
    10 void Queue(int k, int N)
    11 {
    12     if (k == N)
    13         total++;        //排放次数加一
    14 
    15     for (int i = 1; i <= N; i++)        //尝试每个列号
    16     {
    17         int j = 1;
    18         for (; j <= k; j++)
    19         {
    20             if (column[j] == i || (k + 1 - j) == abs(i - column[j]))        //k + 1的列号可能大于k的列号,所以要加绝对值
    21                 break;
    22         }
    23 
    24         if (j == k + 1 && k + 1 <= N)            //说明没有冲突
    25         {
    26             column[k + 1] = i;            //记录下第k + 1个皇后的列号
    27             Queue(k + 1, N);            //
    28         }
    29     }
    30     
    31 }
    32 
    33 int main()
    34 {
    35     int N;
    36 
    37     int chess[11];
    38     for (int i = 1; i < 11; i++)
    39     {
    40         total = 0;
    41         Queue(0, i);        //前0个皇后位置已摆好
    42         chess[i] = total;
    43     }
    44 
    45     while (cin >> N && N != 0)
    46     {
    47         total = chess[N];
    48         cout << total << endl;
    49     }
    50 
    51     return 0;
    52 }
  • 相关阅读:
    mysql一些基本操作
    redis的简单使用2
    redis的基本conf
    redis的简单使用
    $.ajax()方法详解(转)
    流行的9个Java框架介绍: 优点、缺点等等
    数据库查询,显示为树形结构(easyui+SSM)
    JAVA-汉字转换成汉语拼音(pinyin4j-2.5.0-sources.jar)
    Jquery EasyUI 中ValidateBox验证框使用讲解(转)
    EasyUI验证扩展
  • 原文地址:https://www.cnblogs.com/hi3254014978/p/10434199.html
Copyright © 2011-2022 走看看