小代码 链表实现关灯游戏2
/*cout<<"*****************gamebyringlight**************************"<<endl;cout<<"****一个全开的循环串联灯链为节约资源等待你的关灯动作******"<<endl;cout<<"*****提示:当选择一盏灯时,该灯及旁边的灯状态均会发生变化****"<<endl;cout<<"*****************gamebyringlight**************************"<<endl;破解办法居然简单在于逐一从第一个点开关所有的灯就能实现所有的灯从全亮到全灭*/#include<iostream>usingnamespacestd;structnode{intdata;node*prev;node*next;public:node():data(1),prev(NULL),next(NULL){}node(inta):data(a),prev(NULL),next(NULL){}};voidcreat(node*list){intn=20;node*tmp;node*tail;while(n--){if(list->next==NULL){tmp=newnode(1);tmp->prev=list;tmp->next=list;list->prev=tmp;list->next=tmp;tail=tmp;}else{tmp=newnode(1);list->next->prev=tmp;tmp->next=list->next;tmp->prev=tail;tail->next=tmp;list->next=tmp;}}}voidshow(node*list){intn=20;node*p=list->next;while(n--){//cout<<"["<<20-n<<"]"<<p->data<<"";cout<<p->data<<"";p=p->next;}cout<<endl;}void_play(node*list,intx){node*p=list->next;while(x---1){p=p->next;}p->prev->data=1-p->prev->data;p->data=1-p->data;p->next->data=1-p->next->data;}intcheck(node*list){intn=20;node*p=list->next;while(n--){if(p->data==1)break;p=p->next;}return1-p->data;}voidplay(node*list){intx=1;intn=40;while(n--){cout<<"40步解灯谜之"<<40-n<<"步:(输入0~20)"<<endl<<'\t'<<'\t';cin>>x;_play(list,x);show(list);if(n==20&&check(list))break;}}intmain(){cout<<"*****************gamebyringlight**************************"<<endl;cout<<"****一个全开的循环串联灯链为节约资源等待你的关灯动作******"<<endl;cout<<"*****提示:当选择一盏灯时,该灯及旁边的灯状态均会发生变化****"<<endl;cout<<"*****************gamebyringlight**************************"<<endl;nodelist;creat(&list);show(&list);play(&list);show(&list);cout<<"wzzx"<<endl;return0;}
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。