[英]How to find escape routes from one point to another in a matrix?
This is not a homework. 这不是作业。 It's just a practice question.
这只是一个练习题。
Given a matrix find the number of possible escape routes from (0,0) to (N,N). 给定矩阵找到从(0,0)到(N,N)的可能逃逸路线的数量。 You cannot move diagonally.
你不能对角移动。
A '0' position represents an open cell, while a '1' represents a blocked cell. “0”位置表示开放单元,而“1”表示阻塞单元。 I started my journey from (0,0) and had to reach (N,N).
我从(0,0)开始我的旅程并且必须到达(N,N)。
Input format 输入格式
First line is a single odd positive integer, T (<= 85), which indicates the size of the matrix. 第一行是单个奇数正整数,T(<= 85),表示矩阵的大小。 T lines follow, each containing T space separated numbers which are either '0' or '1'.
随后是T行,每行包含T空格分隔的数字,这些数字为“0”或“1”。
Output format 输出格式
Output the number of ways in which I could have escaped from (0,0) to (N,N). 输出我可以从(0,0)转移到(N,N)的方式的数量。
Sample Input 样本输入
7
0 0 1 0 0 1 0
1 0 1 1 0 0 0
0 0 0 0 1 0 1
1 0 1 0 0 0 0
1 0 1 1 0 1 0
1 0 0 0 0 1 0
1 1 1 1 0 0 0
Sample Output 样本输出
4
According to my solution I have taken four directions - left (l), right(r), up(u), down(d). 根据我的解决方案,我采取了四个方向 - 左(l),右(r),上(u),下(d)。
The problem is that it is giving a wrong answer or a stackoverflow error. 问题是它给出了错误的答案或stackoverflow错误。 What is missing?
什么东西少了?
And is this the optimal solution to this question? 这是这个问题的最佳解决方案吗?
My Solution (Java) 我的解决方案(Java)
import java.io.BufferedReader;
import java.io.InputStreamReader;
class testclass {
int no_of_escapes = 0 ;
int[][] arr;
int matrixlength;
public static void main(String[] args) throws Exception
{
testclass obj = new testclass();
obj.checkpaths(0,0,"");
System.out.print(obj.no_of_escapes);
}//main
testclass()
{
try
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
matrixlength =Integer.parseInt(br.readLine());
arr = new int[matrixlength][matrixlength];
for( int k = 0; k < matrixlength; k++){
String str = br.readLine();
int count = 0;
for(int j=0 ; j< ((2*matrixlength)-1); j++){
int v = (int)str.charAt(j) - 48;
if(v == -16){}
else{
arr[k][count] = v;
count++;
}
}//for j
}//for k
}
catch(Exception e){}
}
public void checkpaths(int m, int n,String direction){
if((m == matrixlength -1) && (n == matrixlength-1))
{
no_of_escapes = no_of_escapes +1;
return;
}
if(!direction.equals("l"))
{
if(m < matrixlength && n < matrixlength)
{
if((n+1) < matrixlength )
{
if(arr[m][n+1]==0 )
{
checkpaths(m,n+1,"r");
}
}
}
}
if(!direction.equals("u"))
{
if((m+1) < matrixlength )
{
if(arr[m+1][n]==0 )
{
checkpaths(m+1,n,"d");
}
}
}
if(!direction.equals("r"))
{
if(m < matrixlength && n < matrixlength)
{
if((n+1) < matrixlength )
{
if(arr[m][n+1]==0 )
{
checkpaths(m,n+1,"l");
}
}
}
}
if(!direction.equals("d"))
{
if((m-1)>=0)
{
if(arr[m-1][n]==0 )
{
checkpaths(m-1,n,"u");
}
}
}
}
}//class
I would keep a second 2D array of booleans to mark the cells you already visited, as shown in the snippet below. 我会保留第二个二维2D阵列来标记您已经访问过的单元格,如下面的代码段所示。 I also simplified some other parts of the code, to reduce code-duplication.
我还简化了代码的其他部分,以减少代码重复。
Of course, you need to initialize visited
in your constructor, just as you initialized arr
, by using visited = new boolean[matrixLength][matrixLength]
. 当然,你需要初始化
visited
在你的构造,就像你初始化arr
,通过visited = new boolean[matrixLength][matrixLength]
int[][] arr;
boolean[][] visited;
final int[][] directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
public boolean isValid(int x, int y) {
return 0 <= x && x < matrixLength
&& 0 <= y && y < matrixLength
&& arr[x][y] == 0
&& !visited[x][y];
}
public void checkPaths(int x, int y) {
if (x == matrixLength-1 && y == matrixLength-1) {
no_of_escaped++;
} else {
for (int[] d : directions) {
if (isValid(x + d[0], y + d[1])) {
visited[x + d[0]][y + d[1]] = true;
checkPaths(x + d[0], y + d[1]);
visited[x + d[0]][y + d[1]] = false;
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.