12.矩阵中的路径
**题目描述:**请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径。路径可以从矩阵中的任意一个格子开始,每一步可以在矩阵中向左,向右,向上,向下移动一个格子。如果一条路径经过了矩阵中的某一个格子,则该路径不能再进入该格子。 例如 矩阵中包含一条字符串”bcced”的路径,但是矩阵中不包含”abcb”路径,因为字符串的第一个字符b占据了矩阵中的第一行第二个格子之后,路径不能再次进入该格子。
遍历矩阵,找到与路径第一个字符str[0]相同的矩阵元素m;
遍历m相邻的上、下、左、右四个字符,如果有和路径下一个字符str[1]相同的字符,则它就是下一次遍历的起点;
若没有,回退(回溯)到上一个字符,重新遍历。
需要用一个路径数组记录走过的矩阵元素,避免路径重复。
class Solution {
public boolean exist(char[][] board, String word) {
//if(board==null||board.length==0||board[0].length==0||word==null||word.equals("")){
// return false;
//}
boolean[][] isVisited=new boolean[board.length][board[0].length];
for(int i=0;i<board.length;i++){
for(int j=0;j<board[i].length;j++){
if(solve(board,word,i,j,isVisited,0)){
//找到一种情况即可
return true;
}
}
}
return false;
}
private boolean solve(char[][] board,String word,int x,int y,boolean[][] isVisited,int index){
//越界处理和每个格子只能访问1次
if(x<0||x>=board.length||y<0||y>=board[0].length||isVisited[x][y]){
return false;
}
//匹配到某一位置不满足条件
if(word.charAt(index)!=board[x][y]){
return false;
}
//匹配成功
if(index==word.length()-1){
return true;
}
isVisited[x][y]=true; //对[x][y]作向前、后、左、右移动说明[x][y]已被访问
boolean res=solve(board,word,x+1,y,isVisited,index+1)||
solve(board,word,x-1,y,isVisited,index+1)||
solve(board,word,x,y+1,isVisited,index+1)||
solve(board,word,x,y-1,isVisited,index+1);
isVisited[x][y]=false;// 相当于对[x][y]这个位置进行回溯
return res;
}
}
class Solution {
public boolean exist(char[][] board, String word) {
if (board == null || board[0] == null || board.length == 0 || board[0].length == 0 || word == null || word.equals("")) {
return false;
}
boolean[][] isVisited = new boolean[board.length][board[0].length];
char[] chs = word.toCharArray(); //将字符串转换为字符数组
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[0].length; j++) {
if (board[i][j] == chs[0]) {
if (bfs(board, i, j, isVisited, chs, 0)) return true;
}
}
}
return false;
}
private boolean bfs(char[][] board, int i, int j, boolean[][] isVisited, char[] chs, int index) {
if (index == chs.length) {
return true;
}
if (i < 0 || j < 0 || i == board.length || j == board[0].length || isVisited[i][j] || board[i][j] != chs[index]) {
return false;
}
isVisited[i][j] = true;
boolean res = bfs(board, i + 1, j, isVisited, chs, index + 1)
|| bfs(board, i - 1, j, isVisited, chs, index + 1)
|| bfs(board, i, j + 1, isVisited, chs, index + 1)
|| bfs(board, i, j - 1, isVisited, chs, index + 1);
isVisited[i][j] = false;
return res;
}
}
char[ ] chs = word.toCharArray(); //将字符串转换为字符数组
关于字符串中length与length()的区别
13.机器人的运动范围
**题目描述:**地上有一个m行和n列的方格。一个机器人从坐标0,0的格子开始移动,每一次只能向左,右,上,下四个方向移动一格,但是不能进入行坐标和列坐标的数位之和大于k的格子。 例如,当k为18时,机器人能够进入方格(35,37),因为3+5+3+7 = 18。但是,它不能进入方格(35,38),因为3+5+3+8 = 19。请问该机器人能够达到多少个格子?
从(0,0)开始走,每成功走一步标记当前位置为true,然后从当前位置往四个方向探索,
返回当前位置+ 4个方向的探索值之和
探索时,判断当前节点是否可达的标准为:
---当前节点在矩阵内;
---当前节点未被访问过;
---当前节点满足limit限制
public class Solution {
public int movingCount(int threshold, int rows, int cols)
{
boolean[][] isVisited =new boolean[rows][cols]; //行走痕迹表,防止机器人重复进入格子
return solve(0,0,rows,cols,isVisited,threshold);
}
private int solve(int x, int y, int rows, int cols, boolean[][] isVisited, int threshold) {
if(x<0||y<0||x>=rows||y>=cols||isVisited[x][y]||(getDigitSum(x)+getDigitSum(y))>threshold){
return 0;
}
int count=0;
isVisited[x][y]=true; //当前格子已经进入,痕迹表记录为true,防止再次进入
count=1+solve(x+1,y,rows,cols,isVisited,threshold)+ //1表示当前格子已经进入,计数为1
solve(x-1,y,rows,cols,isVisited,threshold)+
solve(x,y+1,rows,cols,isVisited,threshold)+
solve(x,y-1,rows,cols,isVisited,threshold);
return count;
}
//计算坐标数位之和
public int getDigitSum(int number){
int sum = 0;
//先加个位数,再加十位数,以此类推
while(number != 0){
sum += number % 10; //取余,比如number=423,余3,也就是说先加个位数
number /= 10; //取商(取整),423/10=42,也就是说加完个位,再算剩余的位数
}
return sum;
}
}