Created
July 15, 2016 02:18
-
-
Save pxpc2/4bf3c16728de92b461050b5d1650abfc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package br.com.pxpc2.treino; | |
| import java.util.Scanner; | |
| public class TocaDois | |
| { | |
| private static int n; | |
| private static int m; | |
| private static int[][] sala; | |
| private static int x; | |
| private static int y; | |
| private static int xf; | |
| private static int yf; | |
| private static int[] vx = {0, 1, 0, -1}; | |
| private static int[] vy = {1, 0, -1, 0}; | |
| private static int[][] dist; | |
| public static void main(final String[] args) | |
| { | |
| final Scanner s = new Scanner(System.in); | |
| n = s.nextInt(); | |
| m = s.nextInt(); | |
| sala = new int[n][m]; | |
| dist = new int[n][m]; | |
| for (int nn = 0; nn < n; nn++) | |
| { | |
| for (int mm = 0; mm < m; mm++) | |
| { | |
| sala[nn][mm] = s.nextInt(); | |
| if (sala[nn][mm] == 2) | |
| { | |
| x= nn; | |
| y = mm; | |
| dist[x][y] = 1; | |
| } | |
| else if (sala[nn][mm] == 3) | |
| { | |
| xf = nn; | |
| yf = mm; | |
| dist[nn][mm] = -1; | |
| } | |
| else | |
| { | |
| dist[nn][mm] = -1; | |
| } | |
| } | |
| } | |
| dfs(x, y); | |
| System.out.print(dist[xf][yf]); | |
| } | |
| private static void dfs(int x, int y) | |
| { | |
| for (int k = 0; k < 4; k++) | |
| { | |
| int xx = x + vx[k]; | |
| int yy = y + vy[k]; | |
| if (xx < n && yy < m) | |
| { | |
| if (xx >= 0 && yy >= 0) | |
| { | |
| if (dist[xx][yy] < 0 && sala[xx][yy] > 0) | |
| { | |
| dist[xx][yy] = dist[x][y] + 1; | |
| dfs(xx, yy); | |
| } | |
| } | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment