fork(1) download
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5.  
  6. void printArray (int arr[][9], int rowSize, int columnSize)
  7. {
  8.  
  9.  
  10. for (int i = 0; i < rowSize; i++)
  11. {
  12. for (int j = 0; j < columnSize; j++)
  13. {
  14. cout << arr[i][j] <<" ";
  15. }
  16. cout<<endl;
  17. }
  18.  
  19. }
  20.  
  21. void convertStringToInt (string line,int arr[][9], int rowSize, int whichRow )
  22.  
  23. {
  24. int step = 0;
  25. for(int i=0; i< line.length(); i++)
  26. {
  27. if ( !isspace(line[i]) )
  28. {
  29. arr[whichRow ][step] = line[i]-48;
  30. //cout << arr[whichRow][step]<<" ";
  31. step++;
  32. }
  33.  
  34. }
  35. }
  36.  
  37. bool compareDigitsInLine (int arr[][9], int rowSize, int columnSize,int startAtRow, int startAtColumn)
  38.  
  39. {
  40. bool isDifferent = true;
  41.  
  42. for(int i=0; i < rowSize; i++)
  43.  
  44. {
  45. for(int j=0; j < columnSize-1; j++)
  46. {
  47.  
  48. for (int k = 1; k < columnSize - j; k++)
  49. {
  50. int rowIndex = startAtRow+i;
  51. int colIndex = startAtColumn+j;
  52. if ( arr[rowIndex][colIndex]==arr[rowIndex][colIndex+k] || arr[rowIndex ][colIndex] < 1 || arr[rowIndex ][colIndex] > 9 )
  53. {
  54. isDifferent = false;
  55. return isDifferent;
  56. }
  57. }
  58.  
  59.  
  60. }
  61.  
  62. }
  63.  
  64. return isDifferent;
  65. }
  66.  
  67. bool compareDigitsInSmallSquares(int arr[][9], int rowSize, int columnSize,int startAtRow, int startAtColumn)
  68.  
  69. {
  70. bool isDifferent = true;
  71. int multiplier = rowSize/3;
  72. int jumpRow =0;
  73. int jumpCol = 0;
  74. int colStartPos = 0;
  75. int rowStartPos = 0;
  76. int rowIndx = 0;
  77. int colIndx = 0;
  78. int rowRootIndx =0;
  79. int colRootIndx =0;
  80. int k=0, m=0;
  81.  
  82. while (jumpCol < multiplier)
  83. {
  84. while (jumpRow < multiplier )
  85. {
  86. for (int i = 0; i<rowSize/multiplier; i++ )
  87. {
  88. for (int j = 0; j <columnSize/multiplier; j++)
  89. {
  90.  
  91. for ( k = 0; k < rowSize/multiplier - rowStartPos; k++)
  92. {
  93. if (i==0 && j==0 && k==0)
  94. {
  95. rowStartPos = 0;
  96. colStartPos = 1;
  97. }
  98. if (i==0 && j ==0 && k>0)
  99. {
  100. rowStartPos = 0;
  101. colStartPos = 0;
  102. }
  103. if (i==0 && j==1 && k==0)
  104. {
  105. rowStartPos = 0;
  106. colStartPos = 2;
  107. }
  108. if (i==0 && j==1 && k>0)
  109. {
  110. rowStartPos = 0;
  111. colStartPos = 0;
  112. }
  113. if (i==0 && j==2 && k==0)
  114. {
  115. rowStartPos = 0;
  116. colStartPos = 3;
  117. }
  118. if (i==0 && j==2 && k>0)
  119. {
  120. rowStartPos = 0;
  121. colStartPos = 0;
  122. }
  123. if (i==1 && j==0 && k==0)
  124. {
  125. rowStartPos = 1;
  126. colStartPos = 1;
  127. }
  128.  
  129. if (i==1 && j==0 && k>0)
  130. {
  131. rowStartPos = 1;
  132. colStartPos = 0;
  133. }
  134. if (i==1 && j==1 && k==0)
  135. {
  136. rowStartPos = 1;
  137. colStartPos = 2;
  138. }
  139. if (i==1 && j==1 && k>0)
  140. {
  141. rowStartPos = 1;
  142. colStartPos = 0;
  143. }
  144. if (i==1 && j==2 && k==0)
  145. {
  146. rowStartPos = 1;//bez znaczenia
  147. colStartPos = 3;
  148. }
  149. if (i==1 && j==2 && k>0)
  150. {
  151. rowStartPos = 1;
  152. colStartPos = 0;
  153. }
  154. if (i==2 && j==0 && k==0)
  155. {
  156. rowStartPos = 2;
  157. colStartPos = 1;
  158. }
  159. if (i==2 && j==1 && k==0)
  160. {
  161. rowStartPos = 2;
  162. colStartPos = 2;
  163. }
  164. if (i==2 && j==2 && k==0)
  165. {
  166. rowStartPos = 2;
  167. colStartPos = 3;
  168. }
  169.  
  170.  
  171. for ( m = 0; m < columnSize/multiplier - colStartPos; m++)
  172. {
  173.  
  174. rowIndx = k + rowStartPos + jumpRow*multiplier;
  175. colIndx = m + colStartPos + jumpCol*multiplier;
  176. rowRootIndx = i + jumpRow*multiplier;
  177. colRootIndx = j + jumpCol*multiplier;
  178.  
  179. if ( arr[rowRootIndx][colRootIndx]==arr[rowIndx][colIndx] || arr[rowRootIndx][colRootIndx] < 1 || arr[rowRootIndx][colRootIndx] > 9 )
  180. {
  181. isDifferent = false;
  182. return isDifferent;
  183. }
  184.  
  185. }
  186. }
  187.  
  188. }
  189. }
  190.  
  191. jumpRow ++;
  192. }
  193. jumpCol++;
  194. if ( jumpRow == multiplier )
  195. jumpRow=0;
  196. }
  197.  
  198.  
  199.  
  200. return isDifferent;
  201. }
  202.  
  203.  
  204. bool checkRowColumnSum (int arr[][9], int rowSize, int columnSize, int criteriaForTrue)
  205.  
  206. {
  207. bool verdict = true;
  208. int sum = 0;
  209.  
  210. int i=0,goByRow=0, goByCol = 0;
  211.  
  212. for ( i = 0; i < rowSize; i++)
  213. {
  214. if (verdict == true)
  215. {
  216. for ( goByCol = 0; goByCol < columnSize; goByCol++)
  217. {
  218. sum+= arr[i][goByCol];
  219. }
  220.  
  221. if (sum != criteriaForTrue)
  222. {
  223. verdict = false;
  224. return verdict;
  225. }
  226.  
  227. sum = 0;
  228.  
  229. for ( goByRow = 0; goByRow < rowSize; goByRow++)
  230. {
  231. sum+= arr[goByRow][i];
  232. }
  233. if (sum != criteriaForTrue)
  234. {
  235.  
  236. verdict = false;
  237. return verdict;
  238.  
  239. }
  240.  
  241. if (sum != criteriaForTrue)
  242. {
  243. verdict = false;
  244. return verdict;
  245. }
  246.  
  247. sum = 0;
  248. }
  249. else if (verdict == false)
  250. return verdict;
  251.  
  252. }
  253. return verdict;
  254. }
  255.  
  256. void checkSudoku (int arr[][9], int rowSize, int columnSize,int initialPositionForRow, int initialPositionForColumn, int totalSumLimit)
  257.  
  258. {
  259. bool isDifferent = true;
  260. bool isCorrect = true;
  261. bool isDifferentInSmallSquareCorrect = true;
  262.  
  263. isDifferentInSmallSquareCorrect = compareDigitsInSmallSquares(arr, rowSize, columnSize,initialPositionForRow,initialPositionForColumn);
  264. isDifferent = compareDigitsInLine(arr, rowSize, columnSize,initialPositionForRow,initialPositionForColumn);
  265. isCorrect = checkRowColumnSum(arr,rowSize,columnSize, totalSumLimit);
  266.  
  267.  
  268. if (isDifferent == true && isCorrect == true && isDifferentInSmallSquareCorrect == true )
  269. {
  270. cout<<"TAK"<<endl;
  271. }
  272. else
  273. {
  274. cout<<"NIE"<<endl;
  275. }
  276.  
  277. }
  278.  
  279.  
  280.  
  281.  
  282.  
  283. int main()
  284. {
  285. int rowSize = 9;
  286. int columnSize = 9;
  287. int totalSum = 45; // Suma w linii od 1 do 9 zawsze wynosi 45 - kryterium prawdy ulozenia liczb w Sudoku
  288.  
  289.  
  290. int arr1 [9][9]= { 2, 5, 1, 7, 6, 9, 3, 4, 8,
  291. 9, 8, 6, 3, 4, 5, 2, 7, 1,
  292. 3, 7, 4, 8, 2, 1, 6, 9, 5,
  293. 4, 2, 9, 6, 3, 8, 5, 1, 7,
  294. 8, 6, 3, 5, 1, 7, 9, 2, 4,
  295. 5, 1, 7, 4, 9, 2, 8, 3, 6,
  296. 7, 9, 5, 1, 8, 3, 4, 6, 2,
  297. 1, 4, 2, 9, 5, 6, 7, 8, 3,
  298. 6, 3, 8, 2, 7, 4, 1, 5, 9
  299. };
  300.  
  301. int arr2 [9][9]= { 100, 5, 1, 7, 6, 9, 3, 4, 8,
  302. 9, 8, 6, 3, 4, 5, 2, 7, 1,
  303. 3, 7, 4, 8, 2, 1, 6, 9, 5,
  304. 4, 2, 9, 6, 3, 8, 5, 1, 7,
  305. 8, 6, 3, 5, 1, 7, 9, 2, 4,
  306. 5, 1, 7, 4, 9, 2, 8, 3, 6,
  307. 7, 9, 5, 1, 8, 3, 4, 6, 2,
  308. 1, 4, 2, 9, 5, 6, 7, 8, 3,
  309. 6, 3, 8, 2, 7, 4, 1, 5, 9
  310. };
  311.  
  312. int arr3 [9][9]= { 4, 2, 9, 6, 3, 8, 5, 1, 7,
  313. 8, 6, 3, 5, 1, 7, 9, 2, 4,
  314. 5, 1, 7, 4, 9, 2, 8, 3, 6,
  315. 7, 9, 5, 1, 8, 3, 4, 6, 2,
  316. 1, 4, 2, 9, 5, 6, 7, 8, 3,
  317. 6, 3, 8, 2, 7, 4, 1, 5, 9,
  318. 2, 5, 1, 7, 6, 9, 3, 4, 8,
  319. 9, 8, 6, 3, 4, 5, 2, 7, 1,
  320. 3, 7, 4, 8, 2, 1, 6, 9, 5
  321. };
  322.  
  323. int arr4 [9][9]= { 2, 5, 1, 7, 6, 9, 3, 4, 8,
  324. 9, 8, 6, 3, 4, 5, 2, 7, 1,
  325. 3, 7, 4, 8, 2, 1, 6, 9, 5,
  326. 4, 2, 9, 6, 3, 8, 5, 1, 7,
  327. 8, 6, 3, 5, 1, 7, 9, 2, 4,
  328. 5, 1, 7, 4, 9, 2, 8, 3, 6,
  329. 7, 9, 5, 1, 8, 3, 4, 6, 2,
  330. 1, 4, 2, 9, 5, 6, 7, 8, 3,
  331. 6, 3, 8, 2, 7, 4, 1, 9, 5,
  332. };
  333.  
  334. int arr5 [9][9]= { 2, 5, 1, 7, 6, 9, 3, 4, 8,
  335. 9, 8, 6, 3, 4, 5, 2, 7, 1,
  336. 3, 7, 4, 8, 2, 1, 6, 9, 5,
  337. 4, 2, 9, 6, 3, 8, 5, 1, 7,
  338. 8, 6, 3, 5, 1, 7, 9, 2, 4,
  339. 5, 1, 7, 4, 9, 2, 8, 3, 6,
  340. 7, 9, 5, 1, 8, 3, 4, 6, 2,
  341. 1, 4, 2, 9, 8, 6, 7, 5, 3,
  342. 6, 3, 8, 2, 7, 4, 1, 5, 9,
  343. };
  344.  
  345. int arr6 [9][9]= { 2, 5, 0, 7, 6, 10, 3, 4, 8,
  346. 10, 8, 6, 3, 4, 5, 2, 7, 0,
  347. 3, 7, 4, 8, 2, 0, 6, 10, 5,
  348. 4, 2, 10, 6, 3, 8, 5, 0, 7,
  349. 8, 6, 3, 5, 0, 7, 10, 2, 4,
  350. 5, 0, 7, 4, 10, 2, 8, 3, 6,
  351. 7, 10, 5, 0, 8, 3, 4, 6, 2,
  352. 0, 4, 2, 10, 5, 6, 7, 8, 3,
  353. 6, 3, 8, 2, 7, 4, 0, 5, 10,
  354. };
  355.  
  356.  
  357. int arr7 [9][9]= { 8, 1, 6, 8, 1, 6, 8, 1, 6,
  358. 3, 5, 7, 3, 5, 7, 3, 5, 7,
  359. 4, 9, 2, 4, 9, 2, 4, 9, 2,
  360. 8, 1, 6, 8, 1, 6, 8, 1, 6,
  361. 3, 5, 7, 3, 5, 7, 3, 5, 7,
  362. 4, 9, 2, 4, 9, 2, 4, 9, 2,
  363. 8, 1, 6, 8, 1, 6, 8, 1, 6,
  364. 3, 5, 7, 3, 5, 7, 3, 5, 7,
  365. 4, 9, 2, 4, 9, 2, 4, 9, 2,
  366. };
  367.  
  368.  
  369.  
  370. /*printArray(arr1, rowSize,columnSize);
  371.   printArray(arr2, rowSize,columnSize);
  372.   printArray(arr3, rowSize,columnSize);
  373.   printArray(arr3, rowSize,columnSize);
  374.   printArray(arr5, rowSize,columnSize);
  375.   printArray(arr6, rowSize,columnSize);
  376.   printArray(arr7, rowSize,columnSize);*/
  377.  
  378. checkSudoku(arr1, rowSize, columnSize,0,0,totalSum);
  379. checkSudoku(arr2, rowSize, columnSize,0,0,totalSum);
  380. checkSudoku(arr3, rowSize, columnSize,0,0,totalSum);
  381. checkSudoku(arr4, rowSize, columnSize,0,0,totalSum);
  382. checkSudoku(arr5, rowSize, columnSize,0,0,totalSum);
  383. //checkSudoku(arr6, rowSize, columnSize,0,0,totalSum);
  384. //checkSudoku(arr7, rowSize, columnSize,0,0,totalSum);
  385.  
  386.  
  387. return 0;
  388. }
  389.  
Success #stdin #stdout 0s 4416KB
stdin
Standard input is empty
stdout
TAK
NIE
TAK
NIE
NIE