Advertisement
Guest User

sudoku

a guest
May 30th, 2015
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.97 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. #define N 9
  5. struct Node
  6. {
  7. char sn[10];
  8. struct Node *next;
  9. } *head[N]={0,0,0,0,0,0,0,0,0};
  10. int horizantal_check(char **s)
  11. {
  12.  
  13. int i,j,k;
  14. for(i=0;i<N;i++)
  15. {
  16. for(j=0;j<N-1;j++)
  17. {
  18. for(k=j+1;k<N;k++)
  19. {
  20. if(s[i][j]==s[i][k])
  21. {
  22. return 1;
  23. }
  24. }
  25. }
  26. }
  27. return 0;
  28. }
  29. int vertical_check(char **s)
  30. {
  31. int i,j,k;
  32. for(i=0;i<N;i++)
  33. {
  34. for(j=0;j<N-1;j++)
  35. {
  36. for(k=i+1;k<N;k++)
  37. {
  38. if(s[i][j]==s[k][j])
  39. {
  40. return 1;
  41. }
  42. }
  43. }
  44. }
  45. return 0;
  46. }
  47. int check(char **s)
  48. {
  49. if(horizantal_check(s)==1)
  50. return 1;
  51. else
  52. if(vertical_check(s)==1)
  53. return 1;
  54. return 0;
  55. }
  56. void swap(char *s1,int i,int j)
  57. {
  58. int temp;
  59. temp=s1[i];
  60. s1[i]=s1[j];
  61. s1[j]=temp;
  62. }
  63. void newnode(char *s1,int x)
  64. {
  65. struct Node *new=(struct Node *)calloc(1,sizeof(struct Node));
  66. strcpy(new->sn,s1);
  67. new->next=head[x];
  68. head[x]=new;
  69. }
  70. void comb(char *s1,int i,int x,char **a)
  71. {
  72. int j;
  73. if(i==N)
  74. {
  75. if(clean(s1,a,x)==0)
  76. newnode(s1,x);
  77. }
  78. else
  79. {
  80. for(j=i;j<N;j++)
  81. {
  82. swap(s1,i,j);
  83. comb(s1,i+1,x,a);
  84. swap(s1,i,j);
  85. }
  86. }
  87. }
  88. void traverse(int x)
  89. {
  90. struct Node *temp;
  91. temp=head[x];
  92. if(!temp)
  93. printf("No List");
  94. else
  95. {
  96. while(temp)
  97. {
  98. printf("%s\n",temp->sn);
  99. temp=temp->next;
  100. }
  101. }
  102. }
  103. void make_LL(char **s,char **a)
  104. {
  105. int i;
  106. for(i=0;i<N;i++)
  107. {
  108. comb(s[i],0,i,a);
  109. }
  110. }
  111. void display(char **s)
  112. {
  113. int i;
  114. for(i=0;i<N;i++)
  115. {
  116. printf("%s\n",s[i]);
  117. }
  118. printf("\n\n");
  119. }
  120. int clean(char *sn,char **a,int n)
  121. {
  122. int i,j,K,L,m,o;
  123. for(i=0;i<N;i++)
  124. {
  125. if((sn[i]!=a[n][i])&&(a[n][i]!='0'))
  126. return 1;
  127. }
  128. for(i=0;i<N;i++)
  129. {
  130. if(i!=n)
  131. {
  132. for(j=0;j<N;j++)
  133. {
  134. if(sn[j]==a[i][j])
  135. return 1;
  136. }
  137. }
  138. }
  139. K=(n/3)*3;
  140. for(j=0;j<N;j++)
  141. {
  142. L=(j/3)*3;
  143. for(m=K;m<K+3;m++)
  144. {
  145. for(o=L;o<L+3;o++)
  146. {
  147. if(m!=n)
  148. {
  149. if(sn[j]==a[m][o])
  150. return 1;
  151. }
  152. }
  153. }
  154. }
  155. return 0;
  156. }
  157. void dial(char **s,char **a)
  158. {
  159. struct Node *temp[N];
  160. temp[0]=head[0];
  161. while(temp[0])
  162. {
  163. strcpy(s[0],temp[0]->sn);
  164. temp[0]=temp[0]->next;
  165. temp[1]=head[1];
  166. while(temp[1])
  167. {
  168. strcpy(s[1],temp[1]->sn);
  169. temp[1]=temp[1]->next;
  170. temp[2]=head[2];
  171. while(temp[2])
  172. {
  173. strcpy(s[2],temp[2]->sn);
  174. temp[2]=temp[2]->next;
  175. temp[3]=head[3];
  176. while(temp[3])
  177. {
  178. strcpy(s[3],temp[3]->sn);
  179. temp[3]=temp[3]->next;
  180. temp[4]=head[4];
  181. while(temp[4])
  182. {
  183. strcpy(s[4],temp[4]->sn);
  184. temp[4]=temp[4]->next;
  185. temp[5]=head[5];
  186. while(temp[5])
  187. {
  188. strcpy(s[5],temp[5]->sn);
  189. temp[5]=temp[5]->next;
  190. temp[6]=head[6];
  191. while(temp[6])
  192. {
  193. strcpy(s[6],temp[6]->sn);
  194. temp[6]=temp[6]->next;
  195. temp[7]=head[7];
  196. while(temp[7])
  197. {
  198. strcpy(s[7],temp[7]->sn);
  199. temp[7]=temp[7]->next;
  200. temp[8]=head[8];
  201. while(temp[8])
  202. {
  203. strcpy(s[8],temp[8]->sn);
  204. temp[8]=temp[8]->next;
  205. //******************************************
  206. if(check(s)==0)
  207. {
  208. display(s);
  209. goto over;
  210. }
  211. //******************************************
  212. }
  213. }
  214. }
  215. }
  216. }
  217. }
  218. }
  219. }
  220. }
  221. printf("some error!!");
  222. over:
  223. printf("solution found!!");
  224. }
  225. main(int argc,char *argv[])
  226. {
  227. FILE *fp;
  228. char **s,**a,i,j,*f;
  229. s=(char **)calloc(N,sizeof(char *));
  230. a=(char **)calloc(N,sizeof(char *));
  231. for(i=0;i<N;i++)
  232. {
  233. s[i]=(char *)calloc(N,sizeof(char));
  234. a[i]=(char *)calloc(N,sizeof(char));
  235. }
  236. for(i=0;i<N;i++)
  237. {
  238. for(j=0;j<N;j++)
  239. {
  240. s[i][j]='0'+j+1;
  241. }
  242. }
  243. fp=fopen(argv[1],"r");
  244. for(i=0;i<N;i++)
  245. {
  246. if((fscanf(fp,"%s\n",a[i]))!=EOF);
  247. else
  248. printf("input file wrong format!");
  249. }
  250. make_LL(s,a);
  251. // comb(s[0],0);
  252. fclose(fp);
  253. dial(s,a);
  254. // for(i=0;i<N;i++)
  255. // traverse(i);
  256. free(a);
  257. free(s);
  258. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement