Ejemplo n.º 1
1
int main()
{
   initscr();
   keypad(stdscr, TRUE);
   noecho();
   start_color();
   init_pair(1, COLOR_GREEN,   COLOR_BLACK);
   init_pair(2, COLOR_MAGENTA, COLOR_BLACK);
   finit(9,9,0,2);
   clear();
   printw(
   "      Etherial Void   \n\n"
   "Is it that all exists only in the\n"
   "etherial landscape of the mind?\n\n"
   "The past is uncertian,\n\n"
   "The present is now the past,\n\n"
   "The future might only be thought of.\n\n"
   "Is then not our mind's eye\n"
   "the root of reality?\n\n"
   "But should that eye be blind,\n"
   "where then would rest the soul?"
   "\n\n"
   "(press any key to continue)\n");
   getch();
   clear();
   printw(
   "   I look,\n"
   "but I see nothing.");
   getch();
   clear();
   getch();
   printw(
   "   Perhaps,\n"
   "there is nothing to see.");
   getch();
   helpmsg="Use the arrow keys or WASD to move the charecter.";
   clear();
   while(loop)
   {  
      switch(z)
      {  
         case 0:
            nroom=1;
            eroom=2;
            sroom=3;
            wroom=4;
            uroom=5;
            light=2;
            wall=
            "Some sort of wall mesage should be displayed here,\n"
            "It should inform the player of the solidity of this wall,\n"
            "and it should warn that attempts to surpass this fact are admonishable.";
            
            msg(9,6,
            "   I <do> see something:\n"
            "There seem to be dimly glowing etchings on the ground which signify,\n"
            "'There is naught to the north.'");

            msg(12,9,
            "   There is <something> here:\n"
            "In the ground there are shallow grooves filled with a\n"
            "faintly luminescent material. I cannot comprehend them.");
            if(sw[0]&&sw[1]&&sw[2]&&sw[3])
            {
               map[0][9][9]='^';
               smsg(9,9,
               "This is a new stairwell.");
            }
            break;
         case 1:
            sroom=0;
            lvr(9,9,0);
            break;
         case 2:
            wroom=0;
            wall=
            "There are some large deep grooves on the wall";
            lvr(9,9,1);
            break;
         case 3:
            nroom=0;
            lvr(9,9,2);
            break;
         case 4:
            eroom=0;
            lvr(9,9,3);
            break;
         case 5:
            eroom=6;
            droom=1;
            break;
         case 6:
            wroom=5;
            break;
         default:
            endwin();
            printf("Error: 42\n");
            loop=0;
            exit(42);
      }
      unmove();
      uncover(0);
      showmap();
      input();
   }
   endwin();
   return 0;
}
Ejemplo n.º 2
0
int search(int k)
{
	struct node_t* r;
	struct node_t* j;
	int max = 0x7fffffff;
	int c;
	if(root.right == &root) return 1;
	for(j = root.right;j != &root; j = j->right)
	{
		if(max > size[j->c])
		{
			max = size[j->c];
			c = j->c;
		}
	}
	cover(c);
	for(r = col[c].down;r != &col[c]; r = r->down)
	{
		for(j = r->right;j != r; j = j->right)
			cover(j->c);
		if(search(k + 1)) return 1;
		for(j = r->left;j != r; j = j->left)
			uncover(j->c);
	}
	uncover(c);
	return 0;
}
Ejemplo n.º 3
0
int search(struct node *h, int i, struct node *solution[])
{
    if (h->right == h)
        return 1;
    struct node *c;
    struct node *tmp7;
    int min = 9999;
    for(tmp7 = h->right; tmp7 != h; tmp7 = tmp7->right)
        if (tmp7->s < min) {
            c = tmp7;
            min = tmp7->s;
        }

    cover(c);
    struct node *r, *tmp;
    for (r = c->down; r != c; r = r->down) {
        solution[i] = r;
        for (tmp = r->right; tmp != r; tmp = tmp->right) {
            cover(tmp->ch);
        }
        if (search(h, i+1, solution) == 1)
            return 1;
        struct node *tmp4;
        for (tmp4 = r->left; tmp4 != r; tmp4 = tmp4->left) {
            uncover(tmp4->ch);
        }
    }
    uncover(c);
    return -1;

}
Ejemplo n.º 4
0
Archivo: dlx.c Proyecto: jw013/dlx
/**
 * Reverse the procedure in cover_other_columns(i), for backtracking.
 *
 * Must be called in exact reverse order as cover_other_columns() to ensure
 * matrix is correctly restored to original state.
 */
