Beispiel #1
0
/***************Level Order****************/
void levelOrder(BTNode *b)
{
	QUE *q = newQue();
	BTNode *tmp = b;
	
	enQue(q,tmp);
	while (!queEmpty(q))
	{
		tmp = deQue(q);
		printf("%c ",tmp->data);
		if (tmp->lchild != NULL)
			enQue(q,tmp->lchild);
		if (tmp->rchild != NULL)
			enQue(q,tmp->rchild);
	}
}
Beispiel #2
0
void process(QUE *q) {
	PCB *tmp;
	if (q == NULL)
		return;
	tmp = deQue(q);
	tmp->ranTime += 1;
	printf("Processing %s reqTime = %d ranTime = %d\n",
			tmp->name, tmp->reqTime, tmp->ranTime);

	if (isFinished(tmp)) {
		printf("Process %s is finished.\n",tmp->name);
		free(tmp);
	} else {
		enQue(q,tmp);
	}
}
Beispiel #3
0
/**
 *  breadth first search, if ends with exit found, return steps.
 *  else -1
 */
int bsf (Board board) {
	Node *candidate = NULL;
	int ret = -1;
	int i,j;
	Block bl;
	Board test_bo = NULL, ret_bo = NULL;
	int idx = -1, ridx = -1;

	DEBUG("IN");

	if (board == NULL) {
		ERROR("the board should not be NULL");
		return ret;
	}

	if (!ht || !que) {
		ERROR("hash table or queue should not be NULL.");
		return ret;
	}

	clearMemo();
	candidate = ht->put(board);
	enQue(que, candidate);

	ret_bo = malloc(BOARD_CELLS);
	memset(ret_bo, 0x00, BOARD_CELLS);

	while (que->size > 0) {
		peek(que, candidate);
		if (candidate != NULL) {
			test_bo = candidate->data->board;
			if (test_bo == NULL) {
				WARN("there has no board in candidate node.");
				continue;
			}
#if FALSE
			YIELD("peek board");
			printBoard(test_bo);
#endif

			if (TRUE) {
				idx = (int)((unsigned char *)index((const char*)test_bo, NB) - test_bo);
				ridx = (int)((unsigned char *)index((const char *)test_bo + idx + 1, NB) - test_bo);

				if (idx < 0 || idx >= BOARD_CELLS ||
					ridx < 0 || ridx >= BOARD_CELLS) {
					ERROR("index went out of range: idx = %d, ridx = %d", idx, ridx);
					return ret;
				}

				memset(clrndblnk, 0x00, sizeof(clrndblnk) * CEL_RND_BLNK);

				if (idx+UP>= 0 && idx+UP<BOARD_CELLS) {
					clrndblnk[0].loc = idx + UP;
					clrndblnk[0].dir = DOWN;
				} else {
					clrndblnk[0].loc = -1;
				}
				if (idx+RIGHT>= 0 && idx+RIGHT<BOARD_CELLS) {
					clrndblnk[1].loc = idx + RIGHT;
					clrndblnk[1].dir = LEFT;
				}
				if (idx+DOWN>= 0 && idx+DOWN<BOARD_CELLS) {
					clrndblnk[2].loc = idx + DOWN;
					clrndblnk[2].dir = UP;
				}
				if (idx+LEFT>= 0 && idx+LEFT<BOARD_CELLS) {
					clrndblnk[3].loc = idx + LEFT;
					clrndblnk[3].dir = RIGHT;
				}
				if (ridx+UP>= 0 && ridx+UP<BOARD_CELLS) {
					clrndblnk[4].loc = ridx + UP;
					clrndblnk[4].dir = DOWN;
				}
				if (ridx+RIGHT>= 0 && ridx+RIGHT<BOARD_CELLS) {
					clrndblnk[5].loc = ridx + RIGHT;
					clrndblnk[5].dir = LEFT;
				}
				if (ridx+DOWN>= 0 && ridx+DOWN<BOARD_CELLS) {
					clrndblnk[6].loc = ridx + DOWN;
					clrndblnk[6].dir = UP;
				}
				if (ridx+LEFT>= 0 && ridx+LEFT<BOARD_CELLS) {
					clrndblnk[7].loc = ridx + LEFT;
					clrndblnk[7].dir = RIGHT;
				}

				for (i=0; i<CEL_RND_BLNK; i++) {
					bl.x = clrndblnk[i].loc / BOARD_COL;
					bl.y = clrndblnk[i].loc % BOARD_COL;

					if (tryMoveBlock(test_bo, ret_bo, bl, clrndblnk[i].dir, 1, FALSE, TRUE, candidate)) {
						DEBUG("can move to %d", clrndblnk[i].dir);
						if (ifWin(ret_bo)) {
							ret = 0;
							break;
						}
					}
				}
			} else {
				for (i=0; i<BOARD_ROW; i++) {
					for (j=0;j<BOARD_COL;j++) {
						DEBUG("try move block %d,%d", i,j);
						bl.x = i;
						bl.y = j;

						if (tryMoveBlock(test_bo, ret_bo, bl, UP, 1, TRUE, TRUE, candidate)) {
							DEBUG("can move up.");
							if (ifWin(ret_bo)) {
								ret = 0;
								break;
							}
						}

						if (tryMoveBlock(test_bo, ret_bo, bl, RIGHT, 1, TRUE, TRUE, candidate)) {
							DEBUG("can move right.");
							if (ifWin(ret_bo)) {
								ret = 0;
								break;
							}
						}

						if (tryMoveBlock(test_bo, ret_bo, bl, DOWN, 1, TRUE, TRUE, candidate)) {
							DEBUG("can move down.");
							if (ifWin(ret_bo)) {
								ret = 0;
								break;
							}
						}

						if (tryMoveBlock(test_bo, ret_bo, bl, LEFT, 1, TRUE, TRUE, candidate)) {
							DEBUG("can move left.");
							if (ifWin(ret_bo)) {
								ret = 0;
								break;
							}
						}
					}
					if (ret == 0) break;
				} // end for
			} // end if(idx)

			if(ret == 0) break;
		} else {
			WARN("got NULL candidate.");
		} // end if(candidate)

		if (ret == 0) break;
		deQue(que);
	} // end while

	if (ret == 0) {
		Node *node = ht->get(ret_bo);
		while (node->data->prev) {
			node->data->prev->data->next = node;
			node = node->data->prev;
			ret ++;
		}
	}

	if (!ret_bo) free(ret_bo);

	DEBUG("out");
	return ret;
}