首先,关于这个的基本讨论已过一阶段了。现在就是合成版 以后做mcf java等的界面。 留了 一个bug就是 电脑解决完后 会多打印几个矩阵。 改进的地方,1:增加了一种电脑解法, 2:记录用了自增栈 #include"c.h" #include"c++.h" #define MAX 5 #define N 10 typedef 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; }
关于循环与函数调用的复杂度,以下给一个解:
#include<iostream>
//#include<time.h>
using namespace std;
#define MAX 5
int count = 0;
void Change(int arr[][MAX],int x,int y);
void build_game(int arr[][MAX])
{
arr[0][0] = 1;
arr[0][1] = 1;
arr[1][0] = 1;
srand(NULL);
int i ;
int j ;
int k = rand()%30;
while(k--)
{
i = rand()%MAX;
j = rand()%MAX;
Change(arr,i,j);
}
}
void show(int arr[][MAX])
{
for(int i=0;i<MAX;i++)
{
for(int j=0;j<MAX;j++)
{
cout<<arr[i][j]<< " ";
}
cout<<endl;
}
cout<<endl;
}
void Change(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 search(int arr[][MAX],int n)
{
int k = 0;
while(k < MAX)
{
if(arr[n][k] == 1)
{
return k;
}
k++;
}
return -1;
}
int Click(int arr[][MAX])
{
if(arr[0][0] == 1)
{
Change(arr,0,0);
}
else if(arr[0][MAX-1] == 1)
{
Change(arr,0,MAX-1);
}
else if(arr[MAX-1][0] == 1)
{
Change(arr,MAX-1,0);
}
else if(arr[MAX-1][MAX-1] == 1)
{
Change(arr,MAX-1,MAX-1);
}
else
{
int k = search(arr,0);
if(k == -1)
{
k = search(arr,MAX-1);
if(k == -1)
{
cout<<"已通关"<<endl;
return 0;
}
Change(arr,MAX-1,k);
}
else
{
Change(arr,0,k);
}
}
return 1;
}
void solve(int arr[][MAX])
{
int i = 0;
int j = 0;
int flag = 0;
int FF = 0;
while(1)
{
if(FF != 0)
{
if(Click(arr) == 0)
{
break;
}
count++;
}
show(arr);
if(flag == 0)
{
while(i < MAX-1)
{
j = search(arr,i);
if(j == -1)
{
i++;
continue;
}
Change(arr,i+1,j);
count++;
show(arr);
}
flag = 1;
i = MAX-1;
}
else
{
while(i > 0)
{
j = search(arr,i);
if(j == -1)
{
i--;
continue;
}
Change(arr,i-1,j);
count++;
show(arr);
}
flag = 0;
}
FF = 1;
}
cout<<"所走步数:"<<count<<"步"<<endl;
}
void Menue()
{
cout<<"------------关灯游戏------------"<<endl;
cout<<" 1.开始游戏 "<<endl;
}
void play(int arr[][MAX])
{
build_game(arr);
show(arr);
int x,y;
while(1)
{
cin>>x>>y;
Change(arr,x,y);
show(arr);
}
}
int main()
{
int arr[MAX][MAX] = {};
int x;
int y;
build_game(arr);
show(arr);
solve(arr);
}