static void
uncover_other_columns(struct dlx_node *i)
{
	struct dlx_node *j = i;
	while ((j = j->left) != i)	/* for each column except i */
		uncover((struct dlx_node *) j->header);
}
Ejemplo n.º 5
0
static int search(struct dlx *solver, int k) {
  /* if (k == 21) */
  /*   return 1; */
  int solutions = 0;
  /* printf("k = %d\n", k); */
  struct node *r, *j;

  if (solver->h.next == &solver->h) {
    printsoln(solver, k);
    return 1;
  }

  struct column *c = choosecol(solver);
  if (c->len == 0) {
    printf("X\n");
    uncover(solver, c);
    return 0;
  }

  /* printf("CHOOSE %s\n", c->name); */
  int xx = cover(solver, c);
  if (xx) {
    printf("Got a zero\n");
    return 0;
  }

  for (r = c->node.d; r != &c->node; r = r->d) {
    solver->solnodes[k] = r;
    for (j = r->r; j != r; j = j->r) {
      cover(solver, j->c);
    }
    int rc = search(solver, k+1);
    solutions += search(solver, k+1);
    if (k == 0) {
      printf("Solutions increased by %d to %d\n", rc, solutions);
    }

    for (j = r->l; j != r; j = j->l) {
      uncover(solver, j->c);
    }
    if (solutions >= 1)
      break;
  }
  uncover(solver, c);

  return solutions;
}
Ejemplo n.º 6
0
static void uncoverAll(void)
{
	int x, y;

	for (x = 0; x < GRID_DIM; x++)
		for (y = 0; y < GRID_DIM; y++)
			uncover(x, y);
}
Ejemplo n.º 7
0
bool ExactCoverSolver::solve ()
{
  int cols_count;
  Node<int>* next_col = NULL;
  Node<int>* next_row_in_col = NULL;
  Node<int>* row_node = NULL;

  if (empty ())
  {
    return true;
  }
  next_col = pick_next_col (cols_count);
  if (cols_count < 1)
  {
    return false;
  }
  total_competition_ += cols_count;
  next_row_in_col = next_col->bottom_;
  cover (next_col);
  while (next_row_in_col != next_col && !solved_)
  {
    running_sol_.push (next_row_in_col);
    row_node = next_row_in_col->right_;
    while (row_node != next_row_in_col)
    {
      cover (row_node->col_header_);
      row_node = row_node->right_;
    }
    solved_= solve ();
    if (!solved_)
    {
      running_sol_.pop ();
    }
    row_node = next_row_in_col->right_;
    while (row_node != next_row_in_col)
    {
      uncover (row_node->col_header_);
      row_node = row_node->right_;
    }
    next_row_in_col = next_row_in_col->bottom_;
  }
  uncover (next_col);

  return solved_;
}
Ejemplo n.º 8
0
void Matrix::solve ( quint32 k )
{
        if ( m_header->right == m_header ) {
                ++m_solutions;
                ( *m_solution ) ( m_output, k );
                return;
        }

        HeaderNode* column = 0;
        quint32 s = 0xFFFFFFFF;
        for ( HeaderNode* i = m_header->right->column; i != m_header; i = i->right->column ) {
                if ( i->size < s ) {
                        column = i;
                        s = i->size;
                }
        }

        cover ( column );

        quint32 next_k = k + 1;

        for ( Node* row = column->down; row != column; row = row->down ) {
                m_output[k] = row;

                for ( Node* j = row->right; j != row; j = j->right ) {
                        cover ( j->column );
                }

                solve ( next_k );

                if ( m_solutions >= m_max_solutions ) {
                        return;
                }

                row = m_output[k];
                column = row->column;

                for ( Node* j = row->left; j != row ; j = j->left ) {
                        uncover ( j->column );
                }
        }

        uncover ( column );
}
Ejemplo n.º 9
0
Archivo: dlx.c Proyecto: jw013/dlx
int
dlx_unselect_row(struct dlx_node *r)
{
	if (!is_removed_ud(r))
		return -1;

	uncover_other_columns(r);
	uncover((struct dlx_node *) r->header);
	return 0;
}
Ejemplo n.º 10
0
static void eventHandler(objectKey key, windowEvent *event)
{
	int x = 0;
	int y = 0;

	// Check for window events.
	if (key == window)
	{
		// Check for window refresh
		if (event->type == EVENT_WINDOW_REFRESH)
			refreshWindow();

		// Check for the window being closed
		else if (event->type == EVENT_WINDOW_CLOSE)
			windowGuiStop();
	}

	// Only go through the array of buttons if the event was a mouse click
	else if (event->type == EVENT_MOUSE_LEFTUP)
	{
		for (x = 0; x < GRID_DIM; x++)
		{
			for (y = 0; y < GRID_DIM; y++)
			{
				if (key == gridButtons[x][y])
				{
					// If this spot is empty, invoke the clickEmpties function
					if (mineField[x][y] == -1)
					{
						clickEmpties(x, y);
					}
					else
					{
						if (mineField[x][y] == 9)
						{
							gameOver(0);
						}
						else
						{
							uncover(x, y);

							if (numUncovered >=
								((GRID_DIM * GRID_DIM) - NUM_MINES))
							{
								gameOver(1);
							}
						}
					}

					return;
				}
			}
		}
	}
}
Ejemplo n.º 11
0
void search(int k){
	if(R[0] == 0){
		for(int i = 0; i < k; i ++)
			printf("%d\n", pos[O[i]].x);
		exit(0);
	}
	int s = INF, c;
	for(int j = R[0]; j != 0; j = R[j])
		if(S[j] < s)
			c = j, s = S[j];
	cover(c);
	for(int r = D[c]; r != c; r = D[r]){
		O[k] = r;
		for(int j = R[r]; j != r; j = R[j])
			cover(C[j]);
		search(k + 1);
		for(int j = L[r]; j != r; j = L[j])
			uncover(C[j]);
	}
	uncover(c);
}
Ejemplo n.º 12
0
	void search(int k){
		if(R[0] == 0){
			done = true;
			n_res = k;
			return;
		}
		int s = INF, c;
		for(int j = R[0]; j != 0; j = R[j])
			if(S[j] < s)
				c = j, s = S[j];
		cover(c);
		for(int r = D[c]; !done && r != c; r = D[r]){
			O[k] = r;
			for(int j = R[r]; j != r; j = R[j])
				cover(C[j]);
			search(k + 1);
			for(int j = L[r]; j != r; j = L[j])
				uncover(C[j]);
		}
		uncover(c);
	}
