import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int a = sc.nextInt(); int b = sc.nextInt(); int c = sc.nextInt(); int d = sc.nextInt(); long[][] str = new long[a+1][b+1]; str[c][d]=1; if(c+1<str.length && d+2<str[0].length){ str[c+1][d+2] = 1; } if(c+2<str.length && d+1<str[0].length){ str[c+2][d+1] = 1; } if(c-1>=0 && d+2<str[0].length){ str[c-1][d+2]=1; } if(c-2>=0 && d+1<str[0].length){ str[c-2][d+1]=1; } if(c-1>=0 && d-2>=0){ str[c-1][d-2]=1; } if(c-2>=0 && d-1>=0){ str[c-2][d-1]=1; } if(c+1<str.length && d-2>=0){ str[c+1][d-2]=1; } if(c+2<str.length && d-1>=0){ str[c+2][d-1]=1; } int[][] atr = new int[a+1][b+1]; for(int i = 0;i<str.length;i++){ if(str[i][0]!=1){ atr[i][0]=1; }else{ break; } } for(int i = 0;i<str[0].length;i++){ if(str[0][i]!=1){ atr[0][i]=1; }else{ break; } } for(int i = 1;i<str.length;i++){ for(int j=1;j<str[0].length;j++){ if(str[i][j]!=1){ atr[i][j] = atr[i-1][j]+atr[i][j-1]; } } } System.out.print(atr[atr.length-1][atr[0].length-1]); } }
时间: 2024-04-29 18:20:02 浏览: 16
这段代码主要是实现了一个马走日的问题,输入四个整数 a、b、c、d,其中 a、b 代表棋盘的大小,c、d 代表马的初始位置。棋盘左下角为原点,横坐标范围从 0 到 a-1,纵坐标范围从 0 到 b-1。马只能走“日”字型,即向右或向左走两步,再向上或向下走一步,或者先向上或向下走一步,再向右或向左走两步。求马从初始位置 c、d 出发,到达棋盘右上角的所有走法中,路径长度恰好为 k 的走法数目。
代码中首先使用 Scanner 类读取输入的四个整数,然后定义一个二维数组 str 来表示棋盘上的位置,其中 str[i][j] 表示位置 (i,j) 上是否有马。初始时将位置 (c,d) 上的值设为 1。
接着根据马的走法,遍历棋盘上的所有位置,如果当前位置可以到达,则将该位置的值设为 1。
然后再定义一个二维数组 atr,来表示从起点到每个位置的路径数目。其中 atr[i][j] 表示从位置 (0,0) 到位置 (i,j) 的路径数目。首先将第一行和第一列的值初始化为 1,因为从起点到第一行或第一列的任意一个位置都只有一条路径。
然后使用动态规划的思想,从左上角开始,计算每个位置的路径数目。如果当前位置上没有马,则该位置的路径数目等于它上面位置和左边位置的路径数目之和,即 atr[i][j] = atr[i-1][j]+atr[i][j-1]。
最后输出 atr 数组中右上角位置的值,即为从起点到终点路径长度为 k 的路径数目。
相关问题
import java.util.Scanner; public class Test1 { 晋涛 public static void main(String[] args) { Sy
import java.util.Scanner;
public class Test1 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入您的姓名:");
String name = scanner.nextLine();
.out.println("您好," + name + "!");
}
}
这段代码是一个简单的Java程序,它使用了Java标准库中的Scanner类来实现用户输入和输出。首先,我们通过创建Scanner对象来获取用户的输入。然后,使用nextLine()方法读取用户输入的姓名,并将其存储在name变量中。最后,通过System.out.println()方法将问候语和用户的姓名输出到控制台。
import java.io.*; import java.util.Arrays; import java.util.Scanner; public class FileTest { public static void main(String[] args) throws IOException { // 接收给定的一行字符串 Scanner scanner = new Scanner(System.in); String line = scanner.nextLine();
// 创建文件对象,并检查文件是否存在 File file = new File(line); if (!file.exists()) { System.out.println("文件不存在!"); return; } // 读取文件内容 BufferedReader reader = new BufferedReader(new FileReader(file)); String content = ""; String temp; while ((temp = reader.readLine()) != null) { content += temp + "\n"; } // 关闭文件读取器 reader.close(); // 输出文件内容 System.out.println("文件内容:\n" + content); } }
这段代码是一个读取文件并输出文件内容的Java程序。首先,通过Scanner接收用户输入的文件路径,然后判断文件是否存在。如果文件不存在,则输出提示信息并结束程序。如果文件存在,则创建文件读取器并读取文件内容,将读取到的内容存储在一个字符串变量中。最后,关闭文件读取器并输出文件内容。
相关推荐
![text/x-java](https://img-home.csdnimg.cn/images/20210720083646.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)