Commit dd5dd234 authored by Seblu's avatar Seblu
Browse files

No commit message

No commit message
parent 402d88aa
Loading
Loading
Loading
Loading

README

0 → 100644
+3 −0
Original line number Original line Diff line number Diff line
Sudoku Solver

Wrote by Sebastien Luttringer
 No newline at end of file

TODO

0 → 100644
+1 −0
Original line number Original line Diff line number Diff line
Bug: le dernier carre est pas initialise. voir avec la memeoire!
+4 −3
Original line number Original line Diff line number Diff line
@@ -52,10 +52,10 @@ AC_ARG_WITH([noerror],
AC_ARG_WITH([debug],
AC_ARG_WITH([debug],
  [AS_HELP_STRING([--with-debug], [use -g and don't use -DNDEBUG -O3])],
  [AS_HELP_STRING([--with-debug], [use -g and don't use -DNDEBUG -O3])],
  [dnl action-if-given
  [dnl action-if-given
       CXXFLAGS="$CFLAGS -g"
       CXXFLAGS="$CXXFLAGS -g"
  ],
  ],
  [dnl action-if-not-given
  [dnl action-if-not-given
      CXXFLAGS="$CFLAGS -DNDEBUG -O3"
      CXXFLAGS="$CXXFLAGS -DNDEBUG -O3"
  ]
  ]
)
)


@@ -63,10 +63,11 @@ AC_ARG_WITH([efence],
  [AS_HELP_STRING([--with-efence], [link with lib efence])],
  [AS_HELP_STRING([--with-efence], [link with lib efence])],
  [dnl action-if-given
  [dnl action-if-given
       LDFLAGS="$LDFLAGS -lefence"
       LDFLAGS="$LDFLAGS -lefence"
       test -r "/usr/include/efence.h" &&
       CXXFLAGS="$CXXFLAGS -include stdlib.h -include efence.h"
       CXXFLAGS="$CXXFLAGS -include stdlib.h -include efence.h"
  ],
  ],
  [dnl action-if-not-given
  [dnl action-if-not-given
       test "$dynamic" = "true" || LDFLAGS="$LDFLAGS -static"
       true
  ]
  ]
)
)


+8 −3
Original line number Original line Diff line number Diff line
@@ -14,7 +14,11 @@ inline Block::Block(int value) {


inline bool Block::is_forbidden(int value) const {
inline bool Block::is_forbidden(int value) const {
  assert(value > 0 && value <= GRID_SIDE);
  assert(value > 0 && value <= GRID_SIDE);
  return forbidden_[value];
  return forbidden_[value - 1];
}

inline bool Block::is_set() const {
  return value_ != 0;
}
}


inline int Block::value_get() const
inline int Block::value_get() const
@@ -26,12 +30,13 @@ inline void Block::set(int val)
{
{
  assert(val > 0 && val <= GRID_SIDE);
  assert(val > 0 && val <= GRID_SIDE);
  value_ = val;
  value_ = val;
  for (int i = 0; i < GRID_SIDE; ++i)
    forbidden_[i] = true;
}
}


inline void Block::forbid(int val) {
inline void Block::forbid(int val) {
  assert(val > 0 && val <= GRID_SIDE);
  assert(val > 0 && val <= GRID_SIDE);
  if (val != value_)
  forbidden_[val - 1] = true;
    forbidden_[val - 1] = true;
}
}


inline std::ostream &operator<<(std::ostream &stream, const Block &blk) {
inline std::ostream &operator<<(std::ostream &stream, const Block &blk) {
+23 −16
Original line number Original line Diff line number Diff line
@@ -17,6 +17,8 @@ int Grid::load(const char *filename)
  for (int y = 0; y < GRID_SIDE; ++y)
  for (int y = 0; y < GRID_SIDE; ++y)
    for (int x = 0; x < GRID_SIDE; ++x) {
    for (int x = 0; x < GRID_SIDE; ++x) {
      fs >> std::dec >> val;
      fs >> std::dec >> val;
//       if (val > 0)
// 	std::cout << "(" << x << "," << y << ")" << " " << val << std::endl;
      if (val < 0 || val > 9) {
      if (val < 0 || val > 9) {
	std::cerr << "Invalid value in file: " << val << std::endl;
	std::cerr << "Invalid value in file: " << val << std::endl;
	exit(EXIT_LOADFAIL);
	exit(EXIT_LOADFAIL);
@@ -39,13 +41,18 @@ void Grid::pose(int x, int y, int v)
    std::cerr << "Invalid value: " << v << std::endl;
    std::cerr << "Invalid value: " << v << std::endl;
    exit(EXIT_INV_VAL);
    exit(EXIT_INV_VAL);
  }
  }
  if (block_[x][y].is_set()) {
    std::cerr << "Double positionning: " << x << ", " << y << std::endl;
    exit(EXIT_INV_VAL);
  }
  block_[x][y].set(v);
  for (int i = 0; i < GRID_SIDE; ++i) {
  for (int i = 0; i < GRID_SIDE; ++i) {
    block_[x][i].forbid(v);
    block_[x][i].forbid(v);
    block_[i][y].forbid(v);
    block_[i][y].forbid(v);
  }
  }
  for (int i = 0; i < 3; ++i)
  for (int i = 0; i < 3; ++i)
    for (int j = 0; j < 3; ++j)
    for (int j = 0; j < 3; ++j)
      block_[(x / 3) * 3 + x][(y / 3) * 3 + y].forbid(v);
      block_[(x / 3) * 3 + i][(y / 3) * 3 + j].forbid(v);
}
}


void Grid::resolve()
void Grid::resolve()
@@ -56,34 +63,34 @@ void Grid::resolve()


void Grid::print() const
void Grid::print() const
{
{
  int i, j, k;
  int x, y;


  for (i = 0; i < GRID_SIDE; ++i) {
  for (y = 0; y < GRID_SIDE; ++y) {
    print_line();
    print_line();
    if (i > 0 && i % 3 == 0)
    if (y > 0 && y % 3 == 0)
      print_line();
      print_line();
    if (Grid::advprint) {
    if (Grid::advprint) {
      for (int l = 0; l < 3; ++l) {
      for (int i = 0; i < 3; ++i) {
	for (j = 0; j < GRID_SIDE; ++j) {
	for (x = 0; x < GRID_SIDE; ++x) {
	  if (j > 0 && j % 3 == 0)
	  if (x > 0 && x % 3 == 0)
	    std::cout << "| ";
	    std::cout << "| ";
	  std::cout << "|";
	  std::cout << "|";
	  for (k = l * 3 + 1; k <= l * 3 + 3; ++k)
	  for (int j = i * 3 + 1; j <= i * 3 + 3; ++j)
	    if (block_[i][j].value_get() == k)
	    if (block_[x][y].value_get() == j)
	      std::cout << "\033[0;32m" << k  << "\033[0m";
	      std::cout << "\033[0;32m" << j << "\033[0m";
	    else if (block_[i][j].is_forbidden(k))
	    else if (block_[x][y].is_forbidden(j))
	      std::cout << "\033[0;31m" << k  << "\033[0m";
	      std::cout << "\033[0;31m" << j << "\033[0m";
	    else
	    else
	      std::cout << k;
	      std::cout << j;
	}
	}
	std::cout <<  "|" << std::endl;
	std::cout <<  "|" << std::endl;
      }
      }
    }
    }
    else {
    else {
      std::cout << "|";
      std::cout << "|";
      for (j = 0; j < GRID_SIDE; ++j) {
      for (x = 0; x < GRID_SIDE; ++x) {
	if (j > 0 && j % 3 == 0) std::cout << " |";
	if (x > 0 && x % 3 == 0) std::cout << " |";
	std::cout << " " << block_[i][j] << " |";
	std::cout << " " << block_[x][y] << " |";
      }
      }
      std::cout << std::endl;
      std::cout << std::endl;
    }
    }
Loading