C语言队列写连连看心得
来源:互联网 发布:开淘宝店考试答案 编辑:程序博客网 时间:2024/05/07 14:44
堆栈
类似于链表 先进后出 所以只需要操作头指针
队列
同样类似于链表 从尾指针加入 从头指针出 所以需要两个
void InitQueue()//初始化队列{pqueue = (Queue*)malloc(sizeof(Queue));if(pqueue != NULL){pqueue->front=NULL;pqueue->rear=NULL;pqueue->size=0;}return;}PNode EnQueue(PNode pnode)//队列加入元素{if(pnode != NULL){pnode->next = NULL;if(IsEmpty()){pqueue->front = pnode;}else{pqueue->rear->next = pnode;}pqueue->rear=pnode;pqueue->size++;}return pnode;}PNode DeQueue()//删除元素{PNode pnode= pqueue->front;if(!IsEmpty()){pqueue->size--;pqueue->front = pnode->next;free(pnode);if(pqueue->size == 0)pqueue->rear = NULL;}return pqueue->front;}int IsEmpty()//判断是否为空{if(pqueue->rear == NULL && pqueue->size ==0){return 1;}else return 0;}void ClearQueue()//清空队列{PNode temp;PNode pnode= pqueue->front;while(IsEmpty() != 1){pqueue->size--;temp = pnode;//这里特别注意 先把要free的指针赋值出来 再free 不然容易引起空指针错误pnode = pnode->next;free(temp);if(pqueue->size == 0)pqueue->rear = NULL;}}
堆栈和队列同样有顺序储存结构 类似于数组
做练练看的时候
int g_iMap[8][8];//管理精灵的地图int v[8][8];//管理精灵转折步骤的地图int g_iFirst[2]={-1,-1};int g_iState=0;int move[4][2]={{1,0},{0,1},{-1,0},{0,-1}};//移动方向 下右上左
关键是算法
int run(int a,int b, int c, int d){if(g_iMap[a][b] != g_iMap[c][d] || (a==c && b==d)){return 0;}int i,x,y;if(!IsEmpty()){ClearQueue();//清空队列}for(i=0; i<4; i++){x =a + move[i][0];y =b + move[i][1];if(x<=-1 || y<=-1){continue;}if(x==c && y==d && g_iMap[a][b]==g_iMap[c][d]){g_iWin =1;return 1;}if(x<=7 && x>=0 && y<=7 && y>=0 && g_iMap[x][y]==-1){PNode pnode=(PNode)malloc(sizeof(Node));pnode->iPosX = x;pnode->iPosY = y;pnode->iStep = 0;pnode->next=NULL;pnode->iDirect = i;EnQueue(pnode);v[x][y] =0;}}while(!IsEmpty()){PNode node = pqueue->front;for(i=0; i<4; i++){x =node->iPosX + move[i][0];y =node->iPosY + move[i][1];if(x<=-1 || y<=-1){continue;}if(x==c && y==d ){if(node->iDirect != i && node->iStep >=2){continue;}g_iWin =1;return 1;}if(x<=7 && x>=0 && y<=7 && y>=0 && g_iMap[y][x] == -1){if(i == node->iDirect && (v[x][y] == -1 || v[x][y] >= node->iStep)){PNode pnode=(PNode)malloc(sizeof(Node));pnode->iPosX = x;pnode->iPosY = y;pnode->next=NULL;pnode->iStep=node->iStep;pnode->iDirect = i;EnQueue(pnode);v[x][y]=node->iStep;}else if(node->iStep<2 && (v[x][y] == -1 || v[x][y] >=node->iStep)){PNode pnode=(PNode)malloc(sizeof(Node));pnode->iPosX = x;pnode->iPosY = y;pnode->next=NULL;pnode->iStep=node->iStep+1;pnode->iDirect = i;EnQueue(pnode);v[x][y]=node->iStep+1;}}}DeQueue();}return 0 ;}
写代码的的时候注意先进行判断 把不符合条件的直接返回
然后注意不越界 还有空指针
0 0
- C语言队列写连连看心得
- 连连看写好了
- [Qt C++] 连连看
- 连连看(bfs ,优先队列搜索)
- hdu1175 连连看 (BFS+优先队列)
- HDU 1175 连连看 bfs + 优先队列
- VC写的QQ连连看外挂
- JAVA写的"连连看",一起学习!
- 试用cocos2dx 3.0写连连看
- c语言写的循环队列
- c语言写的循环队列
- c语言写螺旋队列并分析
- C语言:写一个循环队列
- Java 游戏 连连看 语言编写 多媒体
- Java语言实现数字连连看源码
- bnu1058 连连看游戏 C语言版
- 【hdu 1175】 连连看 题解 (c++)
- java连连看源码分享(原来连连看这么简单,你也可以写)
- 设计模式:享元模式
- 中电信推智慧家庭“悦me”产品 融入Xbox One
- 每天学习八小时以下是不道德的(牛人的语言学习历程)--转
- python unittest单元测试方法和用例
- Android数据的四种存储方式SharedPreferences、SQLite、Content Provider和File
- C语言队列写连连看心得
- 使用echo $PATH来解决bash: command not found
- Q13.5
- 全新智能扫地机器人的清洁秘诀!
- Ubuntu将windows作为第一启动项
- Maven搭建hadoop环境报Missing artifact jdk.tools:jdk.tools:jar:1.6
- Shell脚本得到上一个工作日
- 图论最短路径 Dijkstra算法和模板
- MySql5.6 的配置文件路径(For windows - my.ini)