c++的解答
供找不到方向的人參考
禁止直接拷貝
#include<iostream>
using namespace std;
bool ls[9]={};
int mt[9][9];
/////////////////////////////////
bool cinmt();
//////////
bool chm();
bool chn();
bool chsq();
//////
void list0();
bool list_check();
void list_write(int);
void listdp();
//////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////
int main()
{
while(cinmt())
{
if(chm()&&chn()&&chsq()) cout<<"yes"<<endl;
else cout<<"no"<<endl;
}
return 0;
}
///////////////////////////////////////////////
///////////////////////////////////////////////
bool cinmt() // 輸入數獨
{
int k,i=0;
while(i<81&&cin>>k)
{
mt[i/9][i%9]=k;
i++;
}
if(i!=81)return 0; //<-如果遇到 結束代碼 而提早跳出 則 i 就不為81
else return 1;
}
//////////////////////////////////
bool chm() //檢查列
{
for(int i=0;i<9;i++)
{
for(int j=0;j<9;j++)
{
list_write(mt[i][j]);
}
if(!list_check())return 0;
list0();
}
return 1;
}
////////////////
bool chn()//檢查行
{
for(int i=0;i<9;i++)
{
for(int j=0;j<9;j++)
{
list_write(mt[j][i]);
}
if(!list_check())return 0;
list0();
}
return 1;
}
/////////////////
bool chsq() //檢查小方框
{
for(int i=0;i<9;i++)
{
for(int j=0;j<9;j++)
{
list_write(mt[i/3*3+j/3][i%3*3+j%3]);
}
if(!list_check())return 0;
list0();
}
return 1;
}
////////////////////////////////// ls: Y Y Y Y N N Y Y Y
// - - - - - - - - -
void list0() //清單歸零 // 1 2 3 4 5 6 7 8 9
{ // 代表數字是否出現過
for(int i=0;i<9;i++) ls[i]=0; // 0號位代表數字1
}
///////////檢查所有數字是否都出現過
bool list_check()//1合格
{
for(int i=0;i<9;i++)
if(ls[i]==0) return 0;
return 1;
}
//////////// // 登記出現過的數字
void list_write(int i)
{
ls[i-1]=1;
}