The ultimate version of the answer to sleep please turn off the light

Source: Internet
Author: User

First of all, the basic discussion about this has gone through a phase. Now is the synthetic version of the interface to do the MCF Java and so on.

Left a bug is that the computer after the completion of the printing of more than a few matrices.

Improved place, 1: Added a computer solution,

2: The self-increment stack is used for recording.


   #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);d Elete []data;data = p; }bool push (elem_type  value) {if (Isfull ()) {incsize ();} Data[top++] = value;return true;}  int gettop ()//get stack top element {return data[--top];            }bool isfull ()//determine if full {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;  &NBSP;&NBSP;}INT&NBSP;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;} &NBSP;&NBSP;&NBSP;&NBSP;VOID&NBSP;CP (int v[][max],stack &s] {        int y=s.gettop ();     int x=s.gettop ();     play (v,x,y); show (v);   cout<< " Computer display finished   system has exited   "&LT;&LT;ENDL;}  static  int  cc=1;int player () {     while (cc)        {   stack s;//Build a stack       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<< "Sleep before lights out    Welcome to section   <<cc<<"   Off "<<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<< " Sleep before lights out   Enter coordinates (x, y), (0,0) exit "<<endl;   cin>>x>>y;      if (x==0&&y==0) cc=0;    x=x-1;y=y-1;    if (x>-1& &AMP;X&LT;5&AMP;&AMP;Y&GT;-1&AMP;&AMP;Y&LT;5)     {   i++; s.push (x);  s.push (y);       if (i++==100)        {  cout<< "Computer display    player steps too many, system has exited  " <<endl;state=0;}        play (v,x,y);   show (v);       }                      if (x+y>10&&x+y<20) {&NBSP;CP (v,s); state=0;}                  if (x+y &GT;20) Cp2 (v);  }         }}int main () {    cout<< "***************************************" <<endl;   cout<< "            Welcome to participate in the lighting game       <<endl< < "               provided by Wzzx" <<endl;    cout<< "    when you enter x+y greater than 10 during gameplay, you can select the computer demonstration solution Step   " <<endl;    cout<< "***************************************" <<endl;   player ();  return 0;}

About the complexity of loops and function calls, here's a solution:

#include <iostream>//#include <time.h>using namespace std; #define   MAX   5int 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 == &NBSP;-1) {cout<< "customs clearance" &LT;&LT;ENDL;RETURN&NBSP;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&Nbsp;= 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<< "Walk Number:" <<count<< "Step" &LT;&LT;ENDL;} Void menue () {cout<< "------------off the Lights game------------" <<endl;cout<< "           1. Start Game               "&LT;&LT;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);} 

Http://mathworld.wolfram.com/LightsOutPuzzle.html a record a little English, have time to translate

See! You have a math around you! -Lighting Game: 8 years late beautiful, mathematically drawn "two-dimensional code"! (Share insight net) 650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/ Zh-cn/images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>http:// zhuanlan.zhihu.com/mathplusplus/19820908


The ultimate version of the answer to sleep please turn off the light

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.