算法题 Gnome Tetravex

Gnome Tetravex from ZOJ(浙大OJ) Problem Set - 1008,链接http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1008

题目如下:

 Hart is engaged in playing an interesting game, Gnome Tetravex, these days. In the game, at the beginning, the player is given n*n squares. Each square is divided into four triangles marked four numbers (range from 0 to 9). In a square, the triangles are the left triangle, the top triangle, the right triangle and the bottom triangle. For example, Fig. 1 shows the initial state of 2*2 squares.

Fig. 1 The initial state with 2*2 squares The player is required to move the squares to the termination state. In the termination state, any two adjoining squares should make the adjacent triangle marked with the same number. Fig. 2 shows one of the termination states of the above example.
Fig. 2 One termination state of the above example It seems the game is not so hard. But indeed, Hart is not accomplished in the game. He can finish the easiest game successfully. When facing with a more complex game, he can find no way out. One day, when Hart was playing a very complex game, he cried out, "The computer is making a goose of me. It's impossible to solve it." To such a poor player, the best way to help him is to tell him whether the game could be solved. If he is told the game is unsolvable, he needn't waste so much time on it. Input The input file consists of several game cases. The first line of each game case contains one integer n, 0 <= n <= 5, indicating the size of the game. The following n*n lines describe the marking number of these triangles. Each line consists of four integers, which in order represent the top triangle, the right triangle, the bottom triangle and the left triangle of one square. After the last game case, the integer 0 indicates the termination of the input data set. Output You should make the decision whether the game case could be solved. For each game case, print the game number, a colon, and a white space, then display your judgment. If the game is solvable, print the string "Possible". Otherwise, please print "Impossible" to indicate that there's no way to solve the problem. Print a blank line between each game case. Note: Any unwanted blank lines or white spaces are unacceptable. Sample Input 2 5 9 1 4 4 4 5 6 6 8 5 4 0 4 4 3 2 1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4 0 Output for the Sample Input Game 1: Possible Game 2: Impossible

题目简述:

需要重排给的正方形,到达目标状态。在目标状态中,任何两个相邻正方形的相邻三角形上的数字都相同。

思路

最初的想法是用dfs来做,下面的代码是第一次提交的,时间未通过。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX 5
#define USE 1
#define NO_USE 0

typedef struct square
{
    unsigned char up;
    unsigned char right;
    unsigned char down;
    unsigned char left;
} square;

int global_n, global_keep_on, global_answer;
square global_data[MAX * MAX];
char global_data_inuse[MAX * MAX];
square *global_matrix[MAX][MAX];

void dfs(int x, int y)
{
    int temp, i;
    if (global_keep_on == 0)
    {
        return;
    }

    if (y == global_n)
    {
        global_answer = 1;
        global_keep_on = 0;
        return;
    }

    temp = global_n * global_n;
    if (x > 0 && y > 0)
    {
        for (i = 0; i < temp; i++)
        {
            if (global_data_inuse[i] == NO_USE 
                && global_matrix[y - 1][x]->down == global_data[i].up 
                && global_matrix[y][x - 1]->right == global_data[i].left)
            {
                global_matrix[y][x] = &global_data[i];
                global_data_inuse[i] = USE;
                if (x == global_n - 1)
                {
                    dfs(0, y + 1);
                }
                else
                {
                    dfs(x + 1, y);
                }
                global_data_inuse[i] = NO_USE;
            }
        }
    }
    else if (x > 0 && y == 0)
    {
        for (i = 0; i < temp; i++)
        {
            if (global_data_inuse[i] == NO_USE 
                && global_matrix[y][x - 1]->right == global_data[i].left)
            {
                global_matrix[y][x] = &global_data[i];
                global_data_inuse[i] = USE;
                if (x == global_n - 1)
                {
                    dfs(0, y + 1);
                }
                else
                {
                    dfs(x + 1, y);
                }
                global_data_inuse[i] = NO_USE;
            }
        }
    }
    else if (x == 0 && y > 0)
    {
        for (i = 0; i < temp; i++)
        {
            if (global_data_inuse[i] == NO_USE 
                && global_matrix[y - 1][x]->down == global_data[i].up)
            {
                global_matrix[y][x] = &global_data[i];
                global_data_inuse[i] = USE;
                dfs(x + 1, y);
                global_data_inuse[i] = NO_USE;
            }
        }
    }
    else if (x == 0 && y == 0)
    {
        for (i = 0; i < temp; i++)
        {
            if (global_data_inuse[i] == NO_USE)
            {
                global_matrix[y][x] = &global_data[i];
                global_data_inuse[i] = USE;
                dfs(x + 1, y);
                global_data_inuse[i] = NO_USE;
            }
        }
    }
}

