Advertisement
Guest User

Untitled

a guest
Jan 17th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.75 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <string.h>
  5.  
  6. //structure pour les candidats d'une case
  7. typedef struct {
  8. int nbc;
  9. int *tab;
  10. } Cand;
  11.  
  12. //structure avec
  13. typedef struct {
  14. int x;
  15. int y;
  16. } Case;
  17.  
  18.  
  19. Cand C[9][9];
  20. int G[9][9];
  21. Case O[81];
  22. int NBO;
  23. Case temp;
  24.  
  25. //entetes des fonctions
  26. void lireGrille(int grille[][9]);
  27. void ecrireGrille(int grille[][9]);
  28. int appartient(Case, int);
  29. int estCand(int);
  30. int memeRegion(int, int, int, int);
  31. int estCandUnique(Case, int);
  32. Case rechCaseUnique();
  33. void initJeu();
  34. void ecrireCand();
  35. void fermerCase();
  36. void fermerGrille();
  37.  
  38.  
  39.  
  40.  
  41.  
  42. int main() {
  43. lireGrille(G);
  44. ecrireGrille(G);
  45. initJeu();
  46. printf("\n\n\n\n");
  47. ecrireCand();
  48. fermerGrille();
  49. //fermerGrille();
  50. return 0;
  51. }
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59. void lireGrille(int grille[][9]) {
  60. char ligne[6];
  61. int i, j, n, a, b;
  62. //remplit G avec 0
  63. for (a = 0;a < 9;a++) {
  64. for (b = 0;b < 9;b++)
  65. G[a][b] = 0;
  66. }
  67. //ouvre le fichier grille
  68. FILE *f;
  69. f = fopen("grille.txt", "r");
  70. while(fgets(ligne,10,f)) { //lit le fichier et le stocke dans G
  71. sscanf(ligne, "%d %d %d", &i, &j, &n);
  72. grille[i][j] = n;
  73. }
  74. //ferme le fichier
  75. fclose(f);
  76. }
  77.  
  78.  
  79. void ecrireGrille(int grille[][9]) {
  80. printf("-------------------------------\n");//1ere ligne
  81. int a, b;
  82. for (a = 0;a < 9;a++) {
  83. printf("|");
  84. for (b = 0;b < 9;b++) {
  85. printf(" %d ", grille[a][b]); //affiche la valeur
  86. if (((b+1) % 3) == 0)
  87. printf("|");
  88. }
  89. printf("\n");
  90. if ((a == 2) || (a == 5))
  91. printf("|---------|---------|---------|\n");//lignes intermediaires
  92. }
  93. printf("-------------------------------\n");//derniere ligne
  94. }
  95.  
  96.  
  97. int appartient(Case cible, int n) {
  98. if (C[cible.x][cible.y].tab[n] == 1)//verifie si n est candidat de la case cible
  99. return 1;
  100. else
  101. return 0;
  102. }
  103.  
  104. int estCand(int n) {
  105. int a, b;
  106. for (a = 0;a < 9;a++) {
  107. if (G[a][temp.y] == n) {
  108. return 0;
  109. }
  110. }
  111. for (a = 0;a < 9;a++) {
  112. if (G[temp.x][a] == n) {
  113. return 0;
  114. }
  115. }
  116. for (a = 0;a < 9;a++) {
  117. for (b = 0;b < 9;b++) {
  118. if (memeRegion(temp.x,temp.y,a,b)) {
  119. if (G[a][b] == n) {
  120. return 0;
  121. }
  122. }
  123. }
  124. }
  125. //printf("sapas\n");
  126. return 1;
  127. }
  128.  
  129.  
  130. int memeRegion(int oA, int aA, int oB, int aB) {
  131. int regA, regB;
  132. regA = 0;
  133. regB = 0;
  134. if (aA < 3) {
  135. regA = 1;
  136. } else if(aA < 6) {
  137. regA = 2;
  138. } else {
  139. regA = 3;
  140. }
  141. if (oA > 5) {
  142. regA += 6;
  143. } else if (oA > 3) {
  144. regA += 3;
  145. }
  146.  
  147. if (aB < 3) {
  148. regB = 1;
  149. } else if(aB < 6) {
  150. regB = 2;
  151. } else {
  152. regB = 3;
  153. }
  154. if (oB > 5) {
  155. regB += 6;
  156. } else if (oB > 3) {
  157. regB += 3;
  158. }
  159. if (regA == regB)
  160. return 1;
  161. else
  162. return 0;
  163. }
  164.  
  165.  
  166. int estCandUnique(Case cible, int n) {
  167. int i, a, b, sl, ul, uc, ur;
  168. ul = 1;
  169. uc = 1;
  170. ur = 1;
  171. sl = 0;
  172. if ((C[cible.x][cible.y].nbc == 1) && (C[cible.x][cible.y].tab[n] == 1))
  173. sl = 1; //cas ou le candidat est le seul de sa case
  174. for (i = 0;i < 9;i++) {
  175. if ((C[cible.x][i].tab[n] == 1) && (cible.y != i))
  176. ul = 0; //cas ou le candidat n'est pas le seul de la ligne
  177. }
  178.  
  179. for (i = 0;i < 9;i++) {
  180. if ((C[i][cible.y].tab[n] == 1) && (cible.x != i))
  181. uc = 0;//cas ou le candidat n'est pas le seul de la colonne
  182. }
  183.  
  184. for (a = 0;a < 9;a++) {
  185. for (b = 0;b < 9;b++) {
  186. if (memeRegion(cible.x,cible.y,a,b))
  187. if (C[a][b].tab[n] == 1)
  188. ur = 0;//cas ou le candidat n'est pas le seul de la region
  189. }
  190. }
  191. if ((sl) || (!ul) || (!uc) || (!ur)) //1 seule des conditions est suffisante
  192. return 1;
  193. else
  194. return 0;
  195. }
  196.  
  197.  
  198. Case rechCaseUnique() {
  199. int i, j;
  200. for (i = 0;i < NBO;i++) { //parcourt les cases ouvertes
  201. //on copie O[i] dans temp
  202. temp.x = O[i].x;
  203. temp.y = O[i].y;
  204. printf("%d %d\n", temp.x, temp.y);
  205. for (j = 0;j < 9;j++) { //parcourt les candidats
  206. if (estCandUnique(temp, j)) //verifie si j est candidat unique de temp, donc de O[i]
  207. return temp;
  208. }
  209. }
  210. //valeurs de retour si aucune case ouverte n'admet de candidat unique
  211. temp.x = -1;
  212. temp.y = -1;
  213. return temp;
  214. }
  215.  
  216.  
  217. void initJeu() {
  218. NBO = 0;
  219. int a, b, c;
  220. for (a = 0;a < 9;a++) { //parcourt les lignes
  221. for (b = 0;b < 9;b++) { //parcourt les colonnes
  222. if (G[a][b] == 0) { //si la case est ouverte
  223. O[NBO].x = a;
  224. O[NBO].y = b;
  225. NBO ++;
  226. //on stocke ses coordonnees et on incremente NBO
  227. }
  228. }
  229. }
  230. //mise a zero de la table C
  231. for (a = 0;a < 9;a++) {
  232. for (b = 0;b < 9;b++) {
  233. C[a][b].nbc = 0;
  234. C[a][b].tab = malloc(9*sizeof(int));
  235. for (c = 0;c < 9;c++) {
  236. C[a][b].tab[c] = 0;
  237. }
  238. }
  239. }
  240. for (a = 0;a < NBO;a++) { //parcourt les cases ouvertes
  241. temp.x = O[a].x;
  242. temp.y = O[a].y;
  243. //stocke O[a] dans temp
  244. for (b = 1;b < 10;b++) { //parcourt les candidats
  245. if (estCand(b)) {
  246. C[O[a].x][O[a].y].tab[b] = 1;
  247. C[O[a].x][O[a].y].nbc += 1;
  248. }
  249. }
  250. }
  251. }
  252.  
  253.  
  254. void ecrireCand() {
  255. int a, b, n, i;
  256. for (i = 0;i < 64;i++) {
  257. printf("*");
  258. }
  259. printf("\n");
  260. for (a = 0;a < 9;a++) {
  261. printf("*");
  262. for (b = 0;b < 9;b++) {
  263. if (C[a][b].nbc != 0) {
  264. if (C[a][b].nbc < 6){
  265. for (i = 0;i < 6-C[a][b].nbc;i++) {
  266. printf(" ");
  267. }
  268. }
  269. for (n = 1;n < 10;n++) {
  270. if (C[a][b].tab[n]) {
  271. printf("%d", n);
  272. }
  273. }
  274. }
  275. else
  276. printf(" ");
  277. switch (b) {
  278. case 2: case 5: case 8: printf("*");
  279. break;
  280. default: printf("|");
  281. break;
  282. }
  283. }
  284. printf("\n");
  285. switch (a) {
  286. case 2: case 5: case 8: {
  287. for (i = 0;i < 64;i++) {
  288. printf("*");
  289. }
  290. printf("\n");
  291. }
  292.  
  293. break;
  294. default: printf("*--------------------*--------------------*--------------------*\n");
  295. }
  296. }
  297. }
  298.  
  299.  
  300. void fermerGrille() {
  301. int a;
  302. while(NBO!=0){
  303. fermerCase();
  304. printf("Il reste %d case(s) ouverte(s)\n", NBO);
  305. ecrireGrille(G);
  306. ecrireCand();
  307. }
  308.  
  309. }
  310.  
  311.  
  312. void fermerCase() {
  313. int i, j, a ,z,k,l,w;
  314. temp = rechCaseUnique();
  315. for (i = 1;i < 10;i++) {
  316. if (estCandUnique(temp, i)) {
  317. G[temp.x][temp.y] = i;
  318. break;
  319. }
  320. }
  321. for (j = 0;j <=81;j++) {
  322. if ((O[j].x == temp.x) && (O[j].y == temp.y)) {
  323. for (a = j;a < NBO;a++) {
  324. O[a].x = O[a+1].x;
  325. O[a].y = O[a+1].y;
  326. }
  327. --NBO;
  328. }
  329. }
  330. for (z=0 ; z<=81 ; z++){
  331. if (appartient(O[z],i)==1)
  332. {
  333. for (w=0 ; w<9; w++){
  334. C[O[z].x][O[z].y].tab[w]=C[O[z].x][O[z].y].tab[w+1];
  335. }
  336. }
  337. }
  338. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement