点灯游戏的模拟
来源:互联网 发布:电商 双十一 实时数据 编辑:程序博客网 时间:2024/05/22 13:16
留了 一个bug就是 电脑解决完后 会多打印几个矩阵。改进的地方,1:增加了一种电脑解法, 2:记录用了自增栈 #include"c.h"#include"c++.h"#define MAX 5#define N 10typedef int elem_type; class Stack{public: Stack() { top = 0; size = N; data = new elem_type[size]; } ~Stack() { top = 0; delete []data; data = NULL; } void IncSize() { size = 2*size; elem_type *p = new elem_type[size]; memcpy(p,data,sizeof(elem_type)*top); delete []data; data = p; } bool push(elem_type value) { if(isfull()) { IncSize(); } data[top++] = value; return true; } int gettop()//得到栈顶元素 { return data[--top]; } bool isfull()//判断是否已满 { return top == size; } private: elem_type* data; int top; int size;};int show(int arr[][MAX]){ int count=0; for(int i=0;i<MAX;i++) { for(int j=0;j<MAX;j++) { if(arr[i][j]==1)count=1; cout<<arr[i][j]<< " "; } cout<<endl; } cout<<endl; return 0;}void play(int arr[][MAX],int x,int y){ if(x < 0 || y < 0 || x >= MAX || y >= MAX) { return ; } arr[x][y] = 1 -arr[x][y]; if(x-1 >= 0) { arr[x-1][y] = 1 - arr[x-1][y]; } if(x+1 < MAX) { arr[x+1][y] = 1 - arr[x+1][y]; } if(y-1 >= 0) { arr[x][y-1] = 1 - arr[x][y-1]; } if(y+1 < MAX) { arr[x][y+1] = 1 - arr[x][y+1]; }}int cheack(int a[][MAX],int i){ int j = 0,state=0;; while(j++ < MAX) { if(a[i][0] == 1){play(a,i,0);show(a);state=1;} if(a[i][4] == 1){play(a,i,4);show(a);state=1;} if(a[i][j] == 1){ break;} } if(state==0) { play(a,i,j);show(a);state=1;} return state; }int cp2(int a[][MAX]) { int state=1,i=0,j=0; show(a); while(state) { state=cheack(a,i); for(i=0;i<MAX-1;i++) { for(j=0;j<MAX;j++) if(a[i][j]==1){play(a,i+1,j);show(a);} } state=cheack(a,i); for(i=MAX-1;i>0&&state;i--) { for(j=MAX-1;j>=0;j--) if(a[i][j]==1){play(a,i-1,j);show(a);} } state=show(a); } cout << "over" << endl; return 0;} void cp(int v[][MAX],Stack &s){ int y=s.gettop(); int x=s.gettop(); play(v,x,y);show(v); cout<<"电脑显示完毕 系统已退出 "<<endl;} static int cc=1; int player(){ while(cc) { Stack s;//建立一个栈 int v[MAX][MAX] = {0}; int x,y; int k=3,i=0; int state=1; time_t t; srand((unsigned)time(&t)); k=rand()%5+1; cout<<"熄灯前睡觉 欢迎来到第 "<<cc<<" 关"<<endl; cc++;while(i++<k){ x=rand()%5; y=rand()%5; s.push(x); s.push(y); play(v,x,y);} show(v); while( state) { cout<<"熄灯前睡觉 输入坐标(x,y),(0,0)退出"<<endl; cin>>x>>y; if(x==0&&y==0)cc=0; x=x-1;y=y-1; if(x>-1&&x<5&&y>-1&&y<5) { i++; s.push(x); s.push(y); if(i++==100) { cout<<"电脑显示完毕 玩家步数太多,系统已退出 "<<endl;state=0;} play(v,x,y); show(v); } if(x+y>10&&x+y<20){ cp(v,s);state=0;} if(x+y>20)cp2(v); } }}int main(){ cout<<"***************************************"<<endl; cout<<" 欢迎参与点灯游戏 "<<endl<<" 由wzzx提供"<<endl; cout<<" 游戏过程中输入x+y大于10时可以选择电脑演示求解步骤 "<<endl; cout<<"***************************************"<<endl; player(); return 0;}
0 0
- 点灯游戏的模拟
- 点灯游戏
- 点灯游戏
- 5x5点灯游戏的答案
- 点灯游戏算法实现
- 点灯游戏算法实现
- 5X5点灯游戏
- 点灯游戏--一个动态创建组件的示例
- 点灯游戏(还没看,先留着)
- 有趣的js点灯
- 点灯的总结
- dm8168点灯的驱动代码
- STM32的IO配置点灯
- hdu5336 十滴水游戏的模拟
- 猩猩点灯scu2056的解题报告
- 我们的星星点灯(作品展)
- 用zstack实现自己的sample--点灯
- s3c2440的LED点灯裸机汇编程序
- android开发--searchView
- yum错误:Cannot retrieve repository metadata (repomd.xml) for repository解决方法
- 在家如何搭建个服务器
- R语言批量处理TRMM降雨数据
- java110云计算(hadoop)免费培训视频教程
- 点灯游戏的模拟
- 11. Container With Most Water
- 手把手教你安装Hbase,一次成功!
- 复杂单链表的复制
- http详解之telnet
- Android 语音识别
- Android中利用Drawable实现图片循环
- Merge OUTPUT 高级用法综合写的一个MergeTab的存储过程
- SDM 环境配置