阅读背景:

变量事件的值当不对它们进行操作时

来源:互联网 

I have this code:

我有这个代码:

#include <iostream>
#include<cstdio>
#include<conio.h>
#include<windows.h>
#define BOARDSIZE 3
using namespace std;

int board[BOARDSIZE][BOARDSIZE];
int cnt = 0;
int minCount = BOARDSIZE*BOARDSIZE;

void boardSwitch(int x, int y){
    for(int i = x-1; i <= x+1; i++){
        for(int j = y-1; j <= y+1; j++){
            if((i>=0) && (j>=0)){
                board[i][j] = board[i][j] ^ 1;
            }
        }
    }
}

bool isOn(){
    int i, j;

    for(i = 0; i < BOARDSIZE; i++){
        for(j = 0; j < BOARDSIZE; j++){
            if(!board[i][j]){
                return 0;
            }
        }
    }
    return 1;
}

void boardDisp(){
    for(int i = 0; i <BOARDSIZE; i++){
        for(int j = 0; j < BOARDSIZE; j++){
            cout<<board[i][j]<<" ";
        }
        cout<<endl;
    }
}

void turn(int x){
    int i, j;

    j = x%BOARDSIZE;
    i = (x-j)/BOARDSIZE;
    if(x == 0){
        cnt = 0;
    }
    for(int k = 0; k <=1; k++){
        printf("%d \n", cnt);
        cout<<i<<" "<<j<<" cnt = "<<cnt<<endl;
        //getch();
        if(k == 1){
            boardSwitch(i, j);  //switch on (i,j) position
            //cout<<i<<" "<<j<<" cnt = "<<cnt<<endl;
            //cnt++;
            //cout<<"switch on "<<i<<" "<<j<<" cnt = " <<cnt<<endl;
        }
        if(x == BOARDSIZE*BOARDSIZE-1){
//          if(isOn()){
//              minCount = minCount < cnt ? minCount : cnt;
//          }
        }
        else{
            turn(x+1);
        }
    }
    boardSwitch(i, j);  // return status before switch
    //cnt--;
}


int main(){
    for(int i = 0; i < BOARDSIZE; i++){
        for(int j = 0; j < BOARDSIZE; j++){
            board[i][j] = rand()%2;     }
    }
    boardDisp();
    turn(0);
    if(minCount == BOARDSIZE*BOARDSIZE){
        //There is no way to turn board on
        cout<<"There is no way to turn board on";
    }else{
        cout<<minCount;
    }
    return 0;
}
#include <iostrea



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: