暑假训练 The Triangle Game (OpenJ_Bailian - 1574)

题目描述:
在这里插入图片描述
In the triangle game you start off with six triangles numbered on each edge, as in the example above. You can slide and rotate the triangles so they form a hexagon, but the hexagon is only legal if edges common to two triangles have the same number on them. You may not flip any triangle over. Two legal hexagons formed from the six triangles are illustrated below.
在这里插入图片描述
The score for a legal hexagon is the sum of the numbers on the outside six edges. Your problem is to find the highest score that can be achieved with any six particular triangles.
Input
The input will contain one or more data sets. Each data set is a sequence of six lines with three integers from 1 to 100 separated by blanks on each line. Each line contains the numbers on the triangles in clockwise order. Data sets are separated by a line containing only an asterisk. The last data set is followed by a line containing only a dollar sign.
Output
For each input data set, the output is a line containing only the word “none” if there are no legal hexagons or the highest score if there is a legal hexagon.
Sample Input
1 4 20
3 1 5
50 2 3
5 2 7
7 5 20
4 7 50
*
10 1 20
20 2 30
30 3 40
40 4 50
50 5 60
60 6 10
*
10 1 20
20 2 30
30 3 40
40 4 50
50 5 60
10 6 60
$
Sample Output
152
21
none
代码如下:

#include<iostream>
#include<cstdio>
#include<string.h>
using namespace std;
int tri[10][3];
bool vis[10][4];
int way;

void dfs(int a, int b, int c, int d)
{
    b++;
    if(b == 6)
    {
        int n1 = 0;
        if(tri[0][d] == tri[a][c])
        {
            vis[a][c] = 1;
            for(int i = 0; i < 6; i++)
            {
                for(int j = 0; j < 3; j++)
                {
                    if(!vis[i][j]){n1 +=  tri[i][j];break;}
                }
            }
            if(way < n1)way = n1;
            vis[a][c] = 0;
        }
        return ;
    }
    vis[a][3] = 1;
    if(b != 6)
    {
        vis[a][c] = 1;
            for(int i = 0; i < 6; i++)
            {
                if(!vis[i][3])
                {
                    for(int k = 0; k < 3; k++)
                    {
                        if(tri[i][k] == tri[a][c])
                        {
                            vis[i][k] = 1;
                            int next = (k + 1) % 3;
                            dfs(i, b, next, d);
                            vis[i][k] = 0;
                        }
                    }
                }
            }
            vis[a][c] = 0;
    }
    vis[a][3] = 0;
    return ;
}

int main()
{
    int y = 1;
    char cc;
    while(y)
    {
        memset(vis, 0, sizeof vis);
        y = 0; way = 0;
        for(int i = 0; i < 6; i++)
            scanf("%d%d%d", &tri[i][0], &tri[i][1], &tri[i][2]);
        vis[0][2] = 1;
        dfs(0, 0, 0, 2);
        vis[0][2] = 0;
        vis[0][0] = 1;
        dfs(0, 0, 1, 0);
        vis[0][0] = 0;
        vis[0][1] = 1;
        dfs(0, 0, 2, 1);
        vis[0][1] = 0;
        if(way != 0)printf("%d\n", way);
        else printf("none\n");
        scanf("\n");
        scanf("%c", &cc);
        if(cc == '*')y = 1;
            else y = 0;
    }
    return 0;
}

emmm代码很丑,开始读错题了,没发现是顺时针排列的边,WA了几下,后来终于惊觉,改的有点草率。。。

猜你喜欢

转载自blog.csdn.net/wbl1970353515/article/details/82819395
今日推荐