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

100 5 1 7 6 9 3 4 8
9 8 6 3 4 5 2 7 1
3 7 4 8 2 1 6 9 5
4 2 9 6 3 8 5 1 7
8 6 3 5 1 7 9 2 4
5 1 7 4 9 2 8 3 6
7 9 5 1 8 3 4 6 2
1 4 2 9 5 6 7 8 3
6 3 8 2 7 4 1 5 9

4 2 9 6 3 8 5 1 7
8 6 3 5 1 7 9 2 4
5 1 7 4 9 2 8 3 6
7 9 5 1 8 3 4 6 2
1 4 2 9 5 6 7 8 3
6 3 8 2 7 4 1 5 9
2 5 1 7 6 9 3 4 8
9 8 6 3 4 5 2 7 1
3 7 4 8 2 1 6 9 5

2 5 1 7 6 9 3 4 8
9 8 6 3 4 5 2 7 1
3 7 4 8 2 1 6 9 5
4 2 9 6 3 8 5 1 7
8 6 3 5 1 7 9 2 4
5 1 7 4 9 2 8 3 6
7 9 5 1 8 3 4 6 2
1 4 2 9 5 6 7 8 3
6 3 8 2 7 4 1 9 5

2 5 1 7 6 9 3 4 8
9 8 6 3 4 5 2 7 1
3 7 4 8 2 1 6 9 5
4 2 9 6 3 8 5 1 7
8 6 3 5 1 7 9 2 4
5 1 7 4 9 2 8 3 6
7 9 5 1 8 3 4 6 2
1 4 2 9 8 6 7 5 3
6 3 8 2 7 4 1 5 9
stdout
TAK
NIE
NIE
NIE
NIE