牛客网 华为编程

Leetcode 37

class Solution {
    public void solveSudoku(char[][] board) {
        if(board.length == 0 || board == null)
            return;
        helper(board, 0);
    }
    public boolean helper(char[][] board, int index){
        if(index == 81)
            return true;
        int i = index / 9;
        int j = index % 9;
        if(board[i][j] != '.'){
            return helper(board, index + 1);
        }
        else{
            for(char k = '1'; k <= '9'; k++){
                if(check(board, i, j, k)){
                    board[i][j] = k;
                if(helper(board, index + 1))
                    return true;
                else
                    board[i][j] = '.';
                }                    
            }
        } 
        return false;
    }
    public boolean check(char[][] board, int row, int col, char k){
       for(int i = 0; i < 9; i++) {
            if(board[i][col] == k) return false; //check row
            if(board[row][i] == k) return false; //check column
            if(board[3 * (row / 3) + i / 3][3 * (col / 3) + i % 3] == k) return false; //check 3*3 block
        }
        return true;
    }    
}

数独是一个我们都非常熟悉的经典游戏,运用计算机我们可以很快地解开数独难题,现在有一些简单的数独题目,请编写一个程序求解。

输入描述:
输入9行,每行为空格隔开的9个数字,为0的地方就是需要填充的。

输出描述:
输出九行,每行九个空格隔开的数字,为解出的答案。

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int[][] board = new int[9][9];
        while(sc.hasNext()){
	            for(int i = 0; i < 9; i++){
	                for(int j = 0; j < 9; j++){
	                    board[i][j] = sc.nextInt();
	                }
	            } 	           
	        }
        Main test = new Main();
        test.helper(board);
       for(int i = 0; i < 9; i++){
              for(int j = 0; j < 9; j++){
                 if(j!=8)
                    System.out.print(board[i][j]+" ");
                else
                    System.out.println(board[i][j]);
            }
       }       
    }
    public boolean helper(int[][] board){
        for(int i = 0; i < board.length; i++){
            for(int j = 0; j < board[0].length; j++){
                if(board[i][j] == 0){
                    for(int k = 1; k <= 9; k++){
                        if(check(board, i, j, k)){
                            board[i][j] = k;
                        if(helper(board))
                            return true;
                        else
                            board[i][j] = 0;
                        }                    
                     }
                    return false;
                }
             }
          }
        return true;
    }
    public boolean check(int[][] board, int row, int col, int k){
       for(int i = 0; i < 9; i++) {
            if(board[i][col] == k) return false; //check row
            if(board[row][i] == k) return false; //check column
            if(board[3 * (row / 3) + i / 3][3 * (col / 3) + i % 3] == k) return false; //check 3*3 block
        }
        return true;
    }
    
    
}

有一个数组a[N]顺序存放0~N-1,要求每隔两个数删掉一个数,到末尾时循环至开头继续进行,求最后一个被删掉的数的原始下标位置。以8个数(N=7)为例:{0,1,2,3,4,5,6,7},0->1->2(删除)->3->4->5(删除)->6->7->0(删除),如此循环直到最后一个数被删除。

import java.util.Scanner;
import java.util.Queue;
import java.util.LinkedList;
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            int n = scanner.nextInt();
            if (n > 1000) {
                n = 999;
            }
            Main tst = new Main();
            System.out.println(tst.deleteNum(n));
        }
    }
    public int deleteNum(int n){
        Queue q = new LinkedList<>();
        for(int i = 0; i < n; i++){
            q.offer(i);
        }
        while(q.size() != 1){
            for(int count = 2; count != 0; count--){
                q.add(q.poll());
            }
            q.poll();
        }
        return q.poll();
    }
}

你可能感兴趣的:(牛客网 华为编程)