a da se pouzit i pod windows?
Příspěvky odeslané z IP adresy 90.183.137.–
Jay
Jay
Ja mam ted tohle, funguje to dobre pro 3x3, ale z nepochopitelnych duvodu to spadne pri vyssich rozmerech, nevite proc?
#include <cstdlib>
#include <stack>
#include <queue>
#include <iostream>
using namespace std;
#define COLUMNS 3
#define ROWS 3
/* ************************************ */
/* Trida Field - predstavuje hraci pole */
/* ************************************ */
class Field {
public:
bool gamefield[COLUMNS][ROWS];
stack<pair<int,int> > steps;
Field();
Field(bool startfield[COLUMNS][ROWS]);
Field(const Field& f);
void lightswitch(int x, int y);
bool isWin();
void display();
};
Field::Field() {
for(int i=0;i<COLUMNS;i++) {
for(int j=0;j<ROWS;j++) {
this->gamefield[i][j]=false;
}
}
}
Field::Field(const Field& f) {
for(int i=0;i<COLUMNS;i++) {
for(int j=0;j<ROWS;j++) {
this->gamefield[i][j]=f.gamefield[i][j];
}
}
}
void Field::display() {
cout << endl << endl;
cout << "Aktualni stav" << endl << endl;
for(int i=0;i<COLUMNS;i++) {
for(int j=0;j<ROWS;j++) {
if(this->gamefield[i][j]==true) cout << "o ";
else cout << "x ";
}
cout << endl;
}
cout << endl << endl;
}
void Field::lightswitch(int x, int y) {
if(x-1>=0) {
gamefield[x-1][y]=!gamefield[x-1][y];
}
if(x+1<=COLUMNS) {
gamefield[x+1][y]=!gamefield[x+1][y];
}
if(y-1>=0) {
gamefield[x][y-1]=!gamefield[x][y-1];
}
if(y+1<=ROWS) {
gamefield[x][y+1]=!gamefield[x][y+1];
}
gamefield[x][y]=!gamefield[x][y];
}
bool Field::isWin() {
bool result = true;
for(int i=0;i<COLUMNS;i++) {
for(int j=0;j<ROWS;j++) {
if(this->gamefield[i][j]==true) {
result = false;
break;
}
}
}
return result;
}
/* ************************************ */
/* Metoda solve */
/* ************************************ */
void solve(Field currentState) {
int counter = 0;
Field newState = currentState;
queue<Field> q;
while(!newState.isWin()) {
for(int i=0;i<COLUMNS;i++) {
for(int j=0;j<ROWS;j++) {
Field nextState = newState;
nextState.lightswitch(i,j);
nextState.steps.push(make_pair(i,j));
q.push(nextState);
}
}
newState = q.front();
counter ++;
q.pop();
}
cout << "Pocet kroku: " << counter << endl << endl;
}
/* ************************************ */
/* Main */
/* ************************************ */
int main(int argc, char *argv[])
{
Field start;
/* nejklasictejsi ukazka, melo by jit vyresit na dva tahy */
start.gamefield[0][1]=true;
start.gamefield[1][0]=true;
start.gamefield[1][1]=true;
start.gamefield[2][2]=true;
start.display();
solve(start);
system("PAUSE");
return EXIT_SUCCESS;
}
Jay
Tak uz mam to "jadro" programu - dvourozmerne pole objektu (kazda instance pro jedno svetlo). Funkce switchlight prepne prislusna sousedni svetla. Nevedel by nekdo jak ted na ten algoritmus? Na zacatku si nastavim nejakou pocatecni konfiguraci a pote nad tim prozenu ten algoritmus tak, aby zhasla vsechna svetla.
#include <cstdlib>
#include <iostream>
using namespace std;
#define COLUMNS 4
#define ROWS 4
class Light {
public:
bool value;
int x;
int y;
void lightswitch();
};
Light **field;
void Light::lightswitch() {
if(x-1>=0) {
if(field[x-1][y].value==false) field[x-1][y].value=true;
else field[x-1][y].value=false;
}
if(x+1<=COLUMNS) {
if(field[x+1][y].value==false) field[x+1][y].value=true;
else field[x+1][y].value=false;
}
if(y-1>=0) {
if(field[x][y-1].value==false) field[x][y-1].value=true;
else field[x][y-1].value=false;
}
if(y+1<=ROWS) {
if(field[x][y+1].value==false) field[x][y+1].value=true;
else field[x][y+1].value=false;
}
if(value==false) value=true;
else value=false;
}
void display() {
cout << endl << endl << "Aktualni stav:" << endl << endl;
for(int i=0;i<COLUMNS;i++) {
for(int j=0;j<ROWS;j++) {
if(field[i][j].value==false) cout << "x ";
else cout << "o ";
}
cout << endl;
}
cout << endl << endl;
}
int main(int argc, char *argv[])
{
field = new Light*[COLUMNS];
for(int i=0;i<COLUMNS;i++) {
field[i] = new Light[ROWS];
}
for(int i=0;i<COLUMNS;i++) {
for(int j=0;j<ROWS;j++) {
field[i][j].value=false;
field[i][j].x=i;
field[i][j].y=j;
}
}
/* pocatecni konfigurace */
field[1][1].value=true;
field[2][0].value=true;
field[3][2].value=true;
/* ********************* */
display();
field[1][1].lightswitch();
display();
system("PAUSE");
return EXIT_SUCCESS;
}
Honza
Zdravím,
Zkoušel jsem googlit, ale dozvěděl jsem se, že to nejde nebo že se to dá nějak obejít, tak nevím. Nevíte jestli se dá v Oraclu použít trigger nad více tabulkama?
Díky,
H.