Sign in with
Sign up | Sign in
Your question
Closed

C++ Sudoku Solver

Last response: in Applications
Share
May 2, 2010 8:12:17 AM

Hey, to all the geeks on Tom's that feel comfortable in their c++ programming abilities:

I need help writing a program that will solve Sudoku puzzles either from resource files or from user input, it doesn't matter which. The program can be structured using functions or classes with private data members. It needs to have at least one class either way. I don't need a working program until May 10, but I need better than what I have now by tuesday.

Here are a couple different programs I am trying that don't work and don't really meet the project requirements:

  1. #include<stdio.h>
  2. #include<conio.h>
  3. #include<iostream>
  4. #include<stdlib.h>
  5. using namespace std;
  6.  
  7. int i,j,k,a[10][10],o,x[100],y[100];
  8.  
  9. void display();
  10. int getnum();
  11. void solve(int [],int [],int);
  12. int check(int ,int );
  13.  
  14. void main()
  15. {
  16. //clrscr();
  17. cout<<"\n\nEnter the elements of SUDOKU in rowwise.\n[ Enter '0' if element is absent. ]";
  18. for(i=1;i<=9;i++)
  19. for(j=1;j<=9;j++)
  20. scanf("%d",&a[i][j]);
  21. printf("\n\nEntered SUDOKU\n\n");
  22. display();
  23. printf("\nEnter any key for solution....\n");
  24. getch();
  25. o=getnum();
  26. solve(x,y,1);
  27. }
  28.  
  29. int getnum()
  30. {
  31. int c=0;
  32. for(i=1;i<=9;i++)
  33. {
  34. for(j=1;j<=9;j++)
  35. {
  36. if(a[i][j]==0)
  37. {
  38. c++;
  39. x[c]=i;
  40. y[c]=j;
  41. }
  42. }
  43. }
  44. return(c);
  45. }
  46.  
  47. void display()
  48. {
  49. for(i=1;i<=9;i++)
  50. {
  51. for(j=1;j<=9;j++)
  52. {
  53. if(a[i][j]!=0)
  54. printf(" %d",a[i][j]);
  55. else
  56. printf(" ");
  57. }
  58. printf("\n\n");
  59. }
  60. }
  61.  
  62.  
  63. void solve(int p[100],int q[100],int n)
  64. {
  65. for(k=1;k<=9;k++)
  66. for(i=p[n];i<=p[n];i++)
  67. for(j=q[n];j<=q[n];j++)
  68. {
  69. a[i][j]=k;
  70. if(n<0)
  71. solve(p,q,n++);
  72. int ch=check(1,0);
  73. if(ch!=0)
  74. {
  75. display();
  76. getch();
  77. exit(0);
  78. }
  79. }
  80.  
  81. int check(int n,int r)
  82. {
  83. int f=0,cont=0;
  84. if(r==1)
  85. {
  86. for(k=1;k<=9;k++)
  87. {
  88. for(i=n;i<=n;i++)
  89. for(j=1;j<=9;j++)
  90. {
  91. if(k==a[i][j])
  92. f++;
  93. }
  94. if(f!=1)
  95. return(0);
  96. else
  97. cont++;
  98. f=0;
  99. }
  100. if(cont!=9)
  101. return(0);
  102. else if(n==9)
  103. check(1,0);
  104. else
  105. check(n++,1);
  106. }
  107. else
  108. {
  109. for(k=1;k<=9;k++)
  110. {
  111. for(i=1;i<=9;i++)
  112. for(j=n;j<=n;j++)
  113. {
  114. if(k==a[i][j])
  115. f++;
  116. }
  117. if(f!=1)
  118. return(0);
  119. else
  120. cont++;
  121. f=0;
  122. }
  123. if(cont!=9)
  124. return(0);
  125. else if(n!=9)
  126. check(n++,1);
  127. }
  128. }


(Sorry, it's poorly formatted, I know)

And:

  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class CSudokuSolver
  5. {
  6. public :
  7. enum CELL_STATE { STATE_NOTSET=0, STATE_FIX, STATE_FIXTEMP, };
  8. private :
  9. char CellValue[9][9];
  10. int IsValueOkAt(int iValue,int atX,int atY) const;
  11. int FixCellAt(int ix, int iy);
  12. public :
  13. CSudokuSolver( );
  14. void SetValue(int ix,int iy, int iValue, CELL_STATE iState=STATE_FIX);
  15. int GetValue(int ix,int iy) const;
  16. CELL_STATE GetState(int ix,int iy) const;
  17. int Solve();
  18. static int Box3x3Start(int idx) { return 3*(idx/3); };
  19. };
  20.  
  21. int CSudokuSolver::FixCellAt(int ix,int iy)
  22. {
  23. if( GetState(ix,iy)==STATE_FIX ) { // do the next cell
  24. if( ix==8 && iy==8 ) return 1;// The last cell, done successfully
  25. if( ix<8 ) ix++; else { iy++; ix=0; }
  26. return FixCellAt(ix,iy);
  27. }
  28. else {
  29. int i,j,iv; // Try numbers from 1 to 9
  30. for(iv=1; iv<10; iv++) { //---- Reset the values to 0
  31. for(i=ix; i<9; i++) if( GetState(i,iy)!=STATE_FIX) SetValue(i,iy,0);
  32. for(i=iy+1; i<9; i++) {
  33. for(j=0; j<9; j++) if( GetState(j,i)!=STATE_FIX) SetValue(j,i,0);
  34. }
  35. if( IsValueOkAt(iv,ix,iy) ) {
  36. SetValue(ix,iy,iv,STATE_FIXTEMP);
  37. if( ix==8 && iy==8 ) return 1; // The last cell, done successfully
  38. i = ix;
  39. j = iy;
  40. if( i<8 ) i++; else { j++; i=0; }
  41. if( FixCellAt(i,j) ) return 1;
  42. }
  43. }
  44. return 0; // Cannot find a value. Must change the previous cell.
  45. }
  46. }


And one more, just for good measure:

  1. #include <cstdio>
  2. #include <iostream>
  3. #include <fstream>
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. using namespace std;
  9. ifstream getnum;
  10. int sudoku[9][9][10];
  11. int count1,count2,count3,count4,temp,test,trial;
  12. int remove[9][9];
  13. int ukodus[9][9][10];
  14. int guess[9][9];
  15.  
  16. /*Get Numbers*/
  17. getnum.open("filenumbers.txt");
  18. for(count1=0;count1<9;count1++)
  19. {
  20. for(count2=0;count2<9;count2++)
  21. {
  22. getnum>>sudoku[count1][count2][9];
  23. }
  24. }
  25.  
  26. /*Output Grid*/
  27. cout<<"You entered this grid: "<<endl;
  28.  
  29. for(count1=0;count1<9;count1++)
  30. {
  31. for(count2=0;count2<9;count2++)
  32. {
  33. cout<<sudoku[count1][count2][9]<<" ";
  34. }
  35. cout<<endl;
  36. }
  37.  
  38. /*Start Trial to eliminate after possibilites eliminated*/
  39. for(trial=0;trial<=20;trial++)
  40. {
  41.  
  42. /*Store Possibilities*/
  43. for(count1=0;count1<=8;count1++)
  44. {
  45. for(count2=0;count2<=8;count2++)
  46. {
  47. if(sudoku[count1][count2][9]==0)
  48. {
  49. for(count3=0;count3<=8;count3++)
  50. {
  51. sudoku[count1][count2][count3]=count3+1;
  52. }
  53. }
  54. else
  55. {
  56. for(count3=0;count3<=8;count3++)
  57. {
  58. sudoku[count1][count2][count3]=0;
  59. }
  60. }
  61. }
  62. }
  63.  
  64. /*eliminate by looking at rows and columns*/
  65. for(count1=0;count1<=8;count1++)
  66. {
  67. for(count2=0;count2<=8;count2++)
  68. {
  69. if(sudoku[count1][count2][9]!=0)
  70. {
  71. temp=sudoku[count1][count2][9];
  72. temp--;
  73. for(count3=0;count3<=8;count3++)
  74. {
  75. sudoku[count1][count3][temp]=0;
  76. sudoku[count3][count2][temp]=0;
  77. }
  78. }
  79. }
  80. }
  81. /*eliminate by looking at 3 by 3 squares (this part takes a while). Due to the fact that a 9x9 grid is made of
  82. 3x3 squares, a pattern between these grids was implemented in the for loops. However, each box of the 3x3 grids
  83. must be delt with seperately because of differing patterns.*/
  84. for(count1=0;count1<=8;count1++)
  85. {
  86. for(count2=0;count2<=8;count2++)
  87. {
  88. if(sudoku[count1][count2][9]!=0)
  89. {
  90. temp=sudoku[count1][count2][9];
  91. temp--;
  92.  
  93. if(count1%3==0)
  94. {
  95. if(count2%3==0)
  96. {
  97. sudoku[count1][count2+1][temp]=0;
  98. sudoku[count1][count2+2][temp]=0;
  99. sudoku[count1+1][count2][temp]=0;
  100. sudoku[count1+1][count2+1][temp]=0;
  101. sudoku[count1+1][count2+2][temp]=0;
  102. sudoku[count1+2][count2][temp]=0;
  103. sudoku[count1+2][count2+1][temp]=0;
  104. sudoku[count1+2][count2+2][temp]=0;
  105. }
  106. }
  107. if(count1%3==0)
  108. {
  109. if(count2%3==1)
  110. {
  111. sudoku[count1][count2+1][temp]=0;
  112. sudoku[count1][count2-1][temp]=0;
  113. sudoku[count1+1][count2][temp]=0;
  114. sudoku[count1+1][count2+1][temp]=0;
  115. sudoku[count1+1][count2-1][temp]=0;
  116. sudoku[count1+2][count2][temp]=0;
  117. sudoku[count1+2][count2+1][temp]=0;
  118. sudoku[count1+2][count2-1][temp]=0;
  119. }
  120. }
  121. if(count1%3==0)
  122. {
  123. if(count2%3==2)
  124. {
  125. sudoku[count1][count2-2][temp]=0;
  126. sudoku[count1][count2-1][temp]=0;
  127. sudoku[count1+1][count2][temp]=0;
  128. sudoku[count1+1][count2-1][temp]=0;
  129. sudoku[count1+1][count2-2][temp]=0;
  130. sudoku[count1+2][count2][temp]=0;
  131. sudoku[count1+2][count2-1][temp]=0;
  132. sudoku[count1+2][count2-2][temp]=0;
  133. }
  134. }
  135. if(count1%3==1)
  136. {
  137. if(count2%3==0)
  138. {
  139. sudoku[count1][count2+1][temp]=0;
  140. sudoku[count1][count2+2][temp]=0;
  141. sudoku[count1-1][count2][temp]=0;
  142. sudoku[count1-1][count2+1][temp]=0;
  143. sudoku[count1-1][count2+2][temp]=0;
  144. sudoku[count1+1][count2][temp]=0;
  145. sudoku[count1+1][count2+1][temp]=0;
  146. sudoku[count1+1][count2+2][temp]=0;
  147. }
  148. }
  149. if(count1%3==1)
  150. {
  151. if(count2%3==1)
  152. {
  153. sudoku[count1][count2+1][temp]=0;
  154. sudoku[count1][count2-1][temp]=0;
  155. sudoku[count1-1][count2][temp]=0;
  156. sudoku[count1-1][count2+1][temp]=0;
  157. sudoku[count1-1][count2-1][temp]=0;
  158. sudoku[count1+1][count2][temp]=0;
  159. sudoku[count1+1][count2+1][temp]=0;
  160. sudoku[count1+1][count2-1][temp]=0;
  161. }
  162. }
  163. if(count1%3==1)
  164. {
  165. if(count2%3==2)
  166. {
  167. sudoku[count1][count2-2][temp]=0;
  168. sudoku[count1][count2-1][temp]=0;
  169. sudoku[count1+1][count2][temp]=0;
  170. sudoku[count1+1][count2-1][temp]=0;
  171. sudoku[count1+1][count2-2][temp]=0;
  172. sudoku[count1-1][count2][temp]=0;
  173. sudoku[count1-1][count2-1][temp]=0;
  174. sudoku[count1-1][count2-2][temp]=0;
  175. }
  176. }
  177. if(count1%3==2)
  178. {
  179. if(count2%3==0)
  180. {
  181. sudoku[count1][count2+1][temp]=0;
  182. sudoku[count1][count2+2][temp]=0;
  183. sudoku[count1-1][count2][temp]=0;
  184. sudoku[count1-1][count2+1][temp]=0;
  185. sudoku[count1-1][count2+2][temp]=0;
  186. sudoku[count1-2][count2][temp]=0;
  187. sudoku[count1-2][count2+1][temp]=0;
  188. sudoku[count1-2][count2+2][temp]=0;
  189. }
  190. }
  191. if(count1%3==2)
  192. {
  193. if(count2%3==1)
  194. {
  195. sudoku[count1][count2-1][temp]=0;
  196. sudoku[count1][count2+1][temp]=0;
  197. sudoku[count1-1][count2][temp]=0;
  198. sudoku[count1-1][count2+1][temp]=0;
  199. sudoku[count1-1][count2-1][temp]=0;
  200. sudoku[count1-2][count2][temp]=0;
  201. sudoku[count1-2][count2+1][temp]=0;
  202. sudoku[count1-2][count2-1][temp]=0;
  203. }
  204. }
  205. if(count1%3==2)
  206. {
  207. if(count2%3==2)
  208. {
  209. sudoku[count1][count2-1][temp]=0;
  210. sudoku[count1][count2-2][temp]=0;
  211. sudoku[count1-1][count2][temp]=0;
  212. sudoku[count1-1][count2-1][temp]=0;
  213. sudoku[count1-1][count2-2][temp]=0;
  214. sudoku[count1-2][count2][temp]=0;
  215. sudoku[count1-2][count2-1][temp]=0;
  216. sudoku[count1-2][count2-2][temp]=0;
  217. }
  218. }
  219. }
  220. }
  221. }
  222. /*solving after possibilities are removed*/
  223.  
  224. for(count1=0;count1<=8;count1++)
  225. {
  226. for(count2=0;count2<=8;count2++)
  227. {
  228. remove[count1][count2]=0;
  229. }
  230. }
  231.  
  232. for(count1=0;count1<=8;count1++)
  233. {
  234. for(count2=0;count2<=8;count2++)
  235. {
  236. for(count3=0;count3<=8;count3++)
  237. {
  238. if(sudoku[count1][count2][count3]!=0)
  239. {
  240. remove[count1][count2]++;
  241. }
  242. }
  243. }
  244. }
  245.  
  246. for(count1=0;count1<=8;count1++)
  247. {
  248. for(count2=0;count2<=8;count2++)
  249. {
  250. if(remove[count1][count2]==1)
  251. {
  252. for(count3=0;count3<=8;count3++)
  253. {
  254. if(sudoku[count1][count2][count3]!=0)
  255. {
  256. sudoku[count1][count2][9]=sudoku[count1][count2][count3];
  257. }
  258. }
  259. }
  260. }
  261. }
  262.  
  263. }
  264. /*Store another array like this one for guessing*/
  265. for(count1=0;count1<=8;count1++)
  266. {
  267. for(count2=0;count2<=8;count2++)
  268. {
  269. for(count3=0;count3<=8;count3++)
  270. {
  271. ukodus[count1][count2][count3]=sudoku[count1][count2][count3];
  272. }
  273. }
  274. }
  275.  
  276.  
  277. /*Let the guessing begin!! Begin with numbers that only have two possibilities and pick one*/
  278. for(count1=0;count1<=8;count1++)
  279. {
  280. for(count2=0;count2<=8;count2++)
  281. {
  282. if(remove[count1][count2]==2)
  283. {
  284. for(count3=0;count3<=8;count3++)
  285. {
  286. if(ukodus[count1][count2][count3]!=0)
  287. {
  288. ukodus[count1][count2][9]=ukodus[count1][count2][count3];
  289. guess[count1][count2]=ukodus[count1][count2][count3];
  290. }
  291. }
  292. }
  293. }
  294. }
  295. /*if we guessed right, solve this and be done with it*/
  296. temp=1;
  297. for(count1=0;count1<=8;count1++)
  298. {
  299. for(count2=0;count2<=8;count2++)
  300. {
  301. if(udokus[count1][count2][9]==0)
  302. {
  303. temp=0;
  304. }
  305. }
  306. }
  307. if(temp==1)
  308. {
  309. for(count1=0;count1<=8;count1++)
  310. {
  311. for(count2=0;count2<=8;count2++)
  312. {
  313. for(count3=0;count3<=8;count3++)
  314. {
  315. sudoku[count1][count2][count3]=ukodus[count1][count2][count3];
  316. }
  317. }
  318. }
  319. }
  320. /*If we guessed wrong restore to original*/
  321. if(temp==0)
  322. {
  323. for(count1=0;count1<=8;count1++)
  324. {
  325. for(count2=0;count2<=8;count2++)
  326. {
  327. for(count3=0;count3<=8;count3++)
  328. {
  329. ukodus[count1][count2][count3]=sudoku[count1][count2][count3];
  330. }
  331. }
  332. }
  333. }
  334. /*guess another number*/
  335. if(temp==0)
  336. {
  337. for(count1=0;count1<=8;count1++)
  338. {
  339. for(count2=0;count2<=8;count2++)
  340. {
  341. for(count3=0;count3<=8;count3++)
  342. {
  343. ukodus[count1][count2][count3]=sudoku[count1][count2][count3];
  344. }
  345. }
  346. }
  347. }
  348.  
  349. /*break if finished*/
  350. /*if(temp==1)
  351. {
  352. break;
  353. }*/
  354.  
  355.  
  356.  
  357. /*Test Row d*/
  358. for(count2=0;count2<=8;count2++)
  359. {
  360. cout<<" Row: 4 Column: "<<count2+1<<endl;
  361. for(count3=0;count3<=9;count3++)
  362. {
  363. cout<<count3+1<<"="<<sudoku[3][count2][count3]<<",";
  364. }
  365. cout<<endl;
  366. }
  367. cout<<"Answer: "<<endl;
  368. /*Output final grid*/
  369. for(count1=0;count1<9;count1++)
  370. {
  371. for(count2=0;count2<9;count2++)
  372. {
  373. cout<<sudoku[count1][count2][9]<<" ";
  374. }
  375. cout<<endl;
  376. }
  377.  
  378. system("Pause");
  379. return 0;
  380. }


Please help me either get the one with the class working or incorporate a class into the other ones and get them working (they seem to have fewer errors, but no class equals zero points)

And, before you ask, yes - the second one was copied off of the internet. I am not going to use it, but if the others won't work, I'll try modifying it enough to-where it isn't plagiarism anymore.

Any help/suggestions would be greatly appreciated. :) 

