Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- #define N 9
- struct Node
- {
- char sn[10];
- struct Node *next;
- } *head[N]={0,0,0,0,0,0,0,0,0};
- int horizantal_check(char **s)
- {
- int i,j,k;
- for(i=0;i<N;i++)
- {
- for(j=0;j<N-1;j++)
- {
- for(k=j+1;k<N;k++)
- {
- if(s[i][j]==s[i][k])
- {
- return 1;
- }
- }
- }
- }
- return 0;
- }
- int vertical_check(char **s)
- {
- int i,j,k;
- for(i=0;i<N;i++)
- {
- for(j=0;j<N-1;j++)
- {
- for(k=i+1;k<N;k++)
- {
- if(s[i][j]==s[k][j])
- {
- return 1;
- }
- }
- }
- }
- return 0;
- }
- int check(char **s)
- {
- if(horizantal_check(s)==1)
- return 1;
- else
- if(vertical_check(s)==1)
- return 1;
- return 0;
- }
- void swap(char *s1,int i,int j)
- {
- int temp;
- temp=s1[i];
- s1[i]=s1[j];
- s1[j]=temp;
- }
- void newnode(char *s1,int x)
- {
- struct Node *new=(struct Node *)calloc(1,sizeof(struct Node));
- strcpy(new->sn,s1);
- new->next=head[x];
- head[x]=new;
- }
- void comb(char *s1,int i,int x,char **a)
- {
- int j;
- if(i==N)
- {
- if(clean(s1,a,x)==0)
- newnode(s1,x);
- }
- else
- {
- for(j=i;j<N;j++)
- {
- swap(s1,i,j);
- comb(s1,i+1,x,a);
- swap(s1,i,j);
- }
- }
- }
- void traverse(int x)
- {
- struct Node *temp;
- temp=head[x];
- if(!temp)
- printf("No List");
- else
- {
- while(temp)
- {
- printf("%s\n",temp->sn);
- temp=temp->next;
- }
- }
- }
- void make_LL(char **s,char **a)
- {
- int i;
- for(i=0;i<N;i++)
- {
- comb(s[i],0,i,a);
- }
- }
- void display(char **s)
- {
- int i;
- for(i=0;i<N;i++)
- {
- printf("%s\n",s[i]);
- }
- printf("\n\n");
- }
- int clean(char *sn,char **a,int n)
- {
- int i,j,K,L,m,o;
- for(i=0;i<N;i++)
- {
- if((sn[i]!=a[n][i])&&(a[n][i]!='0'))
- return 1;
- }
- for(i=0;i<N;i++)
- {
- if(i!=n)
- {
- for(j=0;j<N;j++)
- {
- if(sn[j]==a[i][j])
- return 1;
- }
- }
- }
- K=(n/3)*3;
- for(j=0;j<N;j++)
- {
- L=(j/3)*3;
- for(m=K;m<K+3;m++)
- {
- for(o=L;o<L+3;o++)
- {
- if(m!=n)
- {
- if(sn[j]==a[m][o])
- return 1;
- }
- }
- }
- }
- return 0;
- }
- void dial(char **s,char **a)
- {
- struct Node *temp[N];
- temp[0]=head[0];
- while(temp[0])
- {
- strcpy(s[0],temp[0]->sn);
- temp[0]=temp[0]->next;
- temp[1]=head[1];
- while(temp[1])
- {
- strcpy(s[1],temp[1]->sn);
- temp[1]=temp[1]->next;
- temp[2]=head[2];
- while(temp[2])
- {
- strcpy(s[2],temp[2]->sn);
- temp[2]=temp[2]->next;
- temp[3]=head[3];
- while(temp[3])
- {
- strcpy(s[3],temp[3]->sn);
- temp[3]=temp[3]->next;
- temp[4]=head[4];
- while(temp[4])
- {
- strcpy(s[4],temp[4]->sn);
- temp[4]=temp[4]->next;
- temp[5]=head[5];
- while(temp[5])
- {
- strcpy(s[5],temp[5]->sn);
- temp[5]=temp[5]->next;
- temp[6]=head[6];
- while(temp[6])
- {
- strcpy(s[6],temp[6]->sn);
- temp[6]=temp[6]->next;
- temp[7]=head[7];
- while(temp[7])
- {
- strcpy(s[7],temp[7]->sn);
- temp[7]=temp[7]->next;
- temp[8]=head[8];
- while(temp[8])
- {
- strcpy(s[8],temp[8]->sn);
- temp[8]=temp[8]->next;
- //******************************************
- if(check(s)==0)
- {
- display(s);
- goto over;
- }
- //******************************************
- }
- }
- }
- }
- }
- }
- }
- }
- }
- printf("some error!!");
- over:
- printf("solution found!!");
- }
- main(int argc,char *argv[])
- {
- FILE *fp;
- char **s,**a,i,j,*f;
- s=(char **)calloc(N,sizeof(char *));
- a=(char **)calloc(N,sizeof(char *));
- for(i=0;i<N;i++)
- {
- s[i]=(char *)calloc(N,sizeof(char));
- a[i]=(char *)calloc(N,sizeof(char));
- }
- for(i=0;i<N;i++)
- {
- for(j=0;j<N;j++)
- {
- s[i][j]='0'+j+1;
- }
- }
- fp=fopen(argv[1],"r");
- for(i=0;i<N;i++)
- {
- if((fscanf(fp,"%s\n",a[i]))!=EOF);
- else
- printf("input file wrong format!");
- }
- make_LL(s,a);
- // comb(s[0],0);
- fclose(fp);
- dial(s,a);
- // for(i=0;i<N;i++)
- // traverse(i);
- free(a);
- free(s);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement