Leetcode全游戏问题

本文介绍了LeetCode中的一系列游戏挑战,包括数独验证、数独求解、雨水填充问题、跳跃游戏、N皇后问题等。涉及算法包括深度优先搜索、贪心策略和动态规划。同时讨论了如何解决这些游戏问题,如使用DFS解决数独,贪心策略处理跳跃游戏等。
摘要由CSDN通过智能技术生成

目录

1、编号34 Valid Sudoku
2、编号36 Sudoku Solver
3、编号41 Trapping Rain Water
4、编号46 Jump Game
5、编号47 Jump Game II 
6、编号50 N-Queens
7、编号52 N-Queens II 
8、编号61 UniquePaths
9、编号63 Unique Paths II
10、编号70 Climbing Stairs
11、编号122 Best Time to Buy and Sell Stock
12、编号123 Best Time to Buy and Sell Stock II 
13、编号124 Best Time to Buy and Sell Stock III
14、编号131 Surrounded Regions
15、编号135 Gas Station
16、编号136 Candy
17、编号147 LRU Cache

1、编号34 Valid Sudoku

Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.
The Sudoku board could be partially filled, where empty cells are filled with the character '.'.

检查数独。没什么好说的,依次按行,列,九宫检查。

class Solution {
public:
    bool isValidSudoku(vector<vector<char> > &board) {
        int m = board.size();
        int n = board[0].size();
        int flagM[m];
        int flagN[n];
        int block[9];
        
        /*Check Row*/
        for(int i = 0; i < m; i++){
            for(int j = 0; j < n; j++) flagN[j] = 0;
            for(int j = 0; j < n; j++){
                int c = board[i][j] - '1';/*Remember to use 1 here*/
                if(board[i][j] != '.'){
                    if(flagN[c] == 1) return false;
                    flagN[c]++; 
                }
            }
        }
        
        /*Check Column*/
        for(int i = 0; i < n; i++){
            for(int j = 0; j < m; j++) flagM[j] = 0;
            for(int j = 0; j < m; j++){
                int c = board[j][i] - '1';
                if(board[j][i] != '.'){
                    if(flagM[c] == 1) return false;    
                    flagM[c]++;
                }
            }
        }
        
        /*Check Block*/
        for(int i1 = 0; i1 < 3; i1++){
            for(int i2 = 0; i2 < 3; i2++){
                for(int j = 0; j < 9; j++) block[j] = 0;
                for(int j = 0; j < 3; j++){
                    for(int k = 0; k < 3; k++){
                        int c = board[j+3*i1][k+3*i2] - '1';
                        if(board[j+3*i1][k+3*i2] != '.'){
                            if(block[c] == 1) return false;
                            block[c]++;
                        }
                    }
                }
            }
        }
        return true;
    }
};

2、编号36 Sudoku Solver

Write a program to solve a Sudoku puzzle by filling the empty cells.
Empty cells are indicated by the character '.'.
You may assume that there will be only one unique solution.

使用DFS(也就是Backtracking递归)来尝试所有组合。下面解法假设数独矩阵是9*9的。基本就是尝试所有可能性的暴力解法。

    bool isValid(vector<vector<char> > &board, int x, int y)  
    {  
        int i, j;  
        for (i = 0; i < 9; i++)  
            if (i != x && board[i][y] == board[x][y])  return false; 
            
        for (j = 0; j < 9; j++)  
            if (j != y && board[x][j] == board[x][y])  return false;  
            
        for (i = 3 * (x / 3); i < 3 * (x / 3 + 1); i++)  
            for (j = 3 * (y / 3); j < 3 * (y / 3 + 1); j++)  
                if (i != x && j != y && board[i][j] == board[x][y])  return false;  
        return true;  
    }  
    bool solveSudoku(vector<vector<char> > &board)  
    {  
        for (int i = 0; i < 9; ++i)  
            for (int j = 0; j < 9; ++j){
                if ('.' == board[i][j])  {  
                    for (int k = 1; k <= 9; ++k){  
                        board[i][j] = '0' + k;  
                        if (isValid(board, i, j) && solveSudoku(board)) return true;  
                        board[i][j] = '.';  
                    }  
                    return false;  
                }  
            }  
        return true;  
    }  
};

3、编号41 Trapping Rain Water

Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.
For example, Given [0,1,0,2,1,0,1,3,2,1,2,1], return 6

建立两个辅助数组,里面存储某点i左边最高和右边最高的高度。最后遍历数组,左右最小值减当前值就是结果。

class Solution {
public:
    int trap(int A[], int n) {
        int result = 0;
        int LeftMost[n];
        int RightMost[n];
        
        LeftMost[0] = A[0];
        for(int i = 1; i < n; i++)
            LeftMost[i] = LeftMost[i-1] > A[i-1] ? LeftMost[i-1] : A[i-1];
        
      
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值