zoukankan      html  css  js  c++  java
  • HDU-2181 哈密顿绕行世界问题

    直接DFS,不用加剪枝。

    #include <cstdio>
    #include <iostream>
    #include <fstream>
    #include <cstdlib>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    #include <queue>
    
    #define rep(i, l, r) for(int i=l; i<=r; i++)
    #define down(i, l, r) for(int i=l; i>=r; i--)
    #define maxn 5678
    #define MAX 1<<30
    
    using namespace std;
    
    int m, e[21][4], st[21], n, t;
    bool b[21]; 
    
    void Search(int x, int n)
    {
        if (n == 21) 
        {
            printf("%d:  ", ++t); rep(i, 1, 20) printf("%d ", st[i]); printf("%d
    ", m); return; 
        }
        if (n == 20)
        {
            st[n] = x; rep(i, 1, 3) if (e[x][i] == m) Search(m, n+1); return;
        }
        b[x] = true; st[n] = x;
        rep(i, 1, 3) if (!b[e[x][i]]) Search(e[x][i], n+1);
        b[x] = false;
    }
    
    int main()
    {
        rep(i, 1, 20) scanf("%d%d%d", &e[i][1], &e[i][2], &e[i][3]); 
        rep(i, 1, 20) sort(e[i]+1, e[i]+4);
        scanf("%d", &m); while (m) { t = 0; Search(m, 1); scanf("%d", &m); }
        return 0;
    }
    View Code
  • 相关阅读:
    表达式for loop
    用户输入
    字符编码
    变量字符编码
    Python安装
    Python 2 or 3?
    Python解释器
    2017中国大学生程序设计竞赛
    Educational Round 27
    Round #429 (Div.2)
  • 原文地址:https://www.cnblogs.com/NanoApe/p/4311944.html
Copyright © 2011-2022 走看看