Java 递归查找二维矩阵中的元素
我试图在矩阵中递归地找到一个数字Java 递归查找二维矩阵中的元素,java,arrays,recursion,matrix,Java,Arrays,Recursion,Matrix,我试图在矩阵中递归地找到一个数字 public class Test2 { static char arry [][] = {{'#','#','#'}, {'#',' ',' '}, {'#',' ','#'}, {'#',' ','1'}, }; public static void main(String args []){ rec
public class Test2 {
static char arry [][] = {{'#','#','#'},
{'#',' ',' '},
{'#',' ','#'},
{'#',' ','1'},
};
public static void main(String args []){
recursion(2,1);
}
private static void recursion(int row, int col) {
if(arry[row][col]=='1' && isInBound(row,col)==true){
System.out.println("Found at " + row + " " + col);
}else if ((isInBound(row,col)==true)&& arry[row][col]==' '){
recursion(row,col+1);
recursion(row,col-1);
recursion(row-1,col);
recursion(row+1,col);
}
}
private static boolean isInBound(int row, int col) {
boolean bol = false;
if(row<= arry.length && col <= arry[0].length){
bol = true;
}
return bol;
}
公共类Test2{
静态字符数组[][]={{{'#','#','#','#'},
{'#',' ',' '},
{'#',' ','#'},
{'#',' ','1'},
};
公共静态void main(字符串参数[]){
递归(2,1);
}
私有静态无效递归(int行,int列){
if(arry[row][col]='1'&&isInBound(row,col)==true){
System.out.println(“位于“+行+”+列);
}else if((isInBound(行,列)=true)和&arry[row][col]=''){
递归(行、列+1);
递归(行,列-1);
递归(第1行,第2列);
递归(行+1,列);
}
}
私有静态布尔值IsInBond(int行,int列){
布尔bol=假;
如果(row仅使用您目前编写的内容,您应该首先使用isInBound
检查您的索引是否有效,如果超出范围,则立即返回;
。按照您当前检查范围的方式,您将始终在测试范围之前使用范围,这意味着您总是会遇到超出范围的异常少了你的起点
例如,在私有静态无效递归(..)的开头添加以下内容:
if(!isInBound(row,col)) {
return;
}
并且不需要后续的数组绑定测试。因此,递归调用对无效边界的任何递归调用都将简单地返回,而不会引起越界异常。仅使用您目前编写的内容,您应该首先使用
isInBound
检查索引是否有效,然后立即使用返回;
如果超出边界。按照当前检查边界的方式,在测试边界之前,您将始终使用边界,这意味着您总是会遇到超出边界的异常,无论从何处开始
private static void recursion(int row, int col) {
if(arry[row][col]=='1'){
System.out.println("Found at " + row + " " + col);
}
else if (arry[row][col]=='#'){
return;
}
if (isInBound(row,col + 1)){
recursion(row,col+1);
}
if(isInBound(row, col - 1)){
recursion(row, col - 1)
}
if(isInBound(row -1, col)){
recursion(row-1,col);
}
if(isInBound(row + 1, col) {
recursion(row+1,col);
}
else {
return;
}
}
例如,在私有静态无效递归(..)的开头添加以下内容:
if(!isInBound(row,col)) {
return;
}
因此,递归调用对无效边界的任何递归调用都将简单地返回,而不会引发越界异常
private static void recursion(int row, int col) {
if(arry[row][col]=='1'){
System.out.println("Found at " + row + " " + col);
}
else if (arry[row][col]=='#'){
return;
}
if (isInBound(row,col + 1)){
recursion(row,col+1);
}
if(isInBound(row, col - 1)){
recursion(row, col - 1)
}
if(isInBound(row -1, col)){
recursion(row-1,col);
}
if(isInBound(row + 1, col) {
recursion(row+1,col);
}
else {
return;
}
}
假设你在跳绳,当你找到一个的时候
假设你正在跳过#的,当你找到一个时。这是有效的:
public class Test2 {
static char arry[][] = { { '#', '#', '#' }, { '#', ' ', ' ' },
{ '#', ' ', '#' }, { '#', ' ', '1' }, };
static boolean[][] visited = new boolean[arry.length][arry[0].length];
public static void main(String args[]) {
// fill visited array
for (int i = 0; i < visited.length; i++) {
for (int j = 0; j < visited[0].length; j++) {
visited[i][j] = false;
}
}
recursion(2, 1);
}
private static void recursion(int row, int col) {
if (!isInBound(row, col) || visited[row][col])
return;
visited[row][col] = true;
if (arry[row][col] == '1') {
System.out.println("Found at " + row + " " + col);
return;
} else if (arry[row][col] == ' ') {
recursion(row, col + 1);
recursion(row - 1, col);
recursion(row + 1, col);
recursion(row, col - 1);
}
}
private static boolean isInBound(int row, int col) {
boolean bol = false;
if (row < arry.length && col < arry[0].length && col >= 0 && row >= 0) {
bol = true;
}
return bol;
}
}
公共类Test2{
静态字符数组[][]={{{'#','#','#'},{'#','','','','},
{ '#', ' ', '#' }, { '#', ' ', '1' }, };
静态布尔值[][]已访问=新布尔值[arry.length][arry[0].length];
公共静态void main(字符串参数[]){
//填充访问数组
for(int i=0;i<0.length;i++){
for(int j=0;j<已访问[0]。长度;j++){
访问[i][j]=错误;
}
}
递归(2,1);
}
私有静态无效递归(int行,int列){
如果(!isInBound(行,列)| |访问[行][列])
返回;
访问[行][列]=真;
if(arry[行][col]=“1”){
System.out.println(“位于“+行+”+列);
返回;
}else if(arry[行][列]=''){
递归(行、列+1);
递归(行-1,列);
递归(行+1,列);
递归(行,列-1);
}
}
私有静态布尔值IsInBond(int行,int列){
布尔bol=假;
如果(行=0&&col>=0){
bol=真;
}
返回bol;
}
}
这项工作:
public class Test2 {
static char arry[][] = { { '#', '#', '#' }, { '#', ' ', ' ' },
{ '#', ' ', '#' }, { '#', ' ', '1' }, };
static boolean[][] visited = new boolean[arry.length][arry[0].length];
public static void main(String args[]) {
// fill visited array
for (int i = 0; i < visited.length; i++) {
for (int j = 0; j < visited[0].length; j++) {
visited[i][j] = false;
}
}
recursion(2, 1);
}
private static void recursion(int row, int col) {
if (!isInBound(row, col) || visited[row][col])
return;
visited[row][col] = true;
if (arry[row][col] == '1') {
System.out.println("Found at " + row + " " + col);
return;
} else if (arry[row][col] == ' ') {
recursion(row, col + 1);
recursion(row - 1, col);
recursion(row + 1, col);
recursion(row, col - 1);
}
}
private static boolean isInBound(int row, int col) {
boolean bol = false;
if (row < arry.length && col < arry[0].length && col >= 0 && row >= 0) {
bol = true;
}
return bol;
}
}
公共类Test2{
静态字符数组[][]={{{'#','#','#'},{'#','','','','},
{ '#', ' ', '#' }, { '#', ' ', '1' }, };
静态布尔值[][]已访问=新布尔值[arry.length][arry[0].length];
公共静态void main(字符串参数[]){
//填充访问数组
for(int i=0;i<0.length;i++){
for(int j=0;j<已访问[0]。长度;j++){
访问[i][j]=错误;
}
}
递归(2,1);
}
私有静态无效递归(int行,int列){
如果(!isInBound(行,列)| |访问[行][列])
返回;
访问[行][列]=真;
if(arry[行][col]=“1”){
System.out.println(“位于“+行+”+列);
返回;
}else if(arry[行][列]=''){
递归(行、列+1);
递归(行-1,列);
递归(行+1,列);
递归(行,列-1);
}
}
私有静态布尔值IsInBond(int行,int列){
布尔bol=假;
如果(行=0&&col>=0){
bol=真;
}
返回bol;
}
}
一旦修复此错误,您将需要以某种方式跟踪访问的单元格,或者您的搜索将在两个单元格之间来回摆动,直到堆栈溢出。一旦修复此错误,您将需要以某种方式跟踪访问的单元格,或者您的搜索将在两个单元格之间来回摆动,直到堆栈溢出W