Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

改进版,应该还有优化的空间

Posted by B06350214 at 2008-04-14 21:55:15 on Problem 1020
In Reply To:我的做法^_^ 140ms能过,还可以优化,哪位大哥帮忙优化一下 Posted by:fxzy at 2006-02-14 14:01:25
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int  cake[41][41];
int  size, n, piece[16], hash[16];
int  ok;
int  cmp(const void *a, const void *b)
{
    return (*(int*)b)-(*(int*)a);
}
int  check(int x, int y, int  s)
{
    int  i, j;
    for(i = x; i < s+x; i++)
        for(j = y; j < s+y; j++)
            if(cake[i][j]) return 0;
    return 1;
}

void  change(int  x, int y, int s, int t)
{
    int  i, j;
    for(i = x; i < x+s; i++)
        for(j = y; j < y+s; j++)
            cake[i][j] = t;
}
void  DFS()
{
    int  x, y;
    int  i, j;
    int  pre = -1;
    if(ok) return ;
    for(x = 1; x <= size; x++)
    {
        for(y = 1; y <= size; y++)
            if(!cake[x][y]) break;
        if(y != size+1)break;
    }
                      //find the first place for place
    if(x == size+1 && y == size+1) {ok = 1; return ;}
    for(i = 1; i < n; i++)
    {
        if(!hash[i])
        {
            if(pre != piece[i] && x+piece[i]-1 <= size && y+piece[i]-1 <= size && check(x, y, piece[i]))
            {
                hash[i] = 1;
                change(x, y, piece[i], 1);
                DFS();
                change(x, y, piece[i], 0);
                if(i != 0)
                    hash[i] = 0;
            }
            pre = piece[i];
        }

    }
}
int  main()
{
    int  t;
    int  i, j;
    scanf("%d", &t);
    while(t--)
    {
        ok = 0;
        memset(hash, 0, sizeof(hash));
        memset(cake, 0, sizeof(cake));
        scanf("%d %d", &size, &n);
        for(i = 0, j = 0; i < n; i++)
        {
            scanf("%d", &piece[i]);
            j += piece[i]*piece[i];
        }
        if(j != size*size)
        {
            printf("HUTUTU!\n");
            continue;
        }
        qsort(piece, n, sizeof(piece[0]), cmp);
        for(i = 0; i <= piece[0]; i++)
            for(j = 0; j <= piece[0]; j++)
                cake[i][j] = 1;
        DFS();
        if(ok) printf("KHOOOOB!\n");
        else  printf("HUTUTU!\n");
    }
    return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator