1. You are given a number n, representing the number of rows.
2. You are given a number m, representing the number of columns.Input Format
3. You are given n*m numbers (1's and 0's), representing elements of 2d array a.
4. Consider this array a maze and a player enters from top-left corner in east direction.
5. The player moves in the same direction as long as he meets '0'. On seeing a 1, he takes a 90 deg right turn.
6. You are required to print the indices in (row, col) format of the point from where you exit the matrix.
A number nOutput Format
A number m
e11
e12..
e21
e22..
.. n * m number of elements
row
col (of the point of exit)
Constraints
1 <= n <= 10^2
1 <= m <= 10^2
e1, e2, .. n * m elements belongs to the set (0, 1)
Sample Input
4
4
0
0
1
0
1
0
0
0
0
0
0
0
1
0
1
0
Sample Output
1
3import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
// write your code here
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
int[][] A = new int[n][m];
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
A[i][j] = sc.nextInt();
int dir = 0, i = 0,j=0;
while(true){
dir = (dir + A[i][j]) % 4;
switch(dir){
case 0: j++; break;
case 1: i++; break;
case 2: j--; break;
case 3: i--; break;
}
if(i<0){
i++;
break;
}else if(i==n){
i--;
break;
}else if(j<0){
j++;
break;
}else if(j==n){
j--;
break;
}
}
System.out.println(i+"\n"+j);
}
}
Comments
Post a Comment