1212:LETTERS

【题目描述】
给出一个roe×col的大写字母矩阵,一开始的位置为左上角,你可以向上下左右四个方向移动,并且不能移向曾经经过的字母。问最多可以经过几个字母。

【输入】
第一行,输入字母矩阵行数R和列数S,1≤R,S≤20。

接着输出R行S列字母矩阵。

【输出】
最多能走过的不同字母的个数。
【输入样例】
3 6
HFDFFB
AJHGDH
DGAGEH
【输出样例】
6
思路分析:
迷宫问题

import java.util.*;
public class Main {
    static char a[][]=new char[22][22];
    static boolean c[][]=new boolean[22][22];
    static int h[]=new int[]{-1,0,1,0};
    static int l[]=new int[]{0,1,0,-1};
    static int ans=0;
    static int t[]=new int[200];//题目限制不能移向曾经已走过的字母
    static int r;
    static int s;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        r=sc.nextInt();
        s=sc.nextInt();
        for(int i=1;i<=r;i++){
            String str=sc.next();
            char b[]=str.toCharArray();
            for(int j=1;j<=s;j++){
                a[i][j]=b[j-1];
            }
        }
        c[1][1]=true;
        t[a[1][1]]=1;
        dfs(1,1,1);
        System.out.println(ans);
    }


    public static void dfs(int x,int y,int cnt) {
        ans=Math.max(ans,cnt);
        for(int i=1;i<=4;i++){//四中移动方案
            int xx=x+h[i-1];
           int yy=y+l[i-1];
            if(xx>0&&xx<=r&&yy>0&&yy<=s&&!c[xx][yy]&&t[a[xx][yy]]==0){
                c[xx][yy]=true;
                t[a[xx][yy]]=1;
                dfs(xx,yy,cnt+1);
                c[xx][yy]=false;
                t[a[xx][yy]]=0;
            }
        }
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值