Tuesday, August 16, 2011

UVA 12036 algorithm and solution C++ Code

All we need to check is whether an element in more than N times or not in the whole list. If does then the answer is true or else false.

For clarification see the following arrangement of 4*4 array. An element can only be at most four times to keep the grid stable. If it is five times then there is no place to put the extra element avoiding repetition .

1  x  x  x
x  1  x  x
x  x  1  x
x  x  x  1

Code:


#include<stdio.h>

int main(){

    int i,m,n,num,t,k,f;
    int frq[102]={0};

    scanf("%d",&t);

    k=0;
    while(++k<=t){

        scanf("%d",&n);

        m=n*n;
        f=1;
        for(i=101;i--;){
        frq[i]=0;
        }


        for(i=m ;i>0;i-- ){
        scanf( "%d", &num);
        frq[num]++;
              if( frq[num]>n ){f=0; break;}
        }
        i--;
        while(i>0){
        scanf( "%d", &num);
        i--;
        }

        if(f) printf("Case %d: yes\n",k);
        else  printf("Case %d: no\n",k);
    }

 return 0;
}

No comments:

Post a Comment