蓝桥杯——路径之谜

 

 思路:箭靶上的数字代表的是经过的次数,我们用深度搜索每搜索一个点就标记这个点,并且

            把对应列和行的次数减一,出口用一个方法判断,当到达(N,N)并且次数完全用尽时

            返回true。

注意:下一个可以走的点一定是未被走过的,剩余次数大于0 的,因为最后要输出路径,所以把路

           径记录到一个集合中去,在回溯的时候,不仅要取消标记,还要把用过的次数加回来,最重

           要的是记录的路径也要删除。

import java.util.ArrayList;
import java.util.Scanner;

public class Main {
    static int[][] vis;
        static int N;
        static int[] north;
        static int[] west;
        static ArrayList<Integer> sb=new ArrayList<>();

        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            N = sc.nextInt();
            vis = new int[N + 1][N + 1];
            north = new int[N + 1];
            west = new int[N + 1];
            for (int i = 1; i <= N; i++) {
                north[i] = sc.nextInt();
            }
            for (int i = 1; i <= N; i++) {
                west[i] = sc.nextInt();
            }

            dfs(1,1);

        }




        public static void dfs(int row,int col){

            if (row==N&&col==N){
                west[row]--;
                north[col]--;
                if (judge()) {
                    sb.add((N - 1) * row + col - 1);
                    //打印
                    for (int i = 0; i < sb.size(); i++) {
                        System.out.print(sb.get(i) + " ");
                    }
                    System.out.println();
                    sb.remove(sb.size() - 1);
                }
                west[row]++;
                north[row]++;
                return ;
            }
            //向左走
            if (col-1>0&&north[col-1]>0&&west[row]>1&&vis[row][col-1]!=1){
                vis[row][col]=1;
                sb.add(N*(row-1)+col-1);
                north[col]--;
                west[row]--;
                dfs(row,col-1);
                north[col]++;
                west[row]++;
                sb.remove(sb.size()-1);
                vis[row][col]=0;
            }
            //向右走
            if (col+1<=N&&north[col+1]>0&&west[row]>1&&vis[row][col+1]!=1){
                vis[row][col]=1;
                sb.add(N*(row-1)+col-1);
                north[col]--;
                west[row]--;
                dfs(row,col+1);
                north[col]++;
                west[row]++;
                sb.remove(sb.size()-1);
                vis[row][col]=0;
            }
            //向下走
            if (row+1<=N&&north[col]>1&&west[row+1]>0&&vis[row+1][col]!=1){
                vis[row][col]=1;
                sb.add(N*(row-1)+col-1);
                north[col]--;
                west[row]--;
                dfs(row+1,col);
                north[col]++;
                west[row]++;
                sb.remove(sb.size()-1);
                vis[row][col]=0;
            }
            //向上走
            if (row-1>0&&north[col]>1&&west[row-1]>0&&vis[row-1][col]!=1){
                vis[row][col]=1;
                sb.add(N*(row-1)+col-1);
                north[col]--;
                west[row]--;
                dfs(row-1,col);
                north[col]++;
                west[row]++;
                sb.remove(sb.size()-1);
                vis[row][col]=0;
            }
        }

    public static boolean judge(){
        for (int i = 1; i <=N; i++) {
            if (north[i]>0){
                return false;
            }
            if (west[i]>0){
                return false;
            }
        }
        return true;
    }

    }

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中,带分数可以用分数类来表示。下面是一个简单的带分数类的实现: ```java public class Fraction { private int integer; private int numerator; private int denominator; public Fraction(int integer, int numerator, int denominator) { this.integer = integer; this.numerator = numerator; this.denominator = denominator; simplify(); } private void simplify() { if (numerator < 0 && denominator < 0) { numerator = -numerator; denominator = -denominator; } if (denominator < 0) { numerator = -numerator; denominator = -denominator; } if (integer < 0 && numerator > 0) { numerator = -numerator; } if (integer < 0 && numerator == 0) { integer = -integer; } if (numerator >= denominator) { integer += numerator / denominator; numerator = numerator % denominator; } int gcd = gcd(numerator, denominator); numerator /= gcd; denominator /= gcd; } private int gcd(int a, int b) { if (b == 0) { return a; } return gcd(b, a % b); } public Fraction add(Fraction other) { int newNumerator = this.numerator * other.denominator + other.numerator * this.denominator; int newDenominator = this.denominator * other.denominator; int newInteger = this.integer + other.integer; return new Fraction(newInteger, newNumerator, newDenominator); } public String toString() { if (integer == 0 && numerator == 0) { return "0"; } String result = ""; if (integer != 0) { result += integer; if (numerator != 0) { result += "_"; } } if (numerator != 0) { result += numerator + "/" + denominator; } return result; } } ``` 这个带分数类实现了以下功能: - 构造函数可以根据整数部分、分子和分母创建一个带分数对象。 - simplify() 方法可以将带分数对象化简,如将负号移到分子上、将整数部分和真分数部分合并、将分数化简等。 - add() 方法可以将两个带分数对象相加,返回一个新的带分数对象。 - toString() 方法可以将带分数对象转换为字符串形式。 这个类实现了带分数的加法操作,可以参考这个类来实现其他的运算操作。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值