int main()
{
    int temp, i, amount;
    amount = 0;
    while (scanf("%d", &global_n) != EOF)
    {
        amount++;
        if (global_n == 0)
        {
            break;
        }

        temp = global_n * global_n;
        for (i = 0; i < temp; i++)
        {
            scanf("%hhd %hhd %hhd %hhd", 
                &global_data[i].up, 
                &global_data[i].right, 
                &global_data[i].down, 
                &global_data[i].left);
        }

        global_answer = 0;
        global_keep_on = 1;
        memset(global_data_inuse, 0, MAX * MAX);
        dfs(0, 0);

        printf("Game %d: ", amount);
        if (global_answer == 1)
        {
            puts("Possible");
        }
        else if (global_answer == 0)
        {
            puts("Impossible");
        }
    }
    return 0;
}

刚开始我以为是多余的步骤的影响,所以对上面的代码进行了简化,下面是简化后的代码,但还是超时了:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX 5
#define USE 1
#define NO_USE 0

typedef struct square
{
    unsigned int up;
    unsigned int right;
    unsigned int down;
    unsigned int left;
} square;

int global_n, global_keep_on, global_answer;
square global_data[MAX * MAX];
int global_data_inuse[MAX * MAX];
square *global_matrix[MAX][MAX];

void dfs(int x, int y, int remain)
{
    int temp, i;
    if (global_keep_on == 0)
    {
        return;
    }

    if (y == global_n)
    {
        global_answer = 1;
        global_keep_on = 0;
        return;
    }

    if (x > 0 && y > 0)
    {
        for (i = 0; i < remain; i++)
        {
            temp = global_data_inuse[i];
            if (global_matrix[y - 1][x]->down == global_data[temp].up 
                && global_matrix[y][x - 1]->right == global_data[temp].left)
            {
                global_matrix[y][x] = &global_data[temp];

                global_data_inuse[i] = global_data_inuse[remain - 1];
                global_data_inuse[remain - 1] = temp;
                if (x == global_n - 1)
                {
                    dfs(0, y + 1, remain -1);
                }
                else
                {
                    dfs(x + 1, y, remain -1);
                }
                global_data_inuse[remain - 1] = global_data_inuse[i];
                global_data_inuse[i] = temp;
            }
        }
    }
    else if (x > 0 && y == 0)
    {
        for (i = 0; i < remain; i++)
        {
            temp = global_data_inuse[i];
            if (global_matrix[y][x - 1]->right == global_data[temp].left)
            {
                global_matrix[y][x] = &global_data[temp];

                global_data_inuse[i] = global_data_inuse[remain - 1];
                global_data_inuse[remain - 1] = temp;
                if (x == global_n - 1)
                {
                    dfs(0, y + 1, remain -1);
                }
                else
                {
                    dfs(x + 1, y, remain -1);
                }
                global_data_inuse[remain - 1] = global_data_inuse[i];
                global_data_inuse[i] = temp;
            }
        }
    }
    else if (x == 0 && y > 0)
    {
        for (i = 0; i < remain; i++)
        {
            temp = global_data_inuse[i];
            if (global_matrix[y - 1][x]->down == global_data[temp].up)
            {
                global_matrix[y][x] = &global_data[temp];

                global_data_inuse[i] = global_data_inuse[remain - 1];
                global_data_inuse[remain - 1] = temp;
                dfs(x + 1, y, remain -1);
                global_data_inuse[remain - 1] = global_data_inuse[i];
                global_data_inuse[i] = temp;
            }
        }
    }
    else if (x == 0 && y == 0)
    {
        for (i = 0; i < remain; i++)
        {
            temp = global_data_inuse[i];

            global_matrix[y][x] = &global_data[temp];

            global_data_inuse[i] = global_data_inuse[remain - 1];
            global_data_inuse[remain - 1] = temp;
            dfs(x + 1, y, remain -1);
            global_data_inuse[remain - 1] = global_data_inuse[i];
            global_data_inuse[i] = temp;
        }
    }
}

int main()
{
    int temp, i, amount;
    amount = 0;
    while (scanf("%d", &global_n) != EOF)
    {
        amount++;
        if (global_n == 0)
        {
            break;
        }

        temp = global_n * global_n;
        for (i = 0; i < temp; i++)
        {
            global_data_inuse[i] = i;
            scanf("%d %d %d %d", 
                &global_data[i].up, 
                &global_data[i].right, 
                &global_data[i].down, 
                &global_data[i].left);
        }

        global_answer = 0;
        global_keep_on = 1;

        dfs(0, 0, temp);

        printf("Game %d: ", amount);
        if (global_answer == 1)
        {
            puts("Possible");
        }
        else if (global_answer == 0)
        {
            puts("Impossible");
        }
    }
    return 0;
}

