A C++ code is given here.
#include<iostream>
#include<vector>
#include<cstring>
using namespace std;
bool mark[20];
vector<int> v[20];
int count=0,n;
void solve(int row)
{
if(row==n)count++;
else
for(int i=0;i<v[row].size();i++)
{
if(!mark[v[row][i]])
{
mark[v[row][i]]=true;
solve(row+1);
mark[v[row][i]]=false;
}
}
}
int main()
{
int x, t;cin>>n;
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
{
cin>>x;
if(x)v[i].push_back(j);
}
memset(mark,false,sizeof(mark));
solve(0);
cout<<count<<endl;
}
I want to write the corresponding C++ code into C. Here's the equivalent code in C but it is in infinite loop. I can't figure out where it's wrong.
[code]
include
include
define INF 100
int count =0, numvertex;
int color[20];
int matrix[20][21];
void solve (int cur_row)
{
if( cur_row == numvertex)count++;
else{int k;
for(k=0;matrix[cur_row][k] != INF;k++){
if( !color[matrix[cur_row][k]] ){
color[matrix[cur_row][k]]=1;
solve(cur_row+1);
color[matrix[cur_row][k]]=0;
}
}
}}
int main()
{
int t;
for(scanf("%d",&t);t--
{
int I, J, temp;
memset (matrix, 0, sizeof matrix); count=0;
memset (color, 0, sizeof color);
scanf("%d",&numvertex);
for(I=0;I int ind=0;
for(J=0;J scanf("%d",&temp);
if(temp)matrix[I][ind++]=J;
}
matrix[I][ind]=INF;
}
solve(0);
printf("%d\n",count);
}
return 0;
}[/code]
Several n by n boolean matrices are in input. ( n <= 20)