标签:size select clu use return sudoku col orm dom
#include <bits/stdc++.h> using namespace std ; int orign_map[10][10],ans_map[10][10]; int ans_cnt ; int pos_x[100],pos_y[100]; bool buc_h[10][10],buc_l[10][10],buc_blc[4][4][10]; void dfs( int dep , int tot ){ if ( ans_cnt >= 2 ) return ; if ( dep==tot+1 ){ ++ ans_cnt ; return ; } for ( int i=1 ; i<=9 ; ++i ) { if( !buc_h[pos_x[dep]][i] && !buc_l[pos_y[dep]][i]&&!buc_blc[(pos_x[dep]-1)/3+1][(pos_y[dep]-1)/3+1][i]) { ans_map[pos_x[dep]][pos_y[dep]] = i ; buc_h[pos_x[dep]][i] = true; buc_l[pos_y[dep]][i] = true; buc_blc[(pos_x[dep]-1)/3+1][(pos_y[dep]-1)/3+1][i] = true; dfs( dep+1 , tot ) ; buc_h[pos_x[dep]][i] = false; buc_l[pos_y[dep]][i] = false; buc_blc[(pos_x[dep]-1)/3+1][(pos_y[dep]-1)/3+1][i] = false; ans_map[pos_x[dep]][pos_y[dep]] = 0 ; } } } void generateOriginalMap(){ int order[]={0,1,2,3,4,5,6,7,8,9}; srand(time(NULL)); random_shuffle(order+1,order+10); for( int i=4 ; i<=6 ; ++i ) for ( int j=4 ; j<=6 ; ++j ) orign_map[i][j] = order[(i-4)*3+j-3]; for (int i=4 ; i<=6 ; ++i ) for ( int j=1 ; j<=3 ; ++j ) orign_map[i][j] = orign_map[(i+1)%3+4][j+3]; for (int i=4 ; i<=6 ; ++i ) for ( int j=7 ; j<=9 ; ++j ) orign_map[i][j] = orign_map[(i+1)%3+4][j-6]; for (int i=4 ; i<=6 ; ++i ) for ( int j=1 ; j<=3 ; ++j ) orign_map[j][i] = orign_map[j+3][(i+1)%3+4]; for (int i=4 ; i<=6 ; ++i ) for ( int j=7 ; j<=9 ; ++j ) orign_map[j][i] = orign_map[j-6][(i+1)%3+4]; for (int i=7 ; i<=9 ; ++i ) for ( int j=1 ; j<=3 ; ++j ) orign_map[j][i] = orign_map[j+3][(i+1)%3+7]; for (int i=7 ; i<=9 ; ++i ) for ( int j=7 ; j<=9 ; ++j ) orign_map[j][i] = orign_map[j-6][(i+1)%3+7]; for (int i=1 ; i<=3 ; ++i ) for ( int j=1 ; j<=3 ; ++j ) orign_map[j][i] = orign_map[j+3][(i+1)%3+1]; for (int i=1 ; i<=3 ; ++i ) for ( int j=7 ; j<=9 ; ++j ) orign_map[j][i] = orign_map[j-6][(i+1)%3+1]; } void generateSolution(int difficulty){ for(int i=1;i<=9;++i)for(int j=1;j<=9;++j)ans_map[i][j] = orign_map[i][j]; srand(time(NULL)); int numOfBlank = 0 , randIndex ; bool isUsed[100]; memset(isUsed,false,sizeof(isUsed)); memset(buc_h,1,sizeof(buc_h)); memset(buc_l,1,sizeof(buc_l)); memset(buc_blc,1,sizeof(buc_blc)); while (difficulty) { ans_cnt = 0 ; randIndex = rand()%81+1; if( isUsed[randIndex] ) continue; else { --difficulty; isUsed[randIndex] = true; int x = (randIndex-1)/9+1 , y = randIndex-(randIndex-1)/9*9; pos_x[++numOfBlank] = x ; pos_y[numOfBlank] = y ; buc_h[x][orign_map[x][y]] = false ; buc_l[y][orign_map[x][y]] = false ; buc_blc[(x-1)/3+1][(y-1)/3+1][orign_map[x][y]] = false; ans_map[x][y]=0; dfs( 1 , numOfBlank ) ; if ( ans_cnt >= 2 ) { // cout << "illegal!!!!!"<<endl; --numOfBlank; buc_h[x][orign_map[x][y]] = true ; buc_l[y][orign_map[x][y]] = true ; buc_blc[(x-1)/3+1][(y-1)/3+1][orign_map[x][y]] = true; ans_map[x][y] = orign_map[x][y]; } } } } void output(){ int blank=0; cout << "Problem:\n" ; for ( int i=1 ; i<=9 ; ++i ) { for ( int j=1 ; j<=9 ; ++j ) { if(!ans_map[i][j]){ cout << "? "; blank++; } else cout << ans_map[i][j] << " " ; } cout << endl ; } cout << "Blank: " << blank << endl ; cout << "-----------------\nAnswer:\n" ; for ( int i=1 ; i<=9 ; ++i ) { for ( int j=1 ; j<=9 ; ++j ) { cout << orign_map[i][j] << " " ; } cout << endl ; } } int in(){ while(1){ cout << "select difficulty:\n"; cout << "1: easy 2: normal 3: hard 4:hard++\n" ; int a;cin >> a; switch (a) { case 1: return 35 ; break; case 2: return 40 ; break; case 3: return 55 ; break; case 4: return 68 ; break ; default: cout << "illegal input\n" ; break; } } } int main( ) { generateOriginalMap(); generateSolution(in()); output(); return 0 ; }
标签:size select clu use return sudoku col orm dom
原文地址:https://www.cnblogs.com/shadowland/p/11253273.html