之后看了大佬的代码,才知道了其中的差距,代码来自https://blog.csdn.net/j_sure/article/details/38332079 ,相比我的代码,下面的代码更精简,而且是把完全一样的方块统计归纳在一处,这样就不必重复尝试,速度也就更快。

#include <stdio.h>
#include <iostream>
#include <string.h>

using namespace std;

struct node
{
    int up,right,down,left;
    bool operator == (const node &a)
    {
        return up == a.up && right == a.right && down == a.down && left == a.left;
    }
} p[30];

int mapp[30][30];
int sum[30];
bool flag;
int n ;

void dfs(int s)
{
    if(s == n*n)
    {
        flag = true ;
        return ;
    }
    int x = s/n,y = s%n;
    for(int i = 0 ; i < n*n && !flag ; i++)
    {
        if(sum[i])//如果该正方形已经没有了,就用下一个
        {
                if(x > 0 && p[i].up != p[mapp[x-1][y]].down) continue;//如果这个正方形和前一个正方形在同一列,那这个的up应该与前一个down相同
                if(y > 0 && p[i].left != p[mapp[x][y-1]].right)  continue;
            mapp[x][y] = i ;//表明这个位置可以放
            --sum[i] ;
            dfs(s+1) ;
            ++sum[i] ;//还原
        }
    }
}

int main()
{
    int cas = 1 ;
    while(scanf("%d",&n) && n)
    {
        memset(sum,0,sizeof(sum));
        for(int i = 0 ; i < n*n ; ++i)
        {
            scanf("%d %d %d %d",&p[i].up,&p[i].left,&p[i].down,&p[i].right);
            flag = false ;
            for(int j = 0 ; j < i ; ++j)
                if(sum[j] && p[j] == p[i])//优化,防止重复
                {
                    ++sum[j];
                    flag = true;
                    break;
                }
            if(!flag)
                ++sum[i];
        }
        memset(mapp,0,sizeof(mapp));
        flag = false;
        dfs(0);
        if(cas > 1)
            printf("\n");
        printf("Game %d: ",cas++);
        if(flag)
            printf("Possible\n");
        else
            printf("Impossible\n");
    }
    return 0;
}

下面是我对这位大佬的代码的总结:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct node
{
    int up, right, down, left;
} node;

int equal(node *a, node *b)
{
    return b->up == a->up && b->right == a->right && b->down == a->down && b->left == a->left;
}

node p[30];
int mapp[30][30];
int sum[30],sum_length;
int flag;
int n,n_2;

void dfs(int s)
{
    int x,y,i;
    if (s == n_2)
    {
        flag = 1;
        return;
    }
    x = s / n, y = s % n; //这里设计的很好
    for (i = 0; i < n_2 && !flag; i++)
    {
        if (sum[i]) //如果该正方形已经没有了,就用下一个
        {
            // 这里也很巧妙
            if (x > 0 && p[i].up != p[mapp[x - 1][y]].down)
                continue; //如果这个正方形和前一个正方形在同一列,那这个的up应该与前一个down相同
            if (y > 0 && p[i].left != p[mapp[x][y - 1]].right)
                continue;

            mapp[x][y] = i; //表明这个位置可以放
            --sum[i];
            dfs(s + 1);
            ++sum[i]; //还原
        }
    }
}

int main()
{
    int cas,i,j;
    cas = 1;
    while (scanf("%d", &n) && n)
    {
        n_2 = n * n;
        memset(sum, 0, sizeof(sum));
        sum_length = 0;
        for (i = 0; i < n_2; ++i)
        {
            scanf("%d %d %d %d", &p[i].up, &p[i].left, &p[i].down, &p[i].right);
            flag = 0;
            for (j = 0; j < i; ++j)
            {
                if (sum[j] && equal(&p[j], &p[i])) //优化,防止重复
                {
                    sum[j]++;
                    flag = 1;
                    break;
                }
            }

            if (!flag)
            {
                sum[i]++;
                sum_length++;
            }
        }

        memset(mapp, 0, sizeof(mapp));

        flag = 0;
        printf("sum:%d, n_2:%d\n",sum_length,n_2);
        dfs(0);
        if (cas > 1)
        {
            printf("\n");
        }

        printf("Game %d: ", cas++);
        if (flag)
        {
            printf("Possible\n");
        }
        else
        {
            printf("Impossible\n");
        }
    }
    return 0;
}

总结

算法总是存在很大的优化空间,总能找到更好的办法。

说点什么

avatar
  Subscribe  
提醒