stack* toQueue(Queue* q) { stack* s = stack_init(1); Postion* p = QueueGet(q); while(p != 0) { stack_push(s, p); p = QueueGet(q); } return s; }
LW_RTOS_RESULT lw_QueueGet (tQueueHandle QueueId, void * pItemBuffer, tTicks Timeout) { if (QueueGet ( (tQueueHeader *)QueueId, pItemBuffer)) return LWR_OK; else return LWR_ERROR; }
int branchAndBound(int myMaze[][9], Postion start, Postion end) { int ok = 0; Queue* q = tk_calloc(1, sizeof(Queue)); Postion* tmp = tk_calloc(1, sizeof(Postion)); stack* s = stack_init(1); tmp->x = start.x; tmp->y = start.y; myMaze[start.y][start.x] = BASEVAULE; while(tmp != 0) { stack_push(s, tmp); if (dealNodeR(myMaze, q, tmp, start, end) == 1) { ok = 1; break; } tmp = QueueGet(q); } if (ok == 1) { markPoit(myMaze, s, end); stack_destory(s); } QueueDestory(q); tk_bookkeeper_mem_report(); return ok; }
/* BFS: Breadth First Search */ int GetMinRingSize( inp_ATOM* atom, QUEUE *q, AT_RANK *nAtomLevel, S_CHAR *cSource, AT_RANK nMaxRingSize ) { int qLen, i, j; AT_RANK nCurLevel, nRingSize, nMinRingSize=MAX_ATOMS+1; qInt at_no, next; int iat_no, inext; while ( qLen = QueueLength( q ) ) { /* traverse the next level (next outer ring) */ for ( i = 0; i < qLen; i ++ ) { if ( 0 <= QueueGet( q, &at_no ) ) { iat_no = (int)at_no; nCurLevel = nAtomLevel[iat_no] + 1; if ( 2*nCurLevel > nMaxRingSize + 4 ) { /* 2*nCurLevel = nRingSize + 3 + k, k = 0 or 1 */ if ( nMinRingSize < MAX_ATOMS+1 ) { return (nMinRingSize >= nMaxRingSize)? 0 : nMinRingSize; } return 0; /* min. ring size > nMaxRingSize */ } for ( j = 0; j < atom[iat_no].valence; j ++ ) { next = (qInt)atom[iat_no].neighbor[j]; inext = (int)next; if ( !nAtomLevel[inext] ) { /* the at_no neighbor has not been traversed yet. Add it to the queue */ if ( 0 <= QueueAdd( q, &next ) ) { nAtomLevel[inext] = nCurLevel; cSource[inext] = cSource[iat_no]; /* keep the path number */ } else { return -1; /* error */ } } else if ( nAtomLevel[inext]+1 >= nCurLevel && cSource[inext] != cSource[iat_no] /* && cSource[(int)next] != -1 */ ) { /* found a ring closure */ /* debug */ if ( cSource[inext] == -1 ) { return -1; /* error */ } if ( (nRingSize = nAtomLevel[inext] + nCurLevel - 2) < nMinRingSize ) { nMinRingSize = nRingSize; } /* return (nRingSize >= nMaxRingSize)? 0 : nRingSize; */ } } } else { return -1; /* error */ } } } if ( nMinRingSize < MAX_ATOMS+1 ) { return (nMinRingSize >= nMaxRingSize)? 0 : nMinRingSize; } return 0; }
/* Give the level-order travel number to each node */ void BFS1() { NODE *pNode; LISTNODE *pList; QueuePut(root); while ( ! QueueEmpty() ) { pNode = QueueGet(); pNode->nodeno = node_count++; for ( pList = pNode->childList; pList; pList = pList->next ) { QueuePut( pList->pNode ); } } }
void markPoit(int myMaze[][9], stack* s, Postion end) { Postion* p = stack_pop(s); Queue* q = QueueInit(); Postion current = end; while(p != 0) { if (isNear(*p, current) == 1) { myMaze[p->y][p->x] = 1; current = *p; QueueAdd(q, p); } else { tk_free(p); } p = stack_pop(s); } p = QueueGet(q); printf("(5,5)"); while(p != 0) { printf("<-(%d,%d)", p->x, p->y); tk_free(p); p = QueueGet(q); } printf("\n"); QueueDestory(q); }
// Поиск в ширину (FIFO) int TreeSearch(){ int i,j; node *n = NodeCreate(start_i,start_j,NULL,0,NOTHING); array arr; queue *h = QueueCreate(); QueueAdd(h,n); while(!QueueIsEmpty(h)){ n = QueueGet(h); if(NodeIsFinish(n)){ while(n->parent != NULL){ NodePrint(n); field[n->state[0]][n->state[1]] = USED; n = n->parent; } field[n->state[0]][n->state[1]] = USED; printf("\nКарта пути:\n"); printf("\"X\" - Препятствие\n"); printf("\".\" - Свободная ячейка\n"); printf("\"*\" - Путь робота\n\n"); for(i = 0; i<SIZE; i++){ for(j = 0; j<SIZE; j++){ if(field[i][j] == FREE)printf("."); if(field[i][j] == OBSTACLE)printf("X"); if(field[i][j] == USED)printf("*"); } printf("\n"); } free(h); return 0; } //если решение еще не нашлось, то заново записываем переферию arr = Expand(n); for(i = 0; i < arr.size; i++){ QueueAdd(h, arr.arr[i]); } } printf("Can't find solution\n"); free(h); return 0; }
/** * Modification of I/O scheme: * This tree is generated after libchewing * construction, so the size of tree must be * obtained from size of file returned by * plat_mmap_create() and sizeof(PHLTreeType). */ void BFS2(const char* bin_name) { NODE *pNode; LISTNODE *pList; PHLTreeType tree={0}; FILE* output; #ifdef USE_BINARY_DATA output=fopen(PHL_KEY_TREE_FILE, "wb"); #else output=fopen(PHL_KEY_TREE_FILE, "w"); #endif if(!output){ fprintf(stderr, "%s: Cannot open " PHL_KEY_TREE_FILE " for output.\n", bin_name); exit(1); } QueuePut(root); tree_size=0; while(!QueueEmpty()){ pNode = QueueGet(); tree.keyin_id=pNode->key; tree.phrase_id=pNode->phraseno; /* compute the begin and end index */ pList=pNode->childList; if(pList){ tree.child_begin = pList->pNode->nodeno; for(; pList->next; pList=pList->next) QueuePut(pList->pNode); QueuePut( pList->pNode ); tree.child_end = pList->pNode->nodeno; } else tree.child_begin=tree.child_end=-1; #ifdef USE_BINARY_DATA fwrite(&tree, sizeof(PHLTreeType), 1, output); #else fprintf(output, "%lu %d %d %d\n", tree.keyin_id, tree.phrase_id, tree.child_begin, tree.child_end); #endif tree_size++; } fclose(output); }
/** * blink LED, send data on queue */ void ProcessIO(void) { BlinkUSBStatus(); if ((USBDeviceState < CONFIGURED_STATE) || (USBSuspendControl == 1)) { return; } if (USBUSARTIsTxTrfReady()) { int len = 0; char temp[QUEUE_SIZE]; while (QueueHas()) { temp[len++] = QueueGet(); } if (len > 0) { putUSBUSART(temp, len); } } CDCTxService(); }