Skip to content

Instantly share code, notes, and snippets.

@niklasjang
Created April 1, 2020 02:48
Show Gist options
  • Save niklasjang/e2c2c3e4cd284b4eb86b66d49708be7d to your computer and use it in GitHub Desktop.
Save niklasjang/e2c2c3e4cd284b4eb86b66d49708be7d to your computer and use it in GitHub Desktop.
[PS][완전탐색][N자리 K진수]/[BOJ][2239][스도쿠]
#include <iostream>
#include <string.h>
using namespace std;
int map[9][9];
bool flag;
bool visited[10];
bool check(int x, int y){
memset(visited,false, sizeof(visited));
for(int i=0; i<9; i++){
int curr = map[x][i];
if(curr && visited[curr]) return false;
visited[curr] = true;
}
memset(visited,false, sizeof(visited));
for(int i=0; i<9; i++){
int curr = map[i][y];
if(curr && visited[curr]) return false;
visited[curr] = true;
}
memset(visited,false, sizeof(visited));
for(int r=0; r<3; r++){
for(int c=0; c<3; c++){
int curr = map[(x/3)*3+r][(y/3)*3+c];
if(curr && visited[curr]) return false;
visited[curr] = true;
}
}
return true;
}
void recur(int x, int y){
if(flag) return;
if(y == 9){
x++;
y=0;
}
if(x==9){
flag = true;
for(int i=0; i<9; i++){
for(int j=0; j<9; j++){
cout<< map[i][j];
}
cout<<"\n";
}
return;
}
if(map[x][y]){
recur(x,y+1);
}else
{
for(int i=1; i<=9;i++){
map[x][y] = i;
if(check(x,y)) recur(x,y+1);
}
map[x][y] = 0;
}
}
int main (void){
for(int i=0; i<9; i++){
for(int j=0; j<9; j++){
scanf("%1d", &map[i][j]);
}
}
recur(0,0);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment