zoukankan      html  css  js  c++  java
  • 剑指offer

    今天完成了剑指offer上的66道编程题,感觉自己还是很多代码实现能力和算法积累都还不够!还需要继续联系,坚持自己独立写代码实现。

    最后将今天的两道题目奉上,都有异曲同工之妙:

    矩阵中的路径:

    #include<memory.h>
    
    //题目描述
    //
    //请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径。
    //路径可以从矩阵中的任意一个格子开始,每一步可以在矩阵中向左,向右,向上,向下移动一个格子。
    //如果一条路径经过了矩阵中的某一个格子,则该路径不能再进入该格子。 
    //例如 a b c e s f c s a d e e 矩阵中包含一条字符串"bcced"的路径,但是矩阵中不包含"abcb"路径,
    //因为字符串的第一个字符b占据了矩阵中的第一行第二个格子之后,路径不能再次进入该格子。
    
    //思路
    //深搜的基本题型,分别以每个格子为起点,往四个方向去遍历,直到找到符合题意的字符串即可
    class Solution {
    public:
        bool hasPath(char* matrix, int rows, int cols, char* str)
        {
            if (matrix==nullptr||str==nullptr||rows<1||cols<1)
            {
                return false;
            }
            //建立一个标记数组,走过的位置为true,没走过的为false
            bool *vis = new bool[rows*cols];
            memset(vis,false,rows*cols);
            int pathLength = 0;
            for (size_t i = 0; i < rows; i++)
            {
                for (size_t j = 0; j < cols; j++)
                {
                    if (hasPathCore(matrix,rows,cols,i,j,str,pathLength,vis))
                    {
                        return true;
                    }
                }
            }
            delete[]vis;
            return false;
        }
        //判断从一个元素出发是否能找到一条路径
        bool hasPathCore(char *matrix,int rows,int cols,int x,int y,char *str,int pathLength,bool *vis)
        {
            if (str[pathLength]=='')//嵌套调用出口
            {
                return true;
            }
            bool hasPath = false;//函数返回值
            if (x>=0&&x<=rows&&y>=0&&y<=cols&&matrix[x*cols+y]==str[pathLength]&&!vis[x*cols+y])//坐标范围判断,x=0开始,所以x*cols+y索引正确
            {
                ++pathLength;
                vis[x*cols + y] = true;
                hasPath = hasPathCore(matrix, rows, cols, x - 1, y, str, pathLength, vis)
                    || hasPathCore(matrix, rows, cols, x + 1, y, str, pathLength, vis)
                    || hasPathCore(matrix, rows, cols, x, y - 1, str, pathLength, vis)
                    || hasPathCore(matrix, rows, cols, x, y + 1, str, pathLength, vis);
                if (!hasPath) //没有路径时,返回到原来位置
                {
                    --pathLength;
                    vis[x*cols + y] = false;
                }        
            }
            return hasPath;
        }
    };

    //题目描述:机器人的运动范围
    //
    //地上有一个m行和n列的方格。一个机器人从坐标0, 0的格子开始移动,每一次只能向左,右,上,下四个方向移动一格,
    //但是不能进入行坐标和列坐标的数位之和大于k的格子。 例如,当k为18时,机器人能够进入方格(35, 37),因为3 + 5 + 3 + 7 = 18。
    //但是,它不能进入方格(35, 38),因为3 + 5 + 3 + 8 = 19。请问该机器人能够达到多少个格子?
    
    //思路
    //深搜基础题,从0开始按照四个方向递归求解即可
    class Solution {
    public:
        int movingCount(int threshold, int rows, int cols)
        {
            bool *vis = new bool[rows*cols];
            memset(vis,false,rows*cols);
            int result = movingCountCore(threshold,rows,cols,0,0,vis);
            delete[]vis;
            return result;
        }
        int movingCountCore(int threshold,int rows,int cols,int x,int y,bool *vis)
        {
            int count = 0;
            if (x >= 0 && x<rows && y >= 0 && y<cols && getSum(x) + getSum(y) <= threshold && !vis[x*cols + y])
            {
                vis[x*cols + y] = true;
                count = 1 + movingCountCore(threshold, rows, cols, x - 1, y, vis)
                    + movingCountCore(threshold, rows, cols, x + 1, y, vis)
                    + movingCountCore(threshold, rows, cols, x, y - 1, vis)
                    + movingCountCore(threshold, rows, cols, x, y + 1, vis);
            }
            return count;
        }
        int getSum(int num)
        {
            int sum = 0;
            while (num)
            {
                sum += num % 10;//先取各位
                num /= 10;//从低到高位
            }
            return sum;
        }
    };
  • 相关阅读:
    毕业设计后续工作目标
    毕业设计第二周每天工作
    毕业设计第二次本周目标
    毕业设计第一周每天工作
    毕业设计第一次本周目标
    MyEclipse在删除文件后servers报错问题解决
    Tomcat问题解决
    MVC设计模式授权第三方登录
    算法评估
    Spark 配置参数
  • 原文地址:https://www.cnblogs.com/ranjiewen/p/5242689.html
Copyright © 2011-2022 走看看