算法题23

DFS:
题目描述
矩阵中找字符串的可能路径
输入描述

输出描述

示例1:
输入

输出

        

代码:

import java.util.*;
public class Main {
    static int num=0;
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextLine()) {
            String n0 = in.nextLine();
            int n=Integer.valueOf(n0);
            String[] str=new String[n];
            for(int i=0;i<n;i++){
                str[i] = in.nextLine();
            }
            String tg="WHFJEE";
            //标记矩阵
            int[][] m=new int[n][(str[0].length()+1)/2];
            for(int i=0;i<n;i++) {
            	for(int j=0;j<(str[0].length()+1)/2;j++){
            		m[i][j]=0;
            	}
            }
            //遍历递归           
            for(int i=0;i<n;i++) {
            	for(int j=0;j<(str[0].length()+1)/2;j++){
            		if(tg.charAt(0)==str[i].charAt(j*2)) {
            			dfs(0,i,j,str,n,tg,m);
            		}
            	}
            }  
            System.out.println(num);
        }
    } 
    private static void dfs(int cnt,int x,int y,String[] str,int n,String tg,int[][] m){
        if(x<0||y<0)return;
        if(x>=n||y>=((str[0].length()+1)/2))return;
        //if(cnt>5)return;
        //if(m[x][y/2]==1||tg.charAt(cnt)!=str[x].charAt(y)) return;
        
        //if(m[x][y]==0&&cnt==5&&tg.charAt(cnt)==str[x].charAt(y*2))
        if(cnt==5){
            num++;
            return;
        }if(x+1<n&&y+1<n&&tg.charAt(cnt+1)==str[x+1].charAt((y+1)*2)){
        	dfs(cnt+1,x+1,y+1,str,n,tg,m);
        }if(x+1<n&&y-1>=0&&tg.charAt(cnt+1)==str[x+1].charAt((y-1)*2)){
        	dfs(cnt+1,x+1,y-1,str,n,tg,m);
        }if(x+1<n&&tg.charAt(cnt+1)==str[x+1].charAt(y*2)){
        	dfs(cnt+1,x+1,y,str,n,tg,m); 
        }if(y-1>=0&&tg.charAt(cnt+1)==str[x].charAt((y-1)*2)){
        	dfs(cnt+1,x,y-1,str,n,tg,m);
        }if(y+1<n&&tg.charAt(cnt+1)==str[x].charAt((y+1)*2)){
        	dfs(cnt+1,x,y+1,str,n,tg,m);
        }if(x-1>=0&&tg.charAt(cnt+1)==str[x-1].charAt(y*2)){
        	dfs(cnt+1,x-1,y,str,n,tg,m);
        }if(x-1>=0&&y-1>=0&&tg.charAt(cnt+1)==str[x-1].charAt((y-1)*2)){
        	dfs(cnt+1,x-1,y-1,str,n,tg,m);
        }if(x-1>=0&&y+1<n&&tg.charAt(cnt+1)==str[x-1].charAt((y+1)*2)){
        	dfs(cnt+1,x-1,y+1,str,n,tg,m);
        }
    }
}

/**
@lru_cache(None)
def dfs(i, j, idx):
    if idx == 5: return 1
    cnt = 0
    for x in range(i - 1, i + 2):
        for y in range(j - 1, j + 2):
            if x == i and y == j: continue
            if 0 <= x < n and 0 <= y < m:
                if mat[x][y] == target[idx + 1]:
                    cnt += dfs(x, y, idx + 1):
    return cnt
**/
/**
5
D V Q Z A
X U O H P
E A L W B
J F H L Q
E E D K O

2**/

另一种写法:

import java.util.Scanner;
public class Main {
    static int num=0;
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextLine()) {
            String n0 = in.nextLine();
            int n=Integer.valueOf(n0);
            String[] str=new String[n];
            for(int i=0;i<n;i++){
                str[i] = in.nextLine();
            }
            String tg="SHOPEE";
            //标记矩阵
            int[][] m=new int[n][(str[0].length()+1)/2];
            for(int i=0;i<n;i++) {
            	for(int j=0;j<(str[0].length()+1)/2;j++){
            		m[i][j]=0;
            	}
            }
            //遍历递归           
            for(int i=0;i<n;i++) {
            	for(int j=0;j<(str[0].length()+1)/2;j++){
            		if(tg.charAt(0)==str[i].charAt(j*2)) {
            			dfs(0,i,j,str,n,tg,m);
            		}
            	}
            }  
            System.out.println(num);
        }
    } 
    private static void dfs(int cnt,int x,int y,String[] str,int n,String tg,int[][] m){
        if(x<0||y<0)return;
        if(x>=n||y>=((str[0].length()+1)/2))return;
        if(cnt==5&&tg.charAt(cnt)==str[x].charAt(y*2)){
            num++;
            return;
        }if(tg.charAt(cnt)==str[x].charAt(y*2)){
        	dfs(cnt+1,x+1,y+1,str,n,tg,m);
        	dfs(cnt+1,x+1,y-1,str,n,tg,m);
        	dfs(cnt+1,x+1,y,str,n,tg,m); 
        	dfs(cnt+1,x,y-1,str,n,tg,m);
        	dfs(cnt+1,x,y+1,str,n,tg,m);
        	dfs(cnt+1,x-1,y,str,n,tg,m);
        	dfs(cnt+1,x-1,y-1,str,n,tg,m);
        	dfs(cnt+1,x-1,y+1,str,n,tg,m);
        }    
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值