Ejemplo n.º 13
0
static void clickEmpties(int x, int y)
{
	// Recursive function which uncovers empty squares

	if (mineField[x][y])
	{
		uncover(x, y);

		if (mineField[x][y] == -1)
		{
			mineField[x][y] = 0;

			// Start from top left corner and make my way around

			if ((x >= 1) && (y >= 1) && (mineField[x - 1][y - 1] != 9))
				clickEmpties((x - 1), (y - 1));

			if ((y >= 1) && (mineField[x][y - 1] != 9))
				clickEmpties(x, (y - 1));

			if ((x < (GRID_DIM - 1)) && (y >= 1) &&
				(mineField[x + 1][y - 1] != 9))
			{
				clickEmpties((x + 1), (y - 1));
			}

			if ((x < (GRID_DIM - 1)) &&	(mineField[x + 1][y] != 9))
				clickEmpties((x + 1), y);

			if ((x < (GRID_DIM - 1)) && (y < (GRID_DIM - 1)) &&
				(mineField[x + 1][y + 1] != 9))
			{
				clickEmpties((x + 1), (y + 1));
			}

			if ((y < (GRID_DIM - 1)) &&	(mineField[x][y + 1] != 9))
				clickEmpties(x, (y + 1));

			if ((x >= 1) && (y < (GRID_DIM - 1)) &&
				(mineField[x - 1][y + 1] != 9))
			{
				clickEmpties((x - 1), (y + 1));
			}

			if ((x >= 1) &&	(mineField[x - 1][y] != 9))
				clickEmpties((x - 1), y);
		}
	}

	return;
}
Ejemplo n.º 14
0
int main()
{  finit( 9, 9, 0, 3);
   int n; /* having a spare int is useful. */
   time_t t;
   printw(
   "   This game is so interesting and meaningfull\n"
   "and the reasons why are explained in this message...\n"
   "...which may or may not be complete\n"
   "\n"
   "(press any 3 keys to continue)\n");
   getch();
   getch();
   getch();
   clear();

   #include "objects.h"

   while(loop)
   {  
      switch(z)
      {  
         case 0:
            uroom[0]=5;
            wroom[0]=1;
 
            wall=
            "Some sort of wall mesage should be displayed here,\n"
            "It should inform the player of the solidity of this wall,\n"
            "and it should warn that attempts to surpass this fact are admonishable.";

            event(11,11,30,"hello");
            event(11,11,31,"hello again");
            event(11,11,32,"hello a third time");

            mesage[0][0].x  =12;
            mesage[0][0].y  =12;
            mesage[0][0].ch='*';
            mesage[0][0].hit="Hello sir, this is a structure mesage";
 
            msg(8,9,
            "THIS SPOT IS IMPORTANT FOR THIS REASON");
            smsg(2,9,
               "   This spot is UBER important :)\n"
               "It clears the screen to show you something!!!! \n");
            smsg(2,9,
               "   And it can do it twice!!!\n");
            smsg(2,9,
               "\n"
               "And thrice as well!!! :)");
            if(sw[0])
            {
               smsg(9,9,
               "This is a new stairwell.");
            }
            break;
         case 1:
            wall=
            "this is a wall in room 1";
            droom[1]=2;
            eroom[1]=0;
            sroom[1]=4;
            break;
         case 2:
            wall=
            "this is a more interesting wall in room 2";
            sroom[2]=3;
            uroom[2]=1;
            break;
         case 3:
            wall=
            "in room 3, the wall is so interesting, it's unsurpassable...";
            nroom[3]=2;
            break;
         case 4:
            wall=
            "...when it's not compared to these walls in room 4\n"
            "(see room 3's walls)";
            
            smsg(6,14,
            "   this is an object of\n"
            "INSURMOUNTABLE POWER...\n"
            "It will somehow help you on your\n"
            "QUEST,\n"
            "(which has neither been defined nor\n"
            "ACCEPTED)...\n"
            "\n"
            "...once the master programmer imbues\n"
            "it with the power to do such.\n"
            "\n"
            "Until then, walk from here back to\n"
            "THE BEGINING,\n"
            "and from there to here 9 times 7.\n"
            "\n");
             
            smsg(6,14,
            "+-----------------------------------+"
            "|                                   |"
            "|                                   |"
            "|            ????                   |"
            "|          ??  ? ???                |"
            "|            ??     ??              |"
            "|             ####    ?   ####      |"
            "|          ###    ##   ? ##  ##     |"
            "|        ##     ?   #  ?     ##     |"
            "|      ##     ?   o   ?     ##      |"
            "|     ##     ?  #   ?     ##        |"
            "|     ##  ## ?   ##    ###          |"
            "|      ####   ?    ####             |"
            "|              ??     ??            |"
            "|                ??? ?  ??          |"
            "|                   ????            |"
            "|                                   |"
            "|                                   |"
            "+-----------------------------------+");

            smsg(6,14,
            "   The Master Programmer hath spoken."
            "He hath declared that the way forth  "
            "shall become clear if you retrace    "
            "your steps to your point of origin.  ");

            lvr(6,14,0);
            if(sw[0]==1)
            {
               map[0][9][9]='^';
               sw[0]==2;
            }
            nroom[4]=1;
            break;
         case 5:
            wall=
            "there is nothing extra-ordinary about these walls";
            droom[5]=0;
            nroom[5]=6;
            smsg(9,8,
            "\n\n\n   Time:\n"
            "all encompasing,\n"
            "inescapable...\n");
            
            smsg(9,1,
            "\n\n\n...incorporeal.\n\n"
            "   In Time\n" /* "Time" which refers to the clock, is a proper noun in this case*/
            "The Trinity will point forward...");

            lvr(9 , 4,1);
            lvr(13, 5,2);
            lvr(14,9 ,3);
            lvr(13,13,4);
            lvr(9 ,14,5);
            lvr(5 ,13,6);
            lvr(4 , 9,7);
            lvr(5 , 5,8);

            if(sw[1]&&sw[3]&&sw[5]&&sw[7]||sw[2]&&sw[4]&&sw[6]&&sw[8])
            {
               sw[1]=0;
               sw[2]=0;
               sw[3]=0;
               sw[4]=0;
               sw[5]=0;
               sw[6]=0;
               sw[7]=0;
               sw[8]=0;
            }
            if(sw[1]&&sw[4]&&sw[6]||sw[2]&&sw[5]&&sw[7]||sw[8]&&sw[5]&&sw[3])
            {
               map[5][0][ 8]=' ';
               map[5][0][ 9]=' ';
               map[5][0][10]=' ';
            }
            else
            {
               map[5][0][ 8]='-';
               map[5][0][ 9]='-';
               map[5][0][10]='-';
            }
            
            if(time(NULL)>t)
            {
            int a,b,c,d,e,f,g,h;
            a=sw[1]; //rotate lvrs
            b=sw[2];
            c=sw[3];
            d=sw[4];
            e=sw[5];
            f=sw[6];
            g=sw[7];
            h=sw[8];
            sw[1]=h;
            sw[2]=a;
            sw[3]=b;
            sw[4]=c;
            sw[5]=d;
            sw[6]=e;
            sw[7]=f;
            sw[8]=g;
            }
            t=time(NULL);
            break;
         case 6:
            sroom[6]=5;
            uroom[6]=7;

            map[5][0][ 8]=' ';
            map[5][0][ 9]=' ';
            map[5][0][10]=' ';

            msg( 9, 9,
            "...all will be of one substance...");

            lvr( 6, 6, 9);
            lvr(12, 6,10);
            lvr( 9, 7,11);
            lvr( 7, 9,12);
            lvr(11, 9,13);
            lvr( 9,11,14);
            lvr( 6,12,15);
            lvr(12,12,16);

            if(sw[ 9]==1)
            {
               flip(13);
               flip(14);
               sw[ 9]=2;
            }
            if(sw[10]==1)
            {
               flip(12);
               flip(14);
               sw[10]=2;
            }
            if(sw[11]==1)
            {
               flip(15);
               flip(16);
               sw[11]=2;
            }
            if(sw[12]==1)
            {
               flip(10);
               flip(16);
               sw[12]=2;
            } 
            if(sw[13]==1)
            {
               flip( 9);
               flip(15);
               sw[13]=2;
            }
            if(sw[14]==1)
            {
               flip( 9);
               flip(10);
               sw[14]=2;
            }
            if(sw[15]==1)
            {
               flip(13);
               flip(11);
               sw[15]=2;
            }
            if(sw[16]==1)
            {
               flip(11);
               flip(12);
               sw[16]=2;
            }
            lvr( 6, 6, 9);
            lvr(12, 6,10);
            lvr( 9, 7,11);
            lvr( 7, 9,12);
            lvr(11, 9,13);
            lvr( 9,11,14);
            lvr( 6,12,15);
            lvr(12,12,16);
            if(sw[9]&&sw[10]&&sw[11]&&sw[12]&&sw[13]&&sw[14]&&sw[15]&&sw[16])
              {map[6][9][9]='^';}
            
            break;
         case 7:
            uroom[7]=8;
            droom[7]=6;
            lvr( 5, 5,17);
            lvr(13, 5,18);
            lvr(13,13,19);
            lvr(5 ,13,20);
            
            for(n=17;n<20;n++)
            {
               if(sw[n]==1)
               {
                  flip(n+4);
                  flip(n+1);
                  flip(n);
               }
            }
            if(sw[20]==1)
            {
               flip(17);
               flip(24);
               flip(20);
            }
            mlvr( 5, 5,17);
            mlvr(13, 5,18);
            mlvr(13,13,19);
            mlvr(5 ,13,20);

            break;
         case 8:
            uroom[8]=9;
            droom[8]=7;

            lvr( 9, 9,30);

            lvr( 5, 5,21);
            lvr(13, 5,22);
            lvr(13,13,23);
            lvr(5 ,13,24);
            
            for(n=22;n<=24;n++)
            {
               if(sw[n]==1)
               {
                  flip(n-1);
                  flip(n+4);
                  flip(n-4);
                  flip(n);
               }
            }
            if(sw[21]==1)
            {
               flip(24);
               flip(25);
               flip(17);
               flip(21);
            }
            if(sw[30]==1)
            {
               int a,b,c,d;
               a=sw[21];
               b=sw[22];
               c=sw[23];
               d=sw[24];
               sw[21]=b;
               sw[22]=c;
               sw[23]=d;
               sw[24]=a;
               flip(30);
            }

            mlvr( 9, 9,30);
            mlvr( 5, 5,21);
            mlvr(13, 5,22);
            mlvr(13,13,23);
            mlvr(5 ,13,24);
            break;
         case 9:
            droom[9]=8;
            uroom[9]=10;
            lvr( 9, 9,29);

            lvr( 5, 5,25);
            lvr(13, 5,26);
            lvr(13,13,27);
            lvr(5 ,13,28);
            
            for(n=25;n<28;n++)
            {
               if(sw[n]==1)
               {
                  flip(n+1);
                  flip(n-4);
                  flip(n);
               }
            }
            if(sw[28]==1)
            {
               flip(25);
               flip(24);
               flip(28);
            }
            if(sw[29]==1)
            {
               int a,b,c,d;
               a=sw[25];
               b=sw[26];
               c=sw[27];
               d=sw[28];
               sw[25]=d;
               sw[26]=a;
               sw[27]=b;
               sw[28]=c;
               flip(29);
            }
            mlvr( 5, 5,25);
            mlvr(13, 5,26);
            mlvr(13,13,27);
            mlvr(5 ,13,28);

            break;
         default:
            endwin();
            printf("ERROR THE PROGGRAMMER HAS YET TO MAKE THIS LEVEL!!!\n");
            loop=0;
            return 1;
      }
      mesages();
      levers();
      BlockMessage();
      uncover(1);
      showmap();
      input();
   }

   endwin();
   return 0;
}
Ejemplo n.º 15
0
Archivo: dlx.c Proyecto: jw013/dlx
size_t dlx_exact_cover(struct dlx_srow *solution, struct dlx_hnode *root,
		size_t k, size_t *pnsol) {
	/*
	 * A basic summary of the Dancing Links Algorithm, as described by
	 * Knuth on page 5 of his DLX paper (link in file header comment).
	 *
	 * Base cases:
	 *  [1] matrix is empty (success: entire matrix has been covered)
	 *   	complete termination: no more recursion
	 *  [2] empty column is found (failure: * uncover-able column)
	 *   	partial termination: unwind one level of the call stack but
	 *   	keep recursing into other branches.
	 * Recursive steps:
	 *   * select column with fewest candidate rows
	 *   * select a row within that column for the solution
	 *   * recurse
	 *   * if no solution found by recursive step,
	 *   	try another row in the column until a base case is hit
	 *
	 * This function makes the following modification to the Knuth DLX
	 * algorithm to allow optional skipping of the first few solutions
	 * found.
	 *
	 * Base cases:
	 *  [1'] matrix is empty (success: entire matrix has been covered)
	 *   	partial termination: unwind one level of the call stack but
	 *   	keep recursing into other branches.
	 *   	decrement the value of *pnsol
	 *  [2] unchanged
	 *  [3] *pnsol == 0 (enough solutions found; stop looking)
	 *  	complete termination: no more recursion
	 *
	 * Recursive steps:
	 *   * ... as before
	 *   * recurse
	 *   * if no solution found AND (*pnsol != 0),
	 *   	try another row in the column ... (as before)
	 *
	 * If this new base case [3] is unreachable (because not enough
	 * solutions exist), then each recursion branch will be terminated by
	 * base case [2] instead, until eventually the entire search tree is
	 * exhausted, and the return value of 0 will be passed all the way up
	 * the call stack.
	 *
	 * There is a question of whether or not the same solution (i.e. a set
	 * of rows where the order does not matter) can be found and counted
	 * multiple times during the course of the backtracking algorithm.  My
	 * hunch is the answer is no, but this is not formally proven.  If
	 * true, then *pnsol will be decremented by the number of *distinct*
	 * solutions, which makes the *pnsol mechanism much more useful for
	 * tasks like checking uniqueness.
	 */

	size_t n = 0;		/* return value, default 0 := no solution */
	struct dlx_node *i;	/* iterator pointer */ struct dlx_node *col;
	/* column to cover in this iteration */

	/* root->right == root means empty matrix */
	if (((struct dlx_node *) root)->right == (struct dlx_node *) root) {
		(*pnsol)--;
		return k;
	}

	col = (struct dlx_node *) hnode_min_count(root);
	cover(col);

	solution[k].cid		= ((struct dlx_hnode *) col)->id;
	solution[k].n_choices	= ((struct dlx_hnode *) col)->node_count;

	/* try selecting each row in the column one at a time and recurse */
	i = col;
	while ((i = i->down) != col) {	/* for each row except header row */
		cover_other_columns(i);
		n = dlx_exact_cover(solution, root, k + 1, pnsol);
		uncover_other_columns(i);
		if (n > 0)	/* solution found */
			solution[k].row_node = i;
		if (*pnsol == 0)
			break;
	}

	/* restore node links and return */
	uncover(col);
	return n;
}
Ejemplo n.º 16
0
void ExactCoverSolver::solve (const std::vector <std::vector <int> >& input_grid)
{
  Node <int>* to_find = NULL;
  Node <int>* insert_next = NULL;
  std::stack <Node <int>*> puzzle_nodes;
  Node <int>* col_node;
  Node <int>* next_row_in_col;
  Node <int>* row_node;
  int val = 0;
  
  solved_ = false;
  total_competition_ = 0;
//  running_sol_.clear ();
  while (!running_sol_.empty ())
  {
    running_sol_.pop();
  }

  for (int i = 0; i < GRID_SIZE_; ++i)
  {
    for (int j = 0; j < GRID_SIZE_; ++j)
    {
      val = input_grid[i][j];
      if (val > GRID_SIZE_ || val < 0)
      {
        std::cout << "ERROR! Invalid puzzle specified." << std::endl;
        return;
      }
      
      if (val != 0)
      {
        to_find = new Node<int> (i, j, val - 1);
        insert_next = find (to_find);
        if (insert_next == NULL)
        {
          std::cerr << "ERROR! Repeated or invalid value '" << val \
          << "' in puzzle at row: " << i + 1 << ", column: " << j + 1 << "." << std::endl;
          return;
        }
        col_node = insert_next->col_header_;
        next_row_in_col = insert_next;
        cover (col_node);
        row_node = next_row_in_col->right_;
        while (row_node != next_row_in_col)
        {
          cover (row_node->col_header_);
          row_node = row_node->right_;
        }
        puzzle_nodes.push (insert_next);
        running_sol_.push (insert_next);
        delete to_find;
        to_find = NULL;
      }
    }
  }

  if (!solve ())
  {
    std::cout << "Puzzle is not solvable or multiple solutions exists." << std::endl; 
  }
  /// Restore initial state to prepare for next puzzle
  while (!puzzle_nodes.empty())
  {
    col_node = (puzzle_nodes.top ())->col_header_;
    next_row_in_col = (puzzle_nodes.top ());
    row_node = next_row_in_col->right_;
    while (row_node != next_row_in_col)
    {
      uncover (row_node->col_header_);
      row_node = row_node->right_;
    }
    uncover (col_node);
    puzzle_nodes.pop();
  }
}