前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >ACMSGURU 101 - Domino

ACMSGURU 101 - Domino

作者头像
Reck Zhang
发布2021-08-11 11:12:53
4790
发布2021-08-11 11:12:53
举报
文章被收录于专栏:Reck Zhang

Domino

Problem Description

Dominoes – game played with small, rectangular blocks of wood or other material, each identified by a number of dots, or pips, on its face. The blocks usually are called bones, dominoes, or pieces and sometimes men, stones, or even cards. The face of each piece is divided, by a line or ridge, into two squares, each of which is marked as would be a pair of dice…

The principle in nearly all modern dominoes games is to match one end of a piece to another that is identically or reciprocally numbered.

ENCYCLOPÆDIA BRITANNICA

Given a set of domino pieces where each side is marked with two digits from 0 to 6. Your task is to arrange pieces in a line such way, that they touch through equal marked sides. It is possible to rotate pieces changing left and right side.

Input

The first line of the input contains a single integer N (1 ≤ N ≤ 100) representing the total number of pieces in the domino set. The following N lines describe pieces. Each piece is represented on a separate line in a form of two digits from 0 to 6 separated by a space.

Output

Write “No solution” if it is impossible to arrange them described way. If it is possible, write any of way. Pieces must be written in left-to-right order. Every of N lines must contains number of current domino piece and sign “+” or “-“ (first means that you not rotate that piece, and second if you rotate it).

Sample Input

代码语言:javascript
复制
5
1 2
2 4
2 4
6 4
2 1

Sample Output

代码语言:javascript
复制
2 -
5 +
1 +
3 +
4 -

Solution

代码语言:javascript
复制
#include <bits/stdc++.h>

struct node {
    int right;
    int index;
    inline bool operator< (const node& outNode) {
        return index < outNode.index;
    }
};

std::unordered_map<int, bool> visit{};
std::unordered_map<int, std::vector<node>> nodes{};
std::unordered_map<int, int> degree{};
std::vector<node> path{};


void dfs(const int& left) {
    for(const auto& nextNode : nodes[left]) {
        if(visit[nextNode.index] == true) {
            continue;
        }

        visit[nextNode.index] = true;
        visit[-nextNode.index] = true;
        dfs(nextNode.right);
        path.push_back(nextNode);
    }
}

int main() {
    std::ios::sync_with_stdio(false);

    int n;
    std::cin >> n;

    for(int i = 1; i <= n; i++) {
        int a{};
        int b{};

        std::cin >> a >> b;

        visit[i] = false;
        visit[-i] = false;

        nodes[a].push_back({b, i});
        nodes[b].push_back({a, -i});

        degree[a]++;
        degree[b]++;
    }

    int oddCount = 0;
    int startIndex = degree.begin()->first;
    for(auto const& item : degree) {
        if(item.second & 1) {
            oddCount++;
            startIndex = item.first;
        }
    }

    if(oddCount != 0 && oddCount != 2) {
        std::cout << "No solution" << std::endl;
        return 0;
    }

    dfs(startIndex);

    std::reverse(path.begin(), path.end());

    if(path.size() == n) {
        for(const auto& res : path) {
            std::cout << abs(res.index) << (res.index > 0 ? " +" : " -") << std::endl;
        }
    } else {
        std::cout << "No solution" << std::endl;
    }

    return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019-10-24,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Domino
    • Problem Description
      • Input
        • Output
          • Sample Input
            • Sample Output
              • Solution
              领券
              问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档