ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 스도쿠
    백준알고리즘 2019. 8. 9. 00:34
    #include<iostream>
    #include <cstdio>
    #include <cstdlib>
    
    using namespace std;
    
    bool row[9][10], col[9][10], squ[9][10];
    int n, a[9][9], b[81];
    
    int s(int i, int j) {
    	return i / 3 * 3 + j / 3;
    }
    
    void solve(int idx) {
    	if (idx == n) {
    		for (int i = 0; i < 9; i++) {
    			for (int j = 0; j < 9; j++) {
                    cout<<a[i][j]<<" ";
    			}
    			cout<<"\n";
    		}
    		exit(0);
    	}
    	int x = b[idx] / 9, y = b[idx] % 9;
    	for (int i = 1; i < 10; i++) {
    		if (row[x][i] || col[y][i] || squ[s(x, y)][i]) continue;
    		row[x][i] = col[y][i] = squ[s(x, y)][i] = true;
    		a[x][y] = i;
    		solve(idx + 1);
    		a[x][y] = 0;
    		row[x][i] = col[y][i] = squ[s(x, y)][i] = false;
    	}
    }
    
    int main() {
    	for (int i = 0; i < 9; i++) {
    		for (int j = 0; j < 9; j++) {
    			cin >> a[i][j];
    			int k = a[i][j];
    			if (!k) b[n++] = i * 9 + j;
    			else {
    				row[i][k] = true;
    				col[j][k] = true;
    				squ[s(i, j)][k] = true;
    			}
    		}
    	}
    	solve(0);
    	return 0;
    }

    q

    '백준알고리즘' 카테고리의 다른 글

    종이조각  (0) 2019.08.11
    스도쿠 재풀이  (0) 2019.08.10
    스타트와 링크  (0) 2019.08.08
    로봇청소기  (0) 2019.08.08
    구슬탈출2  (0) 2019.08.06

    댓글

Designed by Tistory.