Thanks, Kris

More about : sudoku solver

Best solution

a b L Programming
May 3, 2010 2:04:39 AM

I remember doing something like this is java a year or so ago in college, it was a pretty fun assignment.

What we did was use a recursive backtracking solution (aka a brute force solution). It was fairly simply to write once you understood what was going on, and didn't require any advanced knowledge of how to solve sudokus.

That second piece of code you posted seems to be using a backtracking solution.

The basics of solving a sudoku this was is:

1. start at the first empty cell, and put 1 in it.
2. Check the entire board, and see if there are any conflicts
3. If there are coflicts on the board, increase the number in the current cell by 1 (so change 1 to 2, 2 to 3, etc)
4. If the board is clean move, start at step one again.
5. If all nine possible numbers on a given cell cause a conflict in the board, then you set this cell back to empty, go back to the previous cell, and start again from step 3 (this is where the 'backtracking' comes in).

Once every cell have a number in it and there are no conflicts on the board, you have a solved sudoku.

I would recommend understanding this concept, and planning a way to solve it (in my opinion, recursion lends itself nicely to this solution). Once you have that, then start coding it, and post back if you run into any specific problems in your code (My forewarning - you should no that no one here is going to write your code for you, especially for school work)

Good luck :) 
Share
May 3, 2010 2:14:00 AM

