/* * Function * name : CreatePTree * use : Create a parentTree * @param : PTree *pT * @return : void */ Status CreatePTree(PTree *pT) { /* 操作结果: 构造树T */ LinkQueue q; QElemType p, qq; int i = 1, j, l; char c[MAX_TREE_SIZE]; /* 临时存放孩子结点数组 */ InitQueue(&q); /* 初始化队列 */ printf("请输入根结点: "); scanf("%c", &((*pT)->nodes[0].data)); /* 根结点序号为0,%*c吃掉回车符 */ while (getchar() != '\n'){NULL;} //清空标准输入流 if ((*pT)->nodes[0].data != Nil) /* 非空树 */ { (*pT)->nodes[0].parent = -1; /* 根结点无双亲 */ qq.name = (*pT)->nodes[0].data; qq.num = 0; EnQueue(&q, qq); /* 入队此结点 */ while (i < MAX_TREE_SIZE && !QUEUEEMPTY(q)) /* 数组未满且队不空 */ { DeQueue(&q, &qq); /* 出队一个结点 */ printf("请按长幼顺序输入结点%c的所有孩子: ", qq.name); fgets(c, MAX_TREE_SIZE, stdin); l = strlen(c); for (j = 0; j < l - 1; j++) //多读入了一个换行符 { (*pT)->nodes[i].data = c[j]; (*pT)->nodes[i].parent = qq.num; p.name = c[j]; p.num = i; EnQueue(&q, p); /* 入队此结点 */ i++; } } if (i > MAX_TREE_SIZE) { printf("结点数超过数组容量\n"); exit(-1); } (*pT)->node_count = i; } else { (*pT)->node_count = 0; } return OK; }
//Function use: 从第1个顶点起广度非递归遍历图,并对每个顶点 //调用函数,一旦Visit()失败,则失败 void BFSTraverse(MGraph G, Status (*Visit)(VertexType)) { int nSeqV; int nSeqW; int nSeqU; VertexType u; VertexType w; LinkQueue Q; for (nSeqV = 0; nSeqV < G.vexnum; nSeqV++) { visited[nSeqV] = FALSE; } InitQueue(&Q); for (nSeqV = 0; nSeqV < G.vexnum; nSeqV++) { if (!visited[nSeqV]) { visited[nSeqV] = TRUE; Visit(G.vexs[nSeqV]); EnQueue(&Q, nSeqV); while (!QUEUEEMPTY(Q)) { DeQueue(&Q, &nSeqU); strcpy(u, *GetVex(G, nSeqU)); for (nSeqW = FirstAdjVex(G, u);\ nSeqW >= 0;\ nSeqW = NextAdjVex(G, u, strcpy(w, *GetVex(G, nSeqW)))) { visited[nSeqW] = TRUE; Visit(G.vexs[nSeqW]); EnQueue(&Q, nSeqW); } } } } printf("\n"); }
/* For smart monster movement, build a proximity ripple from the player's position, out to a 'distance' of 20. For example: W 5 4 4 W W X Player is at position marked 1 W 5 W 3 3 W W W is a wall. Monsters will attempt W 6 W 2 W 4 W to move to a location with a smaller W 7 W 1 W 5 W value than their current position. W 8 W W W 6 W Note that a monster at location X W 9 9 8 7 7 7 will not move at all. W W W 8 W W W */ static void build_proximity_ripple(void) { int xl, yl, xh, yh ; int k, m, z, tmpx, tmpy; int curx, cury, curdist; xl=tmp3-2; yl=tmp1-2; xh=tmp4+2; yh=tmp2+2; vxy(&xl,&yl); vxy(&xh,&yh); for (k=yl; k<=yh; k++) for (m=xl; m<=xh; m++) { switch(item[m][k]) { case OWALL: case OPIT: case OTRAPARROW: case ODARTRAP: case OCLOSEDDOOR: case OTRAPDOOR: case OTELEPORTER: screen[m][k]=127; break; case OENTRANCE: if (level==1) screen[m][k] = 127; else screen[m][k] = 0; break; default: screen[m][k] = 0; break; }; } screen[playerx][playery]=1; /* now perform proximity ripple from playerx,playery to monster */ xl=tmp3-1; yl=tmp1-1; xh=tmp4+1; yh=tmp2+1; vxy(&xl,&yl); vxy(&xh,&yh); PUTQUEUE( playerx, playery, 1 ); do { GETQUEUE( curx, cury, curdist ); /* test all spots around the current one being looked at. */ if ( ( curx >= xl && curx <= xh ) && ( cury >= yl && cury <= yh ) ) { for (z=1; z<9; z++) { tmpx = curx + diroffx[z] ; tmpy = cury + diroffy[z] ; vxy( &tmpx, &tmpy ); if (screen[tmpx][tmpy] == 0 ) { screen[tmpx][tmpy] = curdist + 1; PUTQUEUE( tmpx, tmpy, curdist + 1 ); } } } } while (!QUEUEEMPTY()); }