n Queen and Sudoku

Welcome

The queen problem and the Sudoku problem can be said to be two very classic problems in the search problem, so put them together to summarize.

n queen

Title link

practice

Search each grid, each grid has two conditions: put and not put, search in this order. Of course, you can also search in the order of the rows.

Introduce here, the diagonal and vice diagonal representation method:

C ++ code

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int N = 15;

int n;
char g[N][N];
bool r[N], c[N], diag[N * 2], undiag[N * 2]; // 行 列 副对角线 对角线

void dfs(int x, int y, int s) {
    if (s > n) return;
    if (x == n && y == n + 1) {
        if (s == n) {
            for (int i = 1; i <= n; i++) printf("%s\n", g[i] + 1);
            puts("");
        }
        return;
    }
    if (y == n + 1) {
        y = 1;
        x++;
    }
    
    // 这个点不放
    g[x][y] = '.';
    dfs(x, y + 1, s);
    
    // 这个点放
    if (!r[x] && !c[y] && !diag[x + y - 1] && !undiag[n + x - y]) {
        g[x][y] = 'Q';
        r[x] = true;
        c[y] = true;
        diag[x + y - 1] = true;
        undiag[n + x - y] = true;
        dfs(x, y + 1, s + 1);
        // 恢复现场
        r[x] = false;
        c[y] = false;
        diag[x + y - 1] = false;
        undiag[n + x - y] = false;
    }
} 

int main()
{
    scanf("%d", &n);
    
    dfs(1, 1, 0);

    return 0;
}

Sudoku

Title link

Sudoku seems to have many optimizations, bit operations? dangcing links? Forget it, forget it, don't learn it: (

practice

The same idea as Queen N, search for each position, there are two cases for each position, if there is a number or not yet, if there is a number, skip to the next position, if there is no number, enumerate 1 ~ 9 to see which number meets the number Independent rules.

Java code

import java.util.*;
public class Main {
    static int N = 12;
    static char[][] g = new char[N][N];
    static boolean[][] r = new boolean[N][N];
    static boolean[][] c = new boolean[N][N];
    static boolean[][] k = new boolean[N][N];
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        String str = null;
        for (int i = 0; i < 9; i++) {
            str = scan.next();
            g[i] = str.toCharArray();
            for (int j = 0; j < 9; j++) {
                if (g[i][j] != '.') {
                    int num = g[i][j] - '0';
                    r[i][num] = c[j][num] = k[get(i, j)][num] = true;
                }
            }
        }
        // for (int i = 0; i < 9; i++) {
        //     for (int j = 0; j < 9; j++) System.out.print(g[i][j]);
        //     System.out.println();
        // }
        dfs(0, 0);
    }
    public static void dfs(int x, int y) {
        if (y == 9) {
            x++;
            y = 0;
        }
        if (x == 9) {
            for (int i = 0; i < 9; i++) {
                System.out.println(g[i]);
            }
            return;
        }
        if (g[x][y] != '.') {
            dfs(x, y + 1);
            return; // 这里一定不能忘了return    T_T
                    // 因为这里和八皇后问题是不一样的,假如不return就会把原来的数覆盖掉的。
        }
        for (int i = 1; i <= 9; i++) {
            if (!r[x][i] && !c[y][i] && !k[get(x, y)][i]) {
                g[x][y] = (char)(i + '0');
                r[x][i] = c[y][i] = k[get(x, y)][i] = true;
                dfs(x, y + 1);
                r[x][i] = c[y][i] = k[get(x, y)][i] = false;
                g[x][y] = '.';
            }
        }
    }
    public static int get(int x, int y) {
        return 3 * (x / 3) + y / 3 + 1;
    }
}

reference

AcWing written interview

Guess you like

Origin www.cnblogs.com/optimjie/p/12697979.html