All right, thank you for your input. How well is recursive backtracking implemented into a class? Is that fairly simple?
Score
0
Related resources
Can't find your answer ? Ask !
a b L Programming
May 3, 2010 2:31:16 AM

It is, you just need to make sure you understand how all this works.

What you have to remember is that when using recursion, it is naturally 'unwinding'. In this sudoku problem for example, if you get through all 9 numbers on a given cell, and then let the current recursive call finish without calling another one, the you will end up right where you left off at the previous function (aka where you made this recursive call from).

This is why recursion makes a very natural solution for backtracking problems. Because of the nature of recursion, it can pretty much take care of the actual 'backtracking' part of the solution for you.

The main code used to solve this isn't very long at all. Just insure you make good use of helper methods (such as checking if the board has any conflicts in it, etc). It may help you to write up the actually backtracking solution in pseudo code first, and then write up the actual code complete with all the helper methods.

Good luck :) 
Score
0
May 4, 2010 11:43:24 AM

Update:

Well, I tried many different variations on the second and third ones, and decided to go with the third one, seeing as how it's the only one I can get to work. I have not yet implemented a class, but I can do that. Thank you, kyeana, for your recommendations, but I couldn't get the other one working.

Btw, I am in an advanced college programming class with almost no programming experience, so now you may understand why I couldn't get a simple recursive program working. :)  Although I did not use your recommendation, I am selecting it as best answer because it was a good one, and because no one else even posted anything.
Score
0
May 4, 2010 11:43:53 AM

