c2tarun
December 18th, 2010, 02:41 PM
I was trying to solve this problem:
http://www.codechef.com/problems/MIXTURES/
this is my code:
#include<stdio.h>
#include<string.h>
char words[100000][2];
int n; // number of words
int sum(int* p)
{
int s=0,i;
for(i=0;i<n;i++)
s+=*(p+i);
return s;
}
int func(int c, int check[])
{
int i;
check[c]=1;
if(sum(&check[0]) != n)
{
for(i=0;i<n;i++)
{
if(check[i] == 0 && words[c][1] == words[i][0] )
{
func(i,check);
// check[i]=0;
}
}
}
else
{
if(sum(&check[0]) == n)
return 1;
else
return 0;
}
return 0;
}
int main(void)
{
char *temp=NULL;
size_t strn=1000;
int t; // number of test cases
int check[100000];
int templen;
int i; // loop variable
memset(check,0,sizeof(check));
getline(&temp,&strn,stdin);
t=atoi(temp);
while(t--)
{
getline(&temp,&strn,stdin);
n=atoi(temp);
for(i=0;i<n;i++)
{
getline(&temp,&strn,stdin);
templen=strlen(temp);
words[i][0]=*temp;
words[i][1]=*(temp+templen-2);
}
for(i=0;i<n;i++)
{
if(func(i,check) == 1)
break;
}
if(i<n)
printf("Ordering is possible.\n");
else
printf("The door cannot be opened.\n");
}
return 0;
}
I don't know exactly what is wrong with my code.
check is a local variable to main, but I am copying check of main to a local check of function 'func'. problem is, i am only changing the value of local check of 'func' but that change is also reflected in main. Can anyone please tell me what is wrong in my code.
thanks
http://www.codechef.com/problems/MIXTURES/
this is my code:
#include<stdio.h>
#include<string.h>
char words[100000][2];
int n; // number of words
int sum(int* p)
{
int s=0,i;
for(i=0;i<n;i++)
s+=*(p+i);
return s;
}
int func(int c, int check[])
{
int i;
check[c]=1;
if(sum(&check[0]) != n)
{
for(i=0;i<n;i++)
{
if(check[i] == 0 && words[c][1] == words[i][0] )
{
func(i,check);
// check[i]=0;
}
}
}
else
{
if(sum(&check[0]) == n)
return 1;
else
return 0;
}
return 0;
}
int main(void)
{
char *temp=NULL;
size_t strn=1000;
int t; // number of test cases
int check[100000];
int templen;
int i; // loop variable
memset(check,0,sizeof(check));
getline(&temp,&strn,stdin);
t=atoi(temp);
while(t--)
{
getline(&temp,&strn,stdin);
n=atoi(temp);
for(i=0;i<n;i++)
{
getline(&temp,&strn,stdin);
templen=strlen(temp);
words[i][0]=*temp;
words[i][1]=*(temp+templen-2);
}
for(i=0;i<n;i++)
{
if(func(i,check) == 1)
break;
}
if(i<n)
printf("Ordering is possible.\n");
else
printf("The door cannot be opened.\n");
}
return 0;
}
I don't know exactly what is wrong with my code.
check is a local variable to main, but I am copying check of main to a local check of function 'func'. problem is, i am only changing the value of local check of 'func' but that change is also reflected in main. Can anyone please tell me what is wrong in my code.
thanks