Example 6-19 self-assembled UVa1572

Source: Internet
Author: User

1. Title Description: Click to open the link

2. Solving ideas: The problem is solved by using topological sorting. Topological sort is applied to the direction graph, and the nodes in the graph can satisfy the given "join" law and form a direction graph, and by topological sort, it is possible to determine whether there is a forward loop in the graph. If you just follow the test instructions to try to stitch a square, it will be time-consuming, because the number of n will be very large. If we further abstract, the label of the Square as a splicing point , because 00 can not be used as a splicing point, so there is a total of 26*2=52 points, then if there is another square B can be connected with the square A, This means that the splice point in B can reach all the stitching points in a, so that the connection rule is determined, and eventually it can be spelled into a graph.

So how to judge whether a square can be made into an infinitely large structure? It can be found that, if possible, some squares in this structure will be repeated, that is, the stitching points between them will be repeated! There is a forward loop in the direction diagram! Therefore, it is only necessary to determine whether the resulting graph has a forward loop, and the topological sort can be used to complete the judgment.

3. Code:

#define _crt_secure_no_warnings #include <iostream> #include <algorithm> #include <string> #include <sstream> #include <set> #include <vector> #include <stack> #include <map> #include < queue> #include <deque> #include <cstdlib> #include <cstdio> #include <cstring> #include < cmath> #include <ctime> #include <functional>using namespace std; #define N 52int g[n][n];int c[n];int ID ( Char A1, Char A2)//assigns each label a id{return (A1-' A ') * 2 + (A2 = = ' + '? 0:1);} void Connect (char A1, Char A2, Char B1, char B2)//Convert the input square to a graph {if (a1 = = ' 0 ' | | b1 = ' 0 ') return;int u = ID (a1, A2) ^ 1, v = ID (b1, b2);//Because the ID (A1,A2) ^1 must be able to be stitched together with the ID (A1,A2), which means that the ID (A1,A2) ^1 can be stitched together with the ID (b1,b2) g[u][v] = 1;}  BOOL Toposort (int u) {c[u] = -1;for (int v = 0; v < n;v++) if (G[u][v]) {if (C[v] < 0) return true;//there is a forward ring, return TrueElse if (!c[v] && toposort (v)) return true; C[u] = 1;return false;//There is no forward ring}bool cycle () {memset (c, 0, sizeof (c)); for (int i = 0; i < n;i++) if (!c[i]) if (Toposort (i)) return true;//There is a forward ring, return truereturn false;} int main () {//freopen ("T.txt", "R", stdin), int n;while (~SCANF ("%d", &n) &&n) {memset (g, 0, sizeof (g)); while ( n--) {char s[10];scanf ("%s", s), for (int i = 0; i < 4;i++) for (int j = 0; J < 4;j++) if (i! = j) Connect (S[i * 2], s[i * 2 + 1], S[j * 2], S[j * 2 + 1]);} printf ("%s\n", cycle ()? "Unbounded": "Bounded");} return 0;}

Example 6-19 self-assembled UVa1572

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.