Skip to content

Instantly share code, notes, and snippets.

@victoriagjh
Created January 21, 2020 15:06
Show Gist options
  • Save victoriagjh/1e5ada6dc8074b98f5c934341d9b18d2 to your computer and use it in GitHub Desktop.
Save victoriagjh/1e5ada6dc8074b98f5c934341d9b18d2 to your computer and use it in GitHub Desktop.
큰일났다.. 바보가 된거같아ㅠㅠㅠ
#include <iostream>
#include <vector>
using namespace std;
int map[10][10];
vector<pair<int,int>> vec;
bool check_v(int y,int num){
for(int i=0; i<9; i++) {
if(num==map[i][y])
return false;
}
return true;
}
bool check_h(int x,int num){
for(int i=0; i<9; i++) {
if(num==map[x][i])
return false;
}
return true;
}
bool check_s(int x, int y, int num){
x=x/3;
y=y/3;
for(int i=x*3; i<x*3+3; i++) {
for(int j=y*3; j<y*3+3; j++) {
if(map[i][j]==num)
return false;
}
}
return true;
}
void dfs(int x,int cnt){
if(cnt==vec.size()){
for(int i=0; i<9; i++) {
for(int j=0; j<9; j++) {
cout<<map[i][j]<<" ";
}
cout<<endl;
}
exit(0);
}
for(int k=1; k<10; k++) {
if(check_h(vec[x].first,k) && check_v(vec[x].second,k) && check_s(vec[x].first, vec[x].second, k)) {
map[vec[x].first][vec[x].second]=k;
dfs(x+1,cnt+1);
map[vec[x].first][vec[x].second]=0;
}
}
}
int main() {
for(int i=0; i<9; i++) {
for(int j=0; j<9; j++) {
cin>>map[i][j];
if(map[i][j]==0) {
vec.push_back({i,j});
}
}
}
dfs(0,0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment