示例#1
0
void
push(struct EACH item)
{
	int cld, pt;
	++count;
	queue[count-1] = item;
	up_heapify(count-1);
}
示例#2
0
void
Astar(char *start_str, int start_pos)
{
	int i, th, hvalue, curp, nxtp;
	char nxt[STR_LEN+1];
	struct EACH cur, tmp;
	strcpy(queue[0].str, start_str);
	queue[0].pos = start_pos;
	queue[0].value = hash_func(start_str);
	count = 1;
	mark[queue[0].value] = 1; /* open list */
	g[queue[0].value] = 0;
	h[queue[0].value] = estimate(start_str);
	pre[queue[0].value] = -1;
	dir[queue[0].value] = -1;
	idx[queue[0].value] = 0;
	while(count != 0) {
		cur = pop();
		if(cur.value == target) {
			output(target);
			printf("\n");
			return;
		}
		mark[cur.value] = 2; /* add to closed-list */
		curp = cur.pos;
		for(i=0; i<4; i++) {
			if(movable[curp][i]) {
				nxtp = curp + pos[i];
				strcpy(nxt, cur.str);
				nxt[curp] = nxt[nxtp];
				nxt[nxtp] = '0';
				hvalue = hash_func(nxt);
				if(mark[hvalue]==1 && g[hvalue]>(g[cur.value]+1)) { /* update */
					g[hvalue] = g[cur.value]+1;
					pre[hvalue] = cur.value;
					dir[hvalue] = i;
					up_heapify(idx[hvalue]);
				} else if(mark[hvalue]==0) { /* add to open-list */
					strcpy(tmp.str, nxt);
					tmp.pos = nxtp;
					tmp.value = hvalue;
					mark[hvalue] = 1;
					g[hvalue] = g[cur.value]+1;
					h[hvalue] = estimate(nxt);
					pre[hvalue] = cur.value;
					dir[hvalue] = i;
					idx[hvalue] = count;
					push(tmp);
				}
			}
		}
	}
	printf("unsolvable\n");
}
示例#3
0
void up_heapify(int u){
	int v = parent(u);
	if(v != -1 && D[H[u]] < D[H[v]]){ // u is not the root of the heap
		int tmp = H[u];
		H[u] = H[v];
		pos[H[v]] = u;		// update the position of node H[v] in the Heap
		H[v] = tmp;
		pos[tmp] = v;		// update the position of node tmp in the Heap
		up_heapify(v);
	}
}
示例#4
0
void decrease_key(int u, int k){
	D[u] = k;
	up_heapify(pos[u]);
}