蓝桥杯模拟一 数独
蒜头君今天突然开始还念童年了,想回忆回忆童年。他记得自己小时候,有一个很火的游戏叫做数独。便开始来了一局紧张而又刺激的高阶数独。蒜头君做完发现没有正解,不知道对不对? 不知道聪明的你能否给出一个标准答案?
标准数独是由一个给与了提示数字的 9 \times 99×9 网格组成,我们只需将其空格填上数字,使得每一行,每一列以及每一个 3 \times 33×3 宫都没有重复的数字出现。
输出这个数独得正解,输出格式如下:
1 * 2 6 * * * * * *
2 * * * 5 * 2 * * 4
3 * * * 1 * * * * 7
4 * 3 * * 2 * 1 8 *
5 * * * 3 * 9 * * *
6 * 5 4 * 1 * * 7 *
7 5 * * * * 1 * * *
8 6 * * 9 * 7 * * *
9 * * * * * * 7 5 *
把上面的 *
替换成 1 - 91−9 就可以了
提醒:两个数字之间要有一个空格,其他地方不要输出多余的符号。
#include <algorithm> #include <iostream> #include <cstring> #include <cstdio> #include <vector> #include <cmath> #include <map> #include <set> #define maxn 10010 #define debug(a) cout << #a << " " << a << endl using namespace std; typedef long long ll; int vis1[10][10] = {0}, vis2[10][10] = {0}, vis3[10][10] = {0}; int mapn[10][10] = {0}, num = 0; int f( int x, int y ) { if( x >= 1 && x <= 3 ) { if( y >= 1 && y <= 3 ) { return 1; } else if( y >= 4 && y <= 6 ) { return 2; } else if( y >= 7 && y <= 9 ) { return 3; } } else if( x >= 4 && x <= 6 ) { if( y >= 1 && y <= 3 ) { return 4; } else if( y >= 4 && y <= 6 ) { return 5; } else if( y >= 7 && y <= 9 ) { return 6; } } else if( x >= 7 && x <= 9 ) { if( y >= 1 && y <= 3 ) { return 7; } else if( y >= 4 && y <= 6 ) { return 8; } else if( y >= 7 && y <= 9 ) { return 9; } } } void dfs( int x, int y ) { //cout << ++num << endl; if( x == 10 && y == 1 ) { ++num; for( int i = 1; i <= 9; i ++ ) { for( int j = 1; j <= 9; j ++ ) { cout << mapn[i][j] << " "; } cout << endl; } cout << "-----------" << endl; return ; } if( mapn[x][y] ) { if( y + 1 > 9 ) { dfs( x + 1, 1 ); } else { dfs( x, y + 1 ); } } else { for( int i = 1; i <= 9; i ++ ) { if( !vis1[x][i] && !vis2[y][i] && !vis3[f(x,y)][i] ) { if( y + 1 > 9 ) { vis1[x][i] = 1,vis2[y][i] = 1,vis3[f(x,y)][i] = 1; //debug(i); mapn[x][y] = i; dfs( x + 1, 1 ); mapn[x][y] = 0; vis1[x][i] = 0,vis2[y][i] = 0,vis3[f(x,y)][i] = 0; } else { vis1[x][i] = 1,vis2[y][i] = 1,vis3[f(x,y)][i] = 1; mapn[x][y] = i; dfs( x, y + 1 ); mapn[x][y] = 0; vis1[x][i] = 0,vis2[y][i] = 0,vis3[f(x,y)][i] = 0; } } } } } int main() { vis1[1][2] = 1,vis1[1][6] = 1,vis1[2][5] = 1, vis1[2][2] = 1,vis1[2][4] = 1,vis1[3][1] = 1, vis1[3][7] = 1,vis1[4][3] = 1,vis1[4][2] = 1, vis1[4][1] = 1,vis1[4][8] = 1,vis1[5][3] = 1, vis1[5][9] = 1,vis1[6][5] = 1,vis1[6][4] = 1, vis1[6][1] = 1,vis1[6][7] = 1,vis1[7][5] = 1, vis1[7][1] = 1,vis1[8][6] = 1,vis1[8][9] = 1, vis1[8][7] = 1,vis1[9][7] = 1,vis1[9][5] = 1; vis2[1][5] = 1,vis2[1][6] = 1,vis2[2][2] = 1, vis2[2][3] = 1,vis2[2][5] = 1,vis2[3][6] = 1, vis2[3][4] = 1,vis2[4][5] = 1,vis2[4][1] = 1, vis2[4][3] = 1,vis2[4][9] = 1,vis2[5][2] = 1, vis2[5][1] = 1,vis2[6][2] = 1,vis2[6][9] = 1, vis2[6][1] = 1,vis2[6][7] = 1,vis2[7][1] = 1, vis2[7][7] = 1,vis2[8][8] = 1,vis2[8][7] = 1, vis2[8][5] = 1,vis2[9][4] = 1,vis2[9][7] = 1; vis3[1][2] = 1,vis3[1][6] = 1,vis3[2][5] = 1, vis3[2][2] = 1,vis3[2][1] = 1,vis3[3][4] = 1, vis3[3][7] = 1,vis3[4][3] = 1,vis3[4][4] = 1, vis3[4][5] = 1,vis3[5][1] = 1,vis3[5][2] = 1, vis3[5][3] = 1,vis3[5][9] = 1,vis3[6][1] = 1, vis3[6][7] = 1,vis3[6][8] = 1,vis3[7][5] = 1, vis3[7][6] = 1,vis3[8][1] = 1,vis3[8][7] = 1, vis3[8][9] = 1,vis3[9][5] = 1,vis3[9][7] = 1; mapn[1][2] = 2,mapn[1][3] = 6,mapn[2][4] = 5, mapn[2][6] = 2,mapn[2][9] = 4,mapn[3][4] = 1, mapn[3][9] = 7,mapn[4][2] = 3,mapn[4][5] = 2, mapn[4][7] = 1,mapn[4][8] = 8,mapn[5][4] = 3, mapn[5][6] = 9,mapn[6][2] = 5,mapn[6][3] = 4, mapn[6][5] = 1,mapn[6][8] = 7,mapn[7][1] = 5, mapn[7][6] = 1,mapn[8][1] = 6,mapn[8][4] = 9, mapn[8][6] = 7,mapn[9][7] = 7,mapn[9][8] = 5; dfs( 1, 1 ); cout << num << endl; return 0; } /*
搜出来的第一种答案
1 2 6 7 3 4 5 9 8 3 7 8 5 9 2 6 1 4 4 9 5 1 6 8 2 3 7 7 3 9 4 2 5 1 8 6 8 6 1 3 7 9 4 2 5 2 5 4 8 1 6 3 7 9 5 4 7 2 8 1 9 6 3 6 1 3 9 5 7 8 4 2 9 8 2 6 4 3 7 5 1 */
彼时当年少,莫负好时光。