bool CheckRule(const char tb[9][9],const int rule[9][9])
{
bool chk[9][9]={{false}};
for(int y=0;y<9;++y)
{
for(int x=0;x<9;++x)
{
char ch=tb[y][x];
if(('0'<=ch)&&(ch<='9'))
{
int row=rule[y][x],col=ch-'0';
if(chk[row][col]) return false;
chk[row][col]=true;
}
}
}
return true;
}
bool CheckSqares(const char tb[9][9])
{
static const int rule[9][9]=
{
{0,0,0,1,1,1,2,2,2},
{0,0,0,1,1,1,2,2,2},
{0,0,0,1,1,1,2,2,2},
{3,3,3,4,4,4,5,5,5},
{3,3,3,4,4,4,5,5,5},
{3,3,3,4,4,4,5,5,5},
{6,6,6,7,7,7,8,8,8},
{6,6,6,7,7,7,8,8,8},
{6,6,6,7,7,7,8,8,8},
};
return CheckRule(tb,rule);
}
bool CheckRows(const char tb[9][9])
{
static const int rule[9][9]=
{
{0,0,0,0,0,0,0,0,0},
{1,1,1,1,1,1,1,1,1},
{2,2,2,2,2,2,2,2,2},
{3,3,3,3,3,3,3,3,3},
{4,4,4,4,4,4,4,4,4},
{5,5,5,5,5,5,5,5,5},
{6,6,6,6,6,6,6,6,6},
{7,7,7,7,7,7,7,7,7},
{8,8,8,8,8,8,8,8,8},
};
return CheckRule(tb,rule);
}
@MarekR22, Ja tu widzę tylko dwie pętli, ba ta sama funkcja sprawdzi również wiersze kolumny i przekątne!
Ewentualnie 3 pętli, ale sprawdza wszystkie reguły na raz:
bool CheckAllRules(const char tb[9][9])
{
struct range
{
int lf,rt;
bool contain(int v)const { return (lf<=v)&&(v<=rt); }
};
struct rule
{
range rx,ry;
bool contain(int x,int y)const { return ((rx.contain(x))&&(ry.contain(y))); }
};
static const vector<rule> rules
{
{
// Squares
{{0,2},{0,2}},{{0,2},{3,5}},{{0,2},{6,8}},
{{3,5},{0,2}},{{3,5},{3,5}},{{3,5},{6,8}},
{{6,8},{0,2}},{{6,8},{3,5}},{{6,8},{6,8}},
//Cols
{{0,0},{0,8}},{{1,1},{0,8}},{{2,2},{0,8}},
{{3,3},{0,8}},{{4,4},{0,8}},{{5,5},{0,8}},
{{6,6},{0,8}},{{7,7},{0,8}},{{8,8},{0,8}},
}
};
vector<unsigned short> chk(rules.size());
for(int y=0;y<9;++y)
{
for(int x=0;x<9;++x)
{
char ch=tb[y][x];
if(('0'<=ch)&&(ch<='9'))
{
int pos=(ch-'0'),mask=(1<<pos);
for(int r=0;r<rules.size();++r)
{
if(rules[r].contain(x,y))
{
if(chk[r]&mask) return false;
chk[r]|=mask;
}
}
}
}
}
return true;
}