Code Snippets

  

C++ Source Code


Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,630 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,062 people online right now. Registration is fast and FREE... Join Now!





Suduku Puzzle

Suduku Puzzle Solver

Submitted By: harshakirans
Actions:
Rating:
Views: 3,864

Language: C++

Last Modified: May 5, 2006
Instructions: Enter the given values for the blank spaces enter the value as 0

Snippet


  1. #include<iostream.h>
  2. #include<conio.h>
  3. #include<process.h>
  4.  
  5. int a[9][9]={0,0,9,0,0,0,0,2,0,
  6.           0,6,0,0,0,9,0,0,5,
  7.           0,0,0,8,7,0,0,0,0,
  8.           0,0,1,0,8,0,0,0,0,
  9.           0,4,0,5,0,7,1,3,0,
  10.           6,0,0,0,1,0,4,0,0,
  11.           0,0,0,0,4,8,2,0,0,
  12.           7,0,0,6,0,0,0,5,0,
  13.           0,9,0,0,0,0,8,0,0};
  14. int b[9][9],k,l,m,z,flag=0,g,h;
  15. int horizontal(int,int,int);
  16. int vertical(int,int,int);
  17. int box(int,int,int);
  18. int assume(int,int);
  19. void main()
  20. {
  21. clrscr();
  22. cout<<"enter values\n";
  23. for(int i=0;i<9;i++)
  24. for(int j=0;j<9;j++)
  25. cin>>a[i][j];
  26.  
  27. for(i=0;i<9;i++)
  28. for(j=0;j<9;j++)
  29. b[i][j]=a[i][j];
  30.  
  31. for(i=0;i<9;i=i+1)
  32. for(j=0;j<9;j=j+1)
  33. {
  34. //flag=0;
  35. if(a[i][j]==0)
  36. {
  37. if(flag)
  38. {
  39. i=g;j=h;
  40. flag=0;
  41. }
  42. if(assume(i,j)==2)
  43. {
  44. flag=1;
  45. if(j==0)
  46. {
  47. h=8;
  48. g=i;
  49. g--;
  50. }
  51. else
  52. {
  53. g=i;
  54. h=j;
  55. h--;
  56. }
  57. if(a[g][h]!=0)
  58. {
  59. if(h==0)
  60. {
  61. h=8;
  62. g--;
  63. }
  64. else
  65. {
  66. h--;
  67. }
  68. if(a[g][h]!=0)
  69. {
  70. if(h==0)
  71. {
  72. h=8;
  73. g--;
  74. }
  75. else
  76. {
  77. h--;
  78. }
  79. if(a[g][h]!=0)
  80. {
  81. if(h==0)
  82. {
  83. h=8;
  84. g--;
  85. }
  86. else
  87. {
  88. h--;
  89. }
  90. if(a[g][h]!=0)
  91. {
  92. if(h==0)
  93. {
  94. h=8;
  95. g--;
  96. }
  97. else
  98. {
  99. h--;
  100. }
  101. }
  102.  
  103. }
  104.  
  105. }
  106.  
  107. }
  108. }
  109.  
  110. }
  111. }
  112. for(i=0;i<9;i++)
  113. {
  114. for(j=0;j<9;j++)
  115. cout<<b[i][j]<<"\t";
  116. cout<<"\n";
  117. }
  118. getch();
  119. }
  120.  
  121.  
  122. int horizontal(int i,int j,int z)
  123. {
  124. k=i;l=j;
  125. for(j=0;j<9;j++)
  126. {
  127. if(j==l)
  128. {
  129. if(j==8)
  130. break;
  131. j++;
  132. }
  133. if((z==b[i][j]))
  134. return 0;
  135. }
  136. return 1;
  137. }
  138. int vertical(int i,int j,int z)
  139. {
  140. k=i;
  141. l=j;
  142. for(i=0;i<9;i++)
  143. {
  144. if(i==k)
  145. {
  146. if(i==8)
  147. break;
  148. i++;
  149. }
  150. if((z==b[i][j]))
  151. return 0;
  152. }
  153. return 1;
  154. }
  155. int box(int i,int j,int z)
  156. {
  157. k=i;
  158. l=j;
  159. if(i<=2&&j<=2)
  160. {
  161. for(int m=0;m<=2;m++)
  162. {
  163. for(int n=0;n<=2;n++)
  164. {
  165. if((k!=m&&l!=n)&&(z==b[m][n])) //checkout here
  166. return 0;
  167. }}
  168. return 1;
  169. }
  170.  
  171. else if(i<=2&&j<=5)
  172. {
  173. for(int m=0;m<=2;m++)
  174. for(int n=3;n<=5;n++)
  175. {
  176. if(k==m&&l==n);
  177. else if(z==b[m][n])
  178. return 0;
  179. }
  180. return 1;
  181. }
  182.  
  183. else if(i<=2&&j<=8)
  184. {
  185. for(int m=0;m<=2;m++)
  186. for(int n=6;n<=8;n++)
  187. {
  188. if(k==m&&l==n);
  189. else if(z==b[m][n])
  190. return 0;
  191. }
  192. return 1;
  193. }
  194.  
  195. else if(i<=5&&j<=2)
  196. {
  197. for(int m=3;m<=5;m++)
  198. for(int n=0;n<=2;n++)
  199. {
  200. if(k==m&&l==n);
  201. else if(z==b[m][n])
  202. return 0;
  203. }
  204. return 1;
  205. }
  206.  
  207. else if(i<=5&&j<=5)
  208. {
  209. for(int m=3;m<=5;m++)
  210. for(int n=3;n<=5;n++)
  211. {
  212. if(k==m&&l==n);
  213. else if(z==b[m][n])
  214. return 0;
  215. }
  216. return 1;
  217. }
  218.  
  219. else if(i<=5&&j<=8)
  220. {
  221. for(int m=3;m<=5;m++)
  222. for(int n=6;n<=8;n++)
  223. {
  224. if(k==m&&l==n);
  225. else if(z==b[m][n])
  226. return 0;
  227. }
  228. return 1;
  229. }
  230.  
  231. else if(i<=8&&j<=2)
  232. {
  233. for(int m=6;m<=8;m++)
  234. for(int n=0;n<=2;n++)
  235. {
  236. if(k==m&&l==n);
  237. else if(z==b[m][n])
  238. return 0;
  239. }
  240. return 1;
  241. }
  242.  
  243. else if(i<=8&&j<=5)
  244. {
  245. for(int m=6;m<=8;m++)
  246. for(int n=3;n<=5;n++)
  247. {
  248. if(k==m&&l==n);
  249. else if(z==b[m][n])
  250. return 0;
  251. }
  252. return 1;
  253. }
  254.  
  255. else if(i<=8&&j<=8)
  256. {
  257. for(int m=6;m<=8;m++)
  258. for(int n=6;n<=8;n++)
  259. {
  260. if(k==m&&l==n);
  261. else if(z==b[m][n])
  262. return 0;
  263. }
  264. return 1;
  265. }
  266.  
  267. return 1;
  268. }
  269.  
  270. int assume(int i,int j)
  271. {
  272. m=b[i][j]+1;
  273. while(1)
  274. {
  275. b[i][j]=m;
  276. if(m>9)
  277. {
  278. b[i][j]=0;
  279. return 2;
  280. }
  281. if(vertical(i,j,b[i][j]))
  282. if(horizontal(i,j,b[i][j]))
  283. if(box(i,j,b[i][j]))
  284. {
  285. b[i][j]=m;
  286. return 0;
  287. }
  288. m++;
  289. }
  290. }

Copy & Paste


Comments


idle_09 2008-06-24 13:15:18

I tried to run this program in Visual C++ 2008 and it gave me an error stating: "fatal error C1083: Cannot open include file: 'iostream.h': No such file or directory". Can anyone tell me what should i do?

harshakirans 2008-08-06 07:56:47

Dude. . Conio.h is Borland/Turbo C Library. It does not confirm to C89 or c99 Standards. (It's a C Library) iostream.h is now just iostream. (New C++ Standards) Use #include using namespace std; Visual C++ is used for Windows Programming (Win32 API) and .NET Programming using C++. You'll be better with good old Turbo C or Borland C++ 5.5 Tools if you are learning the Indian version of C++ [;)](Unupdated since ages. Just Job Oriented courses you see.)

harshakirans 2008-08-06 07:59:06

so use [code] #include using namespace std; [\code] and comment the commands clrscr() and getch()


Add comment


You must be registered and logged on to </dream.in.code> to leave comments.




Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month