zoukankan      html  css  js  c++  java
  • HDU 2514 Another Eight Puzzle

    Another Eight Puzzle

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

    Problem Description
    Fill the following 8 circles with digits 1~8,with each number exactly once . Conntcted circles cannot be filled with two consecutive numbers. There are 17 pairs of connected cicles: A-B , A-C, A-D B-C, B-E, B-F C-D, C-E, C-F, C-G D-F, D-G E-F, E-H F-G, F-H G-H
    Filling G with 1 and D with 2 (or G with 2 and D with 1) is illegal since G and D are connected and 1 and 2 are consecutive .However ,filling A with 8 and B with 1 is legal since 8 and 1 are not consecutive .
    In this problems,some circles are already filled,your tast is to fill the remaining circles to obtain a solution (if possivle).
     
    Input
    The first line contains a single integer T(1≤T≤10),the number of test cases. Each test case is a single line containing 8 integers 0~8,the numbers in circle A~H.0 indicates an empty circle.
     
    Output
    For each test case ,print the case number and the solution in the same format as the input . if there is no solution ,print “No answer”.If there more than one solution,print “Not unique”.
     
    Sample Input
    3
    7 3 1 4 5 8 0 0
    7 0 0 0 0 0 0 0
    1 0 0 0 0 0 0 0
     
    Sample Output
    Case 1: 7 3 1 4 5 8 6 2
    Case 2: Not unique
    Case 3: No answer
     
    思路:好一个DFS
     
    代码:
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cstdlib>
    #include <algorithm>
    using namespace std;
    int map[10];
    int flag;
    int hash[10];
    int t;
    int sum;
    int hhhh[10];
    bool yes_can(int p,int x)
    {
        int pow = 0;
        if(p == 1)
            return true;
        if(p == 2)
        {
            if(map[1] != x + 1 && map[1] != x - 1)
                return true;
        }
        if(p == 3)
        {
            if(map[1] != x + 1 && map[1] != x - 1
            && map[2] != x + 1 && map[2] != x - 1)
                  return true;
        }
        if(p == 4)
        {
            if(map[1] != x + 1 && map[1] != x - 1
            && map[3] != x + 1 && map[3] != x - 1)
                  return true;
        }
        if(p == 5)
        {
            if(map[2] != x + 1 && map[2] != x - 1
            && map[3] != x + 1 && map[3] != x - 1)
                  return true;
        }
        if(p == 6)
        {
            if(map[2] != x + 1 && map[2] != x - 1
            && map[3] != x + 1 && map[3] != x - 1
            && map[4] != x + 1 && map[4] != x - 1
            && map[5] != x + 1 && map[5] != x - 1)
                   return true;
        }
        if(p == 7)
        {
            if(map[3] != x + 1 && map[3] != x - 1
            && map[4] != x + 1 && map[4] != x - 1
            && map[6] != x + 1 && map[6] != x - 1)
                  return true;
        }
        if(p == 8)
        {
            if(map[5] != x + 1 && map[5] != x - 1
            && map[6] != x + 1 && map[6] != x - 1
            && map[7] != x + 1 && map[7] != x - 1)
                  return true;
        }
        return false;
    }
    void DFS(int now)
    {
        //printf("%d           %d ",now,sum);
        if(now == sum)
        {
            flag ++;
            return ;
        }
        if(flag == 2)
        {
            return ;
        }
        for(int i = 1;i <= 8;i ++)
        {
            if(hash[i] == 0 && yes_can(hhhh[now + 1],i))
            {
                hash[i] = 1;
                map[hhhh[now + 1]] = i;
                DFS(now + 1);
                hash[i] = 0;
                if(flag == 2)
                    return ;
            }
        }
    }
    int main()
    {
        scanf("%d",&t);
        for(int l = 1;l <= t;l ++)
        {
            sum = 1;
            memset(hhhh,0,sizeof(hhhh));
            memset(hash,0,sizeof(hash));
            memset(map,0,sizeof(map));
            for(int i = 1;i <= 8;i ++)
            {
               scanf("%d",&map[i]);
               if(map[i] == 0)
               {
                   hhhh[sum ++] = i;
               }
               if(map[i] != 0)
                   hash[map[i]] = 1;
            }
            sum --;
            flag = 0;
            DFS(0);
            printf("Case %d:",l);
            if(flag == 0)
                printf(" No answer ");
            if(flag == 1)
            {
                for(int i = 1;i <= 8;i ++)
                    printf(" %d",map[i]);
                printf(" ");
            }
            if(flag == 2)
                printf(" Not unique ");
        }
        return 0;
    }
    Source
     
    Recommend
    lcy
  • 相关阅读:
    SDNU 1311.Binomial Coeffcients
    SDNU 1306.兑数
    SDNU 1272.SL的秘密
    SDNU 1270.超超的难题
    XCode 自动化打包总结
    Xrun 将 app 转化为 IPA
    mac终端下运行shell脚本
    ios 检测应用程序升级问题
    在iis6.0公布asp.net mvc3网站
    IOS 中 NSArray
  • 原文地址:https://www.cnblogs.com/GODLIKEING/p/3337965.html
Copyright © 2011-2022 走看看