ZOJ2771题解
来源:互联网 发布:根据小说改编的网络剧 编辑:程序博客网 时间:2024/04/30 01:40
#include<stdio.h>#include<stdlib.h>typedef struct node{ int data; int floor; int flag; }node;typedef struct QNode{ node data; struct QNode *next; }QNode,*QueueP;typedef struct { QueueP font; QueueP rear; }LinkQueue;LinkQueue Q;void InitQueue(LinkQueue &Q){ Q.font=Q.rear=(QueueP )malloc(sizeof(QNode)); Q.font->next=NULL; }void EnQueue(LinkQueue &Q,node e){ QueueP p; p=(QueueP )malloc(sizeof(QNode)); p->data=e; p->next=NULL; Q.rear->next=p; Q.rear=p; }void DeQueue(LinkQueue &Q,node &head){ QueueP p; p=Q.font->next; head=p->data; Q.font->next=p->next; if(Q.rear==p)Q.rear=Q.font;//如果队列中的最后一个元素被删除的时候,队尾指针也会被删除所以要将队头指针赋给队尾,然后再释放p free(p); }int QueueEmpty(LinkQueue Q){ if(Q.font==Q.rear)return 1; else return 0; }void DataInit(void){ node temp; for(int i=2;i<5;i++){ temp.data=i; temp.floor=1; temp.flag=1; EnQueue(Q,temp); } } void Mindata(node e){ int temp=e.data; node temp1; while(temp>1){ temp1.data=--temp; temp1.floor=e.floor+1; temp1.flag=0; EnQueue(Q,temp1); } }void Maxdata(node e){ int temp=e.data; node temp1; while(temp<4){ temp1.data=++temp; temp1.floor=e.floor+1; temp1.flag=1; EnQueue(Q,temp1); } }void ChoiceData(node e){ if(e.flag)Mindata(e); else Maxdata(e); }int main(){ int t; long long count; while(scanf("%d",&t)!=EOF){ count=0; node head; InitQueue(Q); if(t==0)printf("1\n"); else if(t==1)printf("3\n"); else if(t>1){ DataInit(); DeQueue(Q,head); while(head.floor<=t){ if(head.floor==t) count++; ChoiceData(head); DeQueue(Q,head); } printf("%d\n",count); } } }
上面用的是搜索的算法,这几天不是在学搜索嘛,所以还是用搜索写的,但是参考别人的时候才知道这个用DP来写的,虽然我写的这个是超时的,运算的速度比较慢,但是结果什么的还是很正确的。
现在还没有真正的懂得动态规划的写法,所以自觉这样正是一个好的引导过程,让我明白动态规划的优越性,一步一步的让自己往那个方面努力,因为自己本来就不太喜欢那种一直灌输性的知识,总是喜欢那种探索式的前进,基于这个想法,我觉得这个代码的意义还是蛮大的,至少让我明白了,搜索并不是适用用任何问题,效率问题有必要考虑。
- ZOJ2771题解
- ZOJ2771 Get Out of the Glass
- 题解
- 题解
- 题解
- 题解
- 题解
- 题解
- 题解
- 题解
- 题解
- 题解
- 题解
- 题解~~~~
- 题解。。。。
- 题解
- 题解
- 1002 题解
- linux下的C语言开发(线程等待)
- VC6.0 中文显示乱码的解决方法
- linux下的C语言开发(线程互斥)
- linux下的C语言开发(网络编程)
- 图片资源中总会出现thumb.db文件
- ZOJ2771题解
- dl-provider.com/MediaCrawler 一个坑爹的网站
- PMON中autoload自动加载操作系统代码分析
- shell 中的find -mtime
- Servlet 工作原理解析
- Hadoop之combiner和partitioner
- 重温CSS系列(一)
- sql2008+vs2008安装心得以及详细教程
- 动态调用用dll封装的控件