WTF??? The best answer button is not working, I tried it like 8 times, and refreshed the page and everything. I will try again later.
Score
0
a b L Programming
May 4, 2010 12:48:46 PM

Best answer selected by randomizer.
Score
0
a b L Programming
May 4, 2010 8:01:59 PM

Ha, no worries :)  If you do run in to any trouble you can always post back here for specific help.

Good luck
Score
0
a b L Programming
May 8, 2010 12:06:29 PM

THAT post should be a reference for all people that would like help for school assignments. Thanks Bruceification73 for giving me back some faith in future coworkers.
Score
0
May 9, 2010 12:39:51 AM

Zenthar said:
THAT post should be a reference for all people that would like help for school assignments. Thanks Bruceification73 for giving me back some faith in future coworkers.


Well, unfortunately I won't be working in Montreal any time soon, but thanks for your kind words. I am thinking of getting a summer job at a computer repair shop in town, but that's here in Colorado, U.S.
Score
0
October 23, 2011 1:25:08 PM

Check this out...



#include <iostream.h>
#include <conio.h>
int array[9][9];
int outputArray[9][9];
int input_value(int x, int y, int value)
{
int i,j;
for (i = 0; i < 9; i++)
{
if (value == outputArray[y] || value == outputArray[x])
{
return 0;
}
}
if (x < 3)
{
if (y < 3)
{
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
if (value == outputArray[j])
{
return 0;
}
}
}
}
else if (y < 6)
{
for (i = 0; i < 3; i++)
{
for (j = 3; j < 6; j++)
{
if (value == outputArray[j])
{
return 0;
}
}
}
}
else
{
for (i = 0; i < 3; i++)
{
for (j = 6; j < 9; j++)
{
if (value == outputArray[j])
{
return 0;
}
}
}
}
}
else if (x < 6)
{
if (y < 3)
{
for (i = 3; i < 6; i++)
{
for (j = 0; j < 3; j++)
{
if (value == outputArray[j])
{
return 0;
}
}
}
}
else if (y < 6)
{
for (i = 3; i < 6; i++)
{
for (j = 3; j < 6; j++)
{
if (value == outputArray[j])
{
return 0;
}
}
}
}
else
{
for (i = 3; i < 6; i++)
{
for (j = 6; j < 9; j++)
{
if (value == outputArray[j])
{
return 0;
}
}
}
}
}
else
{
if (y < 3)
{
for (i = 6; i < 9; i++)
{
for (j = 0; j < 3; j++)
{
if (value == outputArray[j])
{
return 0;
}
}
}
}
else if (y < 6)
{
for (i = 6; i < 9; i++)
{
for (j = 3; j < 6; j++)
{
if (value == outputArray[j])
{
return 0;
}
}
}
}
else
{
for (i = 6; i < 9; i++)
{
for (j = 6; j < 9; j++)
{
if (value == outputArray[j])
{
return 0;
}
}
}
}
}
return value;
}
int backtrack(int x, int y)
{
int temp,i,j;
if (outputArray[x][y] == 0)
{
for (i = 1; i < 10; i++)
{
temp = input_value(x,y,i);
if (temp > 0)
{
outputArray[x][y] = temp;
if (x == 8 && y == 8)
{
return 1;
}
else if (x == 8)
{
if (backtrack(0,y+1))
return 1;
} else {
if (backtrack(x+1,y)) return 1 ;
}
}
}
if (i == 10) {
if (outputArray[x][y] != array[x][y]) outputArray[x][y] = 0;
return 0;
}
} else {
if (x == 8 && y == 8) {
return 1;
} else if (x == 8) {
if (backtrack(0,y+1)) return 1;
} else {
if (backtrack(x+1,y)) return 1;
}
}
return 0;
}
void main()
{
clrscr();
int i,j;
cout<<"Original outputArray\n";
for (i = 0; i < 9; i++)
{
for (j = 0; j < 9; j++)
{
cin>>array[j];
}
}
clrscr();
cout<<"Puzzle is \n";
for (i = 0; i < 9; i++) {
for (j = 0; j < 9; j++) {
outputArray[j] = array[j];
}
}
for (i = 0; i < 9; i++) {
for (j = 0; j < 9; j++) {
cout<<"\t"<<array[j];

}
cout<<"\n";
}
if (backtrack(0,0)) {
cout<<"Soln is :\n";
for (i = 0; i < 9; i++) {
for (j = 0; j < 9; j++) {
cout<<"\t"<< outputArray[j];
}
cout<<"\n";
}
} else
cout<<"No Soln\n";
getch();
}
Score
0
October 23, 2011 1:32:04 PM

This topic has been closed by Area51reopened
Score
0
!