fork download
  1. /* ************************************************************************** */
  2. /* */
  3. /* ::: :::::::: */
  4. /* sudoku.c :+: :+: :+: */
  5. /* +:+ +:+ +:+ */
  6. /* By: jyeo <marvin@42.fr> +#+ +:+ +#+ */
  7. /* +#+#+#+#+#+ +#+ */
  8. /* Created: 2017/08/12 16:18:46 by jyeo #+# #+# */
  9. /* Updated: 2017/08/12 18:43:01 by mhernand ### ########.fr */
  10. /* */
  11. /* ************************************************************************** */
  12.  
  13. /******************************************************************************/
  14. #include <unistd.h>
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17.  
  18. int ft_solver(char **argv, char **copy, int row, int col);
  19. int ft_fill_number(char **argv, char **copy, int row, int col);
  20.  
  21. int ft_putchar(char c)
  22. {
  23. write(1, &c, 1);
  24. return (0);
  25. }
  26.  
  27. void ft_print_sudoku(int argc, char **argv)
  28. {
  29. int i;
  30. int j;
  31.  
  32. i = 1;
  33. j = 0;
  34. while (i < argc)
  35. {
  36. while (argv[i][j])
  37. {
  38. ft_putchar(argv[i][j]);
  39. ft_putchar(' ');
  40. j++;
  41. }
  42. ft_putchar('\n');
  43. j = 0;
  44. i++;
  45. }
  46. }
  47.  
  48. int ft_check_number(char *str)
  49. {
  50. int i;
  51.  
  52. i = 0;
  53. while (str[i] != '\0')
  54. {
  55. if((str[i] < '0' || str[i] > '9') && (str[i] != '.'))
  56. return (0);
  57. i++;
  58. }
  59. if (i != 9)
  60. return(0);
  61. return (1);
  62. }
  63.  
  64. /******************************************************************************/
  65. char **ft_copy(char **args)
  66. {
  67. int i;
  68. int j;
  69. char **board;
  70.  
  71. i = 0;
  72. j = 0;
  73. board = malloc(sizeof(char*) * 10);
  74. while (i < 10)
  75. {
  76. board[i] = malloc(sizeof(char) * 9);
  77. while (j < 9)
  78. {
  79. board[i][j] = args[i][j];
  80. j++;
  81. }
  82. i++;
  83. j = 0;
  84. }
  85. return (board);
  86. }
  87.  
  88. void free_copy(int **sud)
  89. {
  90. int i;
  91. int j;
  92.  
  93. i = 0;
  94. j = 0;
  95. while (i < 9)
  96. {
  97. free(sud[i]);
  98. sud[i] = NULL;
  99. i++;
  100. }
  101. free(sud);
  102. sud = NULL;
  103. }
  104.  
  105. void free_argv(char **sud)
  106. {
  107. int i;
  108. int j;
  109.  
  110. i = 0;
  111. j = 0;
  112. while (i < 10)
  113. {
  114. free(sud[i]);
  115. //sud[i] = NULL;
  116. i++;
  117. }
  118. free(sud);
  119. //sud = NULL;
  120. }
  121.  
  122. //char ft_strcpy(char **dest, char **src, int size)
  123. //{
  124. // int i;
  125. // int j;
  126. //
  127. // i = 0;
  128. // j = 0;
  129. //
  130. // while (i < size)
  131. // {
  132. // while (src[i][j] != '\0')
  133. // {
  134. // dest[i][j] = src[i][j];
  135. // j++;
  136. // }
  137. // dest[i][j] = '\0';
  138. // i++;
  139. // }
  140. // return (**dest);
  141. //}
  142. /******************************************************************************/
  143. //int is_used(int a, int b)
  144. //{
  145. // if (a == b)
  146. // return (0);
  147. // else
  148. // return (1);
  149. //}
  150. //
  151. //int *ft_fill_row(char *str)
  152. //{
  153. // int i;
  154. // int j;
  155. // int k;
  156. //
  157. // i = 0;
  158. // while (str[i] != '\0')
  159. // {
  160. // if(str[i] == '.')
  161. // {
  162. // j = 1;
  163. // k = 0;
  164. // while (j < 10)
  165. // {
  166. // if(is_used(str[k], j) == 0)
  167. // {
  168. // j
  169. // }
  170. // }
  171. // }
  172. // i++;
  173. // }
  174. //}
  175. /******************************************************************************/
  176.  
  177. int ft_row_col(char **copy, int row, int col, int nb)
  178. {
  179. int i;
  180.  
  181. i = 0;
  182. while (i < 9)
  183. {
  184. if (copy[row][i] == (nb + 48))
  185. return (0);
  186. if (copy[i][col] == (nb + 48))
  187. return (0);
  188. i++;
  189. }
  190. return (1);
  191. }
  192.  
  193. /******************************************************************************/
  194.  
  195. int ft_grid(char **copy, int row, int col, int nb)
  196. {
  197. int sr;
  198. int sc;
  199.  
  200. sr = ((row / 3) * 3) + 1;
  201. while (sr < ((row / 3) * 3 + 4))
  202. {
  203. sc = (col / 3) * 3;
  204. while (sc < ((col / 3) * 3 + 3))
  205. {
  206. if (copy[sr][sc] == (nb + 48))
  207. return (0);
  208. sc++;
  209. }
  210. sr++;
  211. }
  212. return (1);
  213. }
  214. /******************************************************************************/
  215.  
  216. int ft_backtrack(char **argv, char **copy, int row, int col)
  217. {
  218. int nb;
  219.  
  220. nb = (copy[row][col] - 48);
  221. printf("code stop at argv row : %d col : %d\n", row, col);
  222. printf("code stop at argv : %d copy : %d nb : %d\n\n", argv[row][col] - 48, copy[row][col] -48, nb);
  223. while (nb < 10)
  224. {
  225.  
  226. if (ft_row_col(copy, row, col, nb) && ft_grid(copy, row, col, nb)
  227. && copy[row][col] != nb)
  228. {
  229. //printf("code stop at argv : %d copy : %d \n", argv[row][col], copy[row][col]);
  230. copy[row][col] = nb + 48;
  231. //free_argv(copy);
  232. //copy = NULL;
  233. return ft_fill_number(argv, copy, row , col +1);
  234. }
  235. nb++;
  236. }
  237. if (nb == 10)
  238. {
  239. copy[row][col] = '.';
  240. //free(copy);
  241. //printf("code stop at argv row : %d col : %d\n", row, col);
  242. //printf("code stop at argv : %d copy : %d nb : %d\n\n", argv[row][col] - 48, copy[row][col] -48, nb);
  243. if(col == 0)
  244. {
  245. ft_backtrack(argv, copy, row - 1, col +8);
  246. }
  247. else
  248. {
  249. ft_backtrack(argv, copy, row , col - 1);
  250. }
  251. }
  252. return (0);
  253. }
  254. /******************************************************************************/
  255. int ft_fill_number(char **argv, char **copy, int row, int col)
  256. {
  257. int nb;
  258.  
  259. nb = 1;
  260. while (nb < 10)
  261. {
  262. //printf("code stop at argv : %d copy : %d nb : %d\n", argv[row][col], copy[row][col], nb);
  263. if (ft_row_col(copy, row, col, nb) && ft_grid(copy, row, col, nb)
  264. && argv[row][col] != nb)
  265. {
  266. //printf("code stop at argv : %d copy : %d \n", argv[row][col], copy[row][col]);
  267. copy[row][col] = nb + 48;
  268. return ft_solver(argv, copy, row , col +1);
  269. }
  270. nb++;
  271. }
  272. if (nb == 10)
  273. {
  274. if(col == 0)
  275. {
  276. ft_backtrack(argv, copy, row - 1, col +8);
  277. }
  278. else
  279. {
  280. ft_backtrack(argv, copy, row , col - 1);
  281. }
  282. }
  283. return (0);
  284. }
  285.  
  286. /******************************************************************************/
  287. int ft_solver(char **argv, char **copy, int row, int col)
  288. {
  289.  
  290. if (row < 10 && col < 10)
  291. {
  292. if (copy[row][col] != '.')
  293. {
  294. if ((col + 1) < 10)
  295. {
  296. return ft_solver(argv, copy, row, col + 1);
  297. }
  298. else if ((row + 1) < 11)
  299. {
  300. return ft_solver(argv, copy, row + 1 , 0);
  301. }
  302. }
  303. else
  304. {
  305. return ft_fill_number(argv, copy, row, col);
  306. }
  307. }
  308. printf("code stop at row : %d col : %d \n", row, col);
  309. return (1);
  310. return (**copy);
  311. }
  312. /******************************************************************************/
  313. int main(int argc, char **argv)
  314. {
  315. int i;
  316. char **copy;
  317.  
  318. i = 1;
  319. if(argc != 10)
  320. {
  321. return (0);
  322. }
  323. else
  324. {
  325. while (i < argc)
  326. {
  327. if(ft_check_number(argv[i]) == 0)
  328. {
  329. //printf("%s i'm the problem \n", argv[i]);
  330. //printf("i'm inside here");
  331. return (0);
  332. }
  333. i++;
  334. }
  335. }
  336. copy = ft_copy(argv);
  337. **argv = ft_solver(argv, copy, 1, 0);
  338. ft_print_sudoku(argc, copy);
  339. return (0);
  340. }
  341.  
Success #stdin #stdout 0s 9296KB
stdin
"9...7...." "2...9..53" ".6..124.." "84...1.9." "5.....8.." ".31..4..." "..37..68." ".9..5.741" "47......."
stdout
Standard output is empty