Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Tentativa de solucionar sudoime através da verificação de linhas, dep…

…ois colunas e depois blocos 3x3. Não funcionou. Devo ter esquecido de algum detalhe sobre as regras dos jogo
  • Loading branch information...
commit 6332b8b1ca367c884be0381500b532709e3944f7 1 parent 792e107
Philip Sampaio authored

Showing 1 changed file with 86 additions and 6 deletions. Show diff stats Hide diff stats

  1. +86 6 sudoime/sudoime.c
92 sudoime/sudoime.c
... ... @@ -1,30 +1,77 @@
1 1 #include <stdio.h>
2 2
  3 +/* Problem SUDOIME https://br.spoj.pl/problems/SUDOIME/ */
  4 +
3 5 int solved_game(int[9][9]);
4 6 int * reset_stretch(int[9][2]);
  7 +int check_rows(int[9][9],int[9][2],int);
  8 +int check_columns(int[9][9],int[9][2],int);
  9 +int check_block(int[9][2]);
  10 +
5 11 int main(int argc, const char *argv[])
6 12 {
7 13 int s[9][9];
8 14 int instances = 0;
  15 + int i = 0;
9 16
10 17 scanf("%d",&instances);
11   -
  18 + if ( instances <= 0 )
  19 + return 0;
12 20 while ( instances ) {
  21 + i++;
13 22 scanf("%d %d %d %d %d %d %d %d %d\n%d %d %d %d %d %d %d %d %d\n%d %d %d %d %d %d %d %d %d\n%d %d %d %d %d %d %d %d %d\n%d %d %d %d %d %d %d %d %d\n%d %d %d %d %d %d %d %d %d\n%d %d %d %d %d %d %d %d %d\n%d %d %d %d %d %d %d %d %d\n%d %d %d %d %d %d %d %d %d",&s[0][0],&s[0][1],&s[0][2],&s[0][3],&s[0][4],&s[0][5],&s[0][6],&s[0][7],&s[0][8],&s[1][0],&s[1][1],&s[1][2],&s[1][3],&s[1][4],&s[1][5],&s[1][6],&s[1][7],&s[1][8],&s[2][0],&s[2][1],&s[2][2],&s[2][3],&s[2][4],&s[2][5],&s[2][6],&s[2][7],&s[2][8],&s[3][0],&s[3][1],&s[3][2],&s[3][3],&s[3][4],&s[3][5],&s[3][6],&s[3][7],&s[3][8],&s[4][0],&s[4][1],&s[4][2],&s[4][3],&s[4][4],&s[4][5],&s[4][6],&s[4][7],&s[4][8],&s[5][0],&s[5][1],&s[5][2],&s[5][3],&s[5][4],&s[5][5],&s[5][6],&s[5][7],&s[5][8],&s[6][0],&s[6][1],&s[6][2],&s[6][3],&s[6][4],&s[6][5],&s[6][6],&s[6][7],&s[6][8],&s[7][0],&s[7][1],&s[7][2],&s[7][3],&s[7][4],&s[7][5],&s[7][6],&s[7][7],&s[7][8],&s[8][0],&s[8][1],&s[8][2],&s[8][3],&s[8][4],&s[8][5],&s[8][6],&s[8][7],&s[8][8]);
14   - solved_game(s);
  23 + if ( solved_game(s) )
  24 + printf("Instancia %d\nSIM\n\n",i);
  25 + else
  26 + printf("Instancia %d\nNAO\n\n",i);
15 27 instances--;
16 28 }
17 29 return 0;
18 30 }
19 31
20 32 int solved_game(int s[9][9]) {
21   - int result = 0;
22 33 int i;
23 34 int p[9][2];
  35 + int n_r = 0;
  36 + int n_c = 0;
24 37 reset_stretch(p);
25   - /* Logic its about see each line or each column and see if exists in a simple line */
26   - /* new method for it */
27   - return result;
  38 +
  39 + for (i = 0; i < 9; i++) {
  40 + if ( check_rows(s,p,i) )
  41 + return 0;
  42 +
  43 + reset_stretch(p);
  44 +
  45 + if ( check_columns(s,p,i) )
  46 + return 0;
  47 +
  48 + reset_stretch(p);
  49 + }
  50 +
  51 + while (n_r < 9) {
  52 + while (n_c < 9 ) {
  53 + reset_stretch(p);
  54 +
  55 + p[0][0] = s[n_r][n_c];
  56 + p[1][0] = s[n_r][n_c+1];
  57 + p[2][0] = s[n_r][n_c+2];
  58 + p[3][0] = s[n_r+1][n_c];
  59 + p[4][0] = s[n_r+1][n_c+1];
  60 + p[5][0] = s[n_r+1][n_c+2];
  61 + p[6][0] = s[n_r+2][n_c];
  62 + p[7][0] = s[n_c+2][n_c+1];
  63 + p[8][0] = s[n_r+2][n_c+2];
  64 +
  65 + if ( check_block(p) )
  66 + return 0;
  67 +
  68 + n_c = n_c + 3;
  69 + }
  70 + n_c = 0;
  71 + n_r = n_r + 3;
  72 + }
  73 +
  74 + return 1;
28 75 }
29 76
30 77 int * reset_stretch(int s[9][2]) {
@@ -35,3 +82,36 @@ int * reset_stretch(int s[9][2]) {
35 82 }
36 83 return (int*)s;
37 84 }
  85 +
  86 +int check_rows(int game[9][9], int stretch[9][2], int n_row) {
  87 + int i = 0;
  88 + for (i = 0; i < 9; i++) {
  89 + if ( stretch[game[n_row][i]-1][1] )
  90 + return 1;
  91 + else
  92 + stretch[game[n_row][i]-1][1] = 1;
  93 + }
  94 + return 0;
  95 +}
  96 +
  97 +int check_columns(int game[9][9],int stretch[9][2],int n_column) {
  98 + int i = 0;
  99 + for (i = 0; i < 9; i++) {
  100 + if ( stretch[game[i][n_column]-1][1] )
  101 + return 1;
  102 + else
  103 + stretch[game[i][n_column]-1][1] = 1;
  104 + }
  105 + return 0;
  106 +}
  107 +
  108 +int check_block(int s[9][2]) {
  109 + int i = 0;
  110 + for (i = 0; i < 9; i++) {
  111 + if ( s[i][1] )
  112 + return 1;
  113 + else
  114 + s[i][1] = 1;
  115 + }
  116 + return 0;
  117 +}

0 comments on commit 6332b8b

Please sign in to comment.
Something went wrong with that request. Please try again.