判断一个 9x9 的数独是否有效。只需要根据以下规则,验证已经填入的数字是否有效即可。
1-9
在每一行只能出现一次。1-9
在每一列只能出现一次。1-9
在每一个以粗实线分隔的 3x3
宫内只能出现一次。上图是一个部分填充的有效的数独。
数独部分空格内已填入了数字,空白格用 '.'
表示。
输入: [ ["5","3",".",".","7",".",".",".","."], ["6",".",".","1","9","5",".",".","."], [".","9","8",".",".",".",".","6","."], ["8",".",".",".","6",".",".",".","3"], ["4",".",".","8",".","3",".",".","1"], ["7",".",".",".","2",".",".",".","6"], [".","6",".",".",".",".","2","8","."], [".",".",".","4","1","9",".",".","5"], [".",".",".",".","8",".",".","7","9"] ] 输出: true
输入: [ ["8","3",".",".","7",".",".",".","."], ["6",".",".","1","9","5",".",".","."], [".","9","8",".",".",".",".","6","."], ["8",".",".",".","6",".",".",".","3"], ["4",".",".","8",".","3",".",".","1"], ["7",".",".",".","2",".",".",".","6"], [".","6",".",".",".",".","2","8","."], [".",".",".","4","1","9",".",".","5"], [".",".",".",".","8",".",".","7","9"] ] 输出: false 解释: 除了第一行的第一个数字从 5 改为 8 以外,空格内其他数字均与 示例1 相同。 但由于位于左上角的 3x3 宫内有两个 8 存在, 因此这个数独是无效的。
1-9
和字符 '.'
。9x9
形式的。class Solution {
public:
bool isValidSudoku(vector>& board) {
for(int i=0;i<9;i++){
int flag1[9]={0};
int flag2[9]={0};
int flag3[9]={0};
for(int j=0;j<9;j++){//每一行判断
if(board[i][j]=='.')continue;
switch(board[i][j])
{
case '1':{if(flag1['1'-'0'-1]==1) return false; else flag1['1'-'0'-1]=1;break;}
case '2':{if(flag1['2'-'0'-1]==1) return false; else flag1['2'-'0'-1]=1;break;}
case '3':{if(flag1['3'-'0'-1]==1) return false; else flag1['3'-'0'-1]=1;break;}
case '4':{if(flag1['4'-'0'-1]==1) return false; else flag1['4'-'0'-1]=1;break;}
case '5':{if(flag1['5'-'0'-1]==1) return false; else flag1['5'-'0'-1]=1;break;}
case '6':{if(flag1['6'-'0'-1]==1) return false; else flag1['6'-'0'-1]=1;break;}
case '7':{if(flag1['7'-'0'-1]==1) return false; else flag1['7'-'0'-1]=1;break;}
case '8':{if(flag1['8'-'0'-1]==1) return false; else flag1['8'-'0'-1]=1;break;}
case '9':{if(flag1['9'-'0'-1]==1) return false; else flag1['9'-'0'-1]=1;break;}
default: return false;
}
}
for(int k=0;k<9;k++){//每一列判断
if(board[k][i]=='.')continue;
switch(board[k][i])
{
case '1':{if(flag2['1'-'0'-1]==1) return false; else flag2['1'-'0'-1]=1;break;}
case '2':{if(flag2['2'-'0'-1]==1) return false; else flag2['2'-'0'-1]=1;break;}
case '3':{if(flag2['3'-'0'-1]==1) return false; else flag2['3'-'0'-1]=1;break;}
case '4':{if(flag2['4'-'0'-1]==1) return false; else flag2['4'-'0'-1]=1;break;}
case '5':{if(flag2['5'-'0'-1]==1) return false; else flag2['5'-'0'-1]=1;break;}
case '6':{if(flag2['6'-'0'-1]==1) return false; else flag2['6'-'0'-1]=1;break;}
case '7':{if(flag2['7'-'0'-1]==1) return false; else flag2['7'-'0'-1]=1;break;}
case '8':{if(flag2['8'-'0'-1]==1) return false; else flag2['8'-'0'-1]=1;break;}
case '9':{if(flag2['9'-'0'-1]==1) return false; else flag2['9'-'0'-1]=1;break;}
default: return false;
}
}
for(int x=0;x<3;x++){//每一块判断
for(int z=0;z<3;z++){
if(board[(i/3)*3+x][3*(i%3)+z]=='.')continue;
switch(board[(i/3)*3+x][3*(i%3)+z])
{
case '1':{if(flag3['1'-'0'-1]==1) return false; else flag3['1'-'0'-1]=1;break;}
case '2':{if(flag3['2'-'0'-1]==1) return false; else flag3['2'-'0'-1]=1;break;}
case '3':{if(flag3['3'-'0'-1]==1) return false; else flag3['3'-'0'-1]=1;break;}
case '4':{if(flag3['4'-'0'-1]==1) return false; else flag3['4'-'0'-1]=1;break;}
case '5':{if(flag3['5'-'0'-1]==1) return false; else flag3['5'-'0'-1]=1;break;}
case '6':{if(flag3['6'-'0'-1]==1) return false; else flag3['6'-'0'-1]=1;break;}
case '7':{if(flag3['7'-'0'-1]==1) return false; else flag3['7'-'0'-1]=1;break;}
case '8':{if(flag3['8'-'0'-1]==1) return false; else flag3['8'-'0'-1]=1;break;}
case '9':{if(flag3['9'-'0'-1]==1) return false; else flag3['9'-'0'-1]=1;break;}
default: return false;
}
}
}
}
return true;
}
};
判断每行每列每块中是不是有重复的数字就可以了。方法很暴力,有什么更好的办法欢迎指教。