判断一个矩阵中是否可以找到一条包含某个String的路径

//要求上下左右移动,去过的点不可再次去
//写了递归实现与循环实现
//递归
class Solution1 {
    
    public boolean hasPath(char[] matrix, int rows, int cols, char[] str) {
    	//边界条件
        if(matrix==null || matrix.length==0 || str==null || str.length==0 || matrix.length!=rows*cols || rows<=0 || cols<=0 || rows*cols < str.length) {
            return false ;
        }
       //访问标志位
        boolean[] visited = new boolean[rows*cols] ;
        int[] pathLength = {0} ;
 
        for(int i=0 ; i<=rows-1 ; i++) {
            for(int j=0 ; j<=cols-1 ; j++) {
                if(hasPathCore(matrix, rows, cols, str, i, j, visited, pathLength)) { return true ; }
            }
        }
 
        return false ;
    }
     
    public boolean hasPathCore(char[] matrix, int rows, int cols, char[] str, int row, int col, boolean[] visited, int[] pathLength) {
        boolean flag = false ;
        if(row>=0 && row<rows && col>=0 && col<cols && !visited[row*cols+col] && matrix[row*cols+col]==str[pathLength[0]]) {
            pathLength[0]++ ;
            visited[row*cols+col] = true ;
            if(pathLength[0]==str.length) { return true ; }
            flag = hasPathCore(matrix, rows, cols, str, row, col+1, visited, pathLength)  ||
                   hasPathCore(matrix, rows, cols, str, row+1, col, visited, pathLength)  ||
                   hasPathCore(matrix, rows, cols, str, row, col-1, visited, pathLength)  ||
                   hasPathCore(matrix, rows, cols, str, row-1, col, visited, pathLength) ;
 
            if(!flag) {
                pathLength[0]-- ;
                visited[row*cols+col] = false ;
            }
        }
 
        return flag ;
    }
     
}
//循环实现
class Solution2{
public static boolean hasPath(char[] matrix, int rows, int cols, char[] str)
    {   if(matrix==null||matrix.length<str.length)
    	   return false;
    	int  len = matrix.length;
    	boolean[] array = new boolean[len];
    	Arrays.fill(array, false);
    	int[] dirc = new int[len];
    	Arrays.fill(dirc, -1);
    	int r,c;
    	int index=1;
    	Stack<Integer> s = new Stack<Integer>();
    	for(int  i =0; i<len;i++){
    		//找到首字母
    		Arrays.fill(array, false);
    		s = new Stack<Integer>();
    		Arrays.fill(dirc, -1);
    		if(matrix[i]==str[0]){
    			r = i/cols;
    			c = i%cols;
    			array[i]=true;
    			s.push(i);
    			//递归查找
    			//System.out.println(i);
    		    if(str.length==1)
    		    	return true;
    			while(true){
    				//System.out.println(i);
    				int loc= r*cols+c;
    				if(++dirc[loc]<=3){
    					//上下左右
    					int oldR = r;
    					int oldC=c;
    					if(dirc[loc]==0){
    						r = r>=1?r-1:r;
    						if(array[r*cols+c]==true){
    							r=oldR;
    						}
    					}else
    					if(dirc[loc]==1){
    						r=r<(rows-1)?r+1:r;
    						if(array[r*cols+c]==true){
    							r=oldR;
    						}
    					}else
    					if(dirc[loc]==2){
    						c=c>=1?c-1:c;
    						if(array[r*cols+c]==true){
    							c=oldC;
    						}
    					}else
    					if(dirc[loc]==3){
    						c=c<(cols-1)?c+1:c;
    						if(array[r*cols+c]==true){
    							c=oldC;
    						}
    					}
    					//找到新的方向
    					if(r!=oldR||c!=oldC){
    						int locNew = r*cols+c;
    						//没被访问过
    						if(array[locNew]==false){
    							//相等
    							if(matrix[locNew]==str[index]){
    								index++;
    								System.out.println("weizhio"+i);
    								 //System.out.println(locNew);
    								if(index==str.length)
    									return true;
    								s.push(locNew);
    								array[locNew]=true;//修改访问标志位
    							}//不相等则退回
    							else{
    								r = oldR;
    								c=oldC;
    							}
    						}
    					}
    				}else{
    					if(s.isEmpty()){
    						index=1;
    						break;
    					}
    					array[s.pop()]=false;//一处栈顶,修改访问位
    					index--;
    					if(s.size()>=1){
    					r = s.peek()/cols;
    					c = s.peek()%cols;
    				     //System.out.printlnloc);
    					}
    				}
    				
    			}
    		}
    		
    	}
    	return false;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
判断一个是否为欧拉图或半欧拉图,需要根据欧拉图和半欧拉图的定义来判断。 1. 欧拉图 如果一个有向图或无向图存在一条路径,经过所有的边恰好一次,那么这个图就是欧拉图。 对于无向图,如果这个图是连通的,并且每个顶点的度数都是偶数,那么这个图就是欧拉图。 对于有向图,如果这个图是强连通的,并且每个顶点的入度等于出度,那么这个图就是欧拉图。 2. 半欧拉图 如果一个有向图或无向图存在一条路径,经过所有的边恰好一次,但是起点和终点不同,那么这个图就是半欧拉图。 对于无向图,如果这个图是连通的,且只有两个奇数度数的顶点,那么这个图就是半欧拉图。 对于有向图,如果这个图是强连通的,并且有一个顶点的出度比入度大1,有一个顶点的入度比出度大1,其它顶点的入度等于出度,那么这个图就是半欧拉图。 下面是一个Java程序,可以判断输入的一个有向图和无向图的关系矩阵是不是欧拉图和半欧拉图。 ```java import java.util.Scanner; public class EulerGraph { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // 读入图的类型 System.out.print("请输入图的类型(1为有向图,2为无向图):"); int type = scanner.nextInt(); // 读入顶点个数 System.out.print("请输入顶点个数:"); int n = scanner.nextInt(); // 读入边的个数 System.out.print("请输入边的个数:"); int m = scanner.nextInt(); // 读入关系矩阵 int[][] matrix = new int[n][n]; System.out.println("请输入关系矩阵:"); for (int i = 0; i < m; i++) { int u = scanner.nextInt(); int v = scanner.nextInt(); matrix[u][v] = 1; if (type == 2) { matrix[v][u] = 1; } } // 判断是否为欧拉图或半欧拉图 boolean euler = true; boolean semiEuler = true; int oddDegreeCount = 0; for (int i = 0; i < n; i++) { int degree = 0; for (int j = 0; j < n; j++) { degree += matrix[i][j]; } if (degree % 2 == 1) { oddDegreeCount++; } if (type == 1 && degree != 0 && matrix[i][i] == 0) { euler = false; } } if (type == 1 && oddDegreeCount != 0) { semiEuler = false; } if (type == 2 && oddDegreeCount != 0 && oddDegreeCount != 2) { semiEuler = false; } // 输出结果 if (euler) { System.out.println("这是一个欧拉图。"); } else if (semiEuler) { System.out.println("这是一个半欧拉图。"); } else { System.out.println("这不是欧拉图也不是半欧拉图。"); } } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值