Skip to content

Instantly share code, notes, and snippets.

@zuigon
Last active December 28, 2015 23:49
Show Gist options
  • Save zuigon/7581403 to your computer and use it in GitHub Desktop.
Save zuigon/7581403 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <cstdlib>
using namespace std;
int m[3][4];
/*
X 0 X
0 0 0
0 0 0
X 0 X
- unosi brojeve 1 do 8 u matricu na mjesta 0
- dva susjedna (ni dijagonalno) ne smiju biti abs(a, b)==1
*/
void init(){
for(int i=0; i<3; ++i)
for(int j=0; j<4; ++j)
m[i][j]=0;
m[0][0]=m[0][3]=m[2][0]=m[2][3]=-1;
}
bool ok(){
for(int i=0; i<2; ++i)
for(int j=0; j<3; ++j)
if(
abs(m[i][j]-m[i+0][j+0])==1 ||
abs(m[i][j]-m[i+0][j+1])==1 ||
abs(m[i][j]-m[i+1][j+0])==1 ||
abs(m[i][j]-m[i+1][j+1])==1
) return false;
return true;
}
void ispis(){
for(int i=0; i<4; ++i){
for(int j=0; j<3; ++j)
if(m[j][i]==-1) cout<<"X ";
else cout<<m[j][i]<<" ";
cout<<endl;
}
}
void gen(){
int iskoristeni[9]={0}, r, t;
while(1) {
for(int i=0; i<3; ++i)
for(int j=0; j<4; ++j)
if(
!(i==0 && j==0) &&
!(i==0 && j==3) &&
!(i==2 && j==0) &&
!(i==2 && j==3)
){
postavi:
r = rand()%8+1;
if(iskoristeni[r]==1) goto postavi;
m[i][j]=r;
iskoristeni[r]=1;
}
for(int i=1, t=0; i<=8; ++i){
t+=iskoristeni[i];
if(t==8) return;
}
}
}
int main(){
init();
while(1) {
gen();
if(ok()){
cout<<"Rjesenje:"<<endl;
ispis();
break;
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment