UVA11586 Train Tracks【水题】

Andy loves his set of wooden trains and railroad tracks. Each day, Daddy has to build a new track for him. The tracks he likes best form a simple loop with no branches or dead ends, so he can run his trains around and around for hours until it is time for the big crash that destroys the whole construction.
在这里插入图片描述
Some wooden pieces

So here is the question: Given a set of track pieces, can you form a simple loop with them, while using up all the pieces?
Each piece of track is described by the connectors at both ends. A standard piece has one “male” and one “female” connector. But there are also track pieces with two male or two female connectors, as shown in the front right of the picture.
To fit together, each male connector must be connected to a female connector. Unlike real wooden tracks, our pieces are assumed to be flexible, so their length or shape is not an issue here. However, you may not connect the two ends of the same piece together.
Input
Input begins with the number of test cases. Each following line contains one test case. Each test case consists of a list of between 1 and 50 (inclusive) train track pieces. A piece is described by two code letters: ‘M’ for male or ‘F’ for female connector. Pieces are separated by space characters.
Output
For each test case, output a line containing either ‘LOOP’ or ‘NO LOOP’ to indicate whether or not all the pieces can be joined into a single loop.
Sample Input
4
MF MF
FM FF MF MM
MM FF
MF MF MF MF FF
Sample Output
LOOP
LOOP
LOOP
NO LOOP

问题链接UVA11586 Train Tracks
问题简述:给定若干积木片,每片的两端仅仅能是凸或凹(M或F),凸凹可拼起来,问是否能拼成一个环?
问题分析
    统计一下就可以了,需要凸凹数量相同,但是不能是一片。。
    懂得图论是加分的,可以看作欧拉循环,每一个积木片看作一条边,凸或凹看作结点。
程序说明:(略)
参考链接:(略)
题记:心中要有图。

AC的C++语言程序如下:

/* UVA11586 Train Tracks */

#include <bits/stdc++.h>

using namespace std;

const int N = 50;
char s[N * 3 + 1];

int main()
{
    int n;
    scanf("%d", &n);
    gets(s);
    while(n--) {
        int m = 0, f = 0;

        gets(s);
        for(int i = 0; s[i]; i++) {
            if(s[i] == 'M') m++;
            else if(s[i] == 'F') f++;
        }

        if(f == m && m > 1) puts("LOOP");
        else puts("NO LOOP");
    }

    return 0;
}
发布了2289 篇原创文章 · 获赞 2373 · 访问量 265万+

猜你喜欢

转载自blog.csdn.net/tigerisland45/article/details/105551958