ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1054. Tower of Hanoi

(WA) Here is my code in C, could anyone give me some test cases to break it please?
Posted by Jonathan Yu 27 Dec 2002 19:20
/*
    Prog: acm_1054
    results:
*/

#include <stdio.h>
#include <math.h>
int a = 1,b = 2,c = 4;
int disks[5];
int numbers[5][101];
int dest[5][101];
int destdisks[5];
int count;
int found;
int num;

int finish()
{
    int i,j;
    found = 1;

    for(i = 1;i<5;i++)
    {
        for(j = 0;j<=num;j++)
        {
            if(numbers[i][j]!=dest[i][j])
            {
                found = 0;
                break;
            }
        }
        if(!found)
            break;
    }

    if(found)return 1;
    if(disks[b]==num)
        return 1;
    return 0;
}
void move(int s,int n, int d)
{
    int k ;
    if(found||finish())return;
    if(n==1)
    {
        disks[d]++;
        numbers[d][disks[d]] = numbers[s][disks[s]];
        numbers[s][disks[s]] = 0;
        disks[s]--;

        count++;

    }else {
          k = 7^(s|d);

        move(s,n-1,k);

        move(s,1,d);
        move(k,n-1,d);
        finish();
    }
}

int main(int argc, char *argv[]) {
    int pos[100],i,j;
    int tmp[101];
    scanf("%d",&num);
    disks[a] = num;
    for(i = 0;i<num;i++)
        numbers[a][i+1] = num-i;

    for(i = 1;i<=num;i++)
    {
        scanf("%d",&pos[i]);
        pos[i]  =(int)pow(2,pos[i]-1);
        destdisks[pos[i]]++;
        dest[pos[i]][destdisks[pos[i]]] = i;
    }
    for(i = 1;i<5;i++)
    {
        for(j = 1;j<=destdisks[i];j++)
            tmp[destdisks[i]-j+1] = dest[i][j];
        for(j = 1;j<=destdisks[i];j++)
            dest[i][j] = tmp[j];
    }



    move(a,num,b);
    if(found)
        printf("%d",count);
    else printf("%d",-1);


    return 0;
}
// Cheers!