Ejemplo n.º 1
0
/* Output a solution indicator if the specified cell is part of the solution. */
void maze_to_fig_solution(FILE *fp, struct maze *mp, int row, int col, int upc)
{
	if ((maze_get_cell(mp, row, col) & SOLUTION) == 0)
		return;
	if (maze_cells_connected(mp, row, col, row - 1, col) &&
	    (maze_get_cell(mp, row - 1, col) & SOLUTION))
		maze_to_fig_line(fp, col * upc + upc / 2, row * upc,
				 col * upc + upc / 2, row * upc + upc / 2,
				 14, 51);
	if (maze_cells_connected(mp, row, col, row + 1, col) &&
	    (maze_get_cell(mp, row + 1, col) & SOLUTION))
		maze_to_fig_line(fp, col * upc + upc / 2, row * upc + upc / 2,
				 col * upc + upc / 2, row * upc + upc,
				 14, 51);
	if (maze_cells_connected(mp, row, col, row, col - 1) &&
	    (maze_get_cell(mp, row, col - 1) & SOLUTION))
		maze_to_fig_line(fp, col * upc, row * upc + upc / 2,
				 col * upc + upc / 2, row * upc + upc / 2,
				 14, 51);
	if (maze_cells_connected(mp, row, col, row, col + 1) &&
	    (maze_get_cell(mp, row, col + 1) & SOLUTION))
		maze_to_fig_line(fp, col * upc + upc / 2, row * upc + upc / 2,
				 col * upc + upc, row * upc + upc / 2,
				 14, 51);
}
Ejemplo n.º 2
0
/*
 * Wall in the specified cell.  This allows easy construction of
 * unsolvable mazes.  Returns 1 on success, and 0 if the specified
 * cell was not in the maze.
 */
int maze_wall_in(struct maze *mp, int row, int col)
{
	if (!maze_cell_exists(mp, row, col))
		return 0;
	if (maze_cells_connected(mp, row, col, row - 1, col))
		maze_build_one_cell_wall(mp, row - 1, col,
					 row, col, row, col, row, col);
	if (maze_cells_connected(mp, row, col, row + 1, col))
		maze_build_one_cell_wall(mp, row + 1, col,
					 row, col, row, col, row, col);
	if (maze_cells_connected(mp, row, col, row, col - 1))
		maze_build_one_cell_wall(mp, row, col - 1,
					 row, col, row, col, row, col);
	if (maze_cells_connected(mp, row, col, row, col + 1))
		maze_build_one_cell_wall(mp, row, col + 1,
					 row, col, row, col, row, col);
	return 1;
}
Ejemplo n.º 3
0
/*
 * Is the second cell the predecessor to the first one along the
 * solution path?  The "backwards" order of arguments is due to
 * the fact that we traverse the solution backwards when marking it.
 */
int maze_is_prev_cell(struct maze *mp, int row1, int col1, int row2, int col2,
		      int *nextrow, int *nextcol)
{
	if (!maze_cells_connected(mp, row1, col1, row2, col2) ||
	    !maze_same_tids(mp, row1, col1, row2, col2) ||
	    maze_get_cell_distance(mp, row1, col1) -
	    maze_get_cell_distance(mp, row2, col2) != 1)
		return 0;
	*nextrow = row2;
	*nextcol = col2;
	return 1;
}
Ejemplo n.º 4
0
/* Attempt to visit a cell, return 0 if it has already been visited.  */
int maze_try_visit_cell(struct maze *mp, int cr, int cc, int tr, int tc,
			int *nextrow, int *nextcol, int distance)
{
	cell_t t;
	cell_t *tp;
	int vi;

	if (!maze_cells_connected(mp, cr, cc, tr, tc))
		return -1;
	tp = maze_get_cell_addr(mp, tr, tc);
	do {
		t = ACCESS_ONCE(*tp);
		if (t & VISITED) {
			record_encounter(mp, cr, cc, tr, tc);
			return 1;
		}
	} while (!__sync_bool_compare_and_swap(tp, t, t | VISITED | myid));
	maze_set_cell_distance(mp, tr, tc, distance);
	*nextrow = tr;
	*nextcol = tc;

	/* If we are solving the maze, mymcp is non-NULL.  */
	if (mymcp != NULL) {
		/* Use this thread's state. */
		vi = mymcp->vi++;
		mymcp->visited[vi].row = tr;
		mymcp->visited[vi].col = tc;
		if (nthreads == 1 &&
		    tr == mymcp->mcsp->endrow && tc == mymcp->mcsp->endcol)
			mymcp->mcsp->done = 1;
	} else {
		/* Use global state. */
		vi = mp->vi++;
		mp->visited[vi].row = tr;
		mp->visited[vi].col = tc;
	}
	return 0;
}