Advertisement
Guest User

Untitled

a guest
Nov 16th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.01 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.*;
  5. public class treasure {
  6.     public static void main(String[] args) throws IOException{
  7.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  8.         StringTokenizer st = new StringTokenizer(br.readLine());
  9.         int r = Integer.parseInt(st.nextToken());
  10.         int c = Integer.parseInt(st.nextToken());
  11.         char[][] area = new char[r][c];
  12.         boolean finished = false;
  13.         int x= 0, y=0, tx=0, ty=0;
  14.         for (int i = 0; i<r; i++) {
  15.             String input = br.readLine();
  16.             if (input.contains("T")) {
  17.                 tx = i;
  18.                 ty = input.indexOf("T");
  19.             }
  20.             area[i] = input.toCharArray();
  21.         }
  22.        
  23.         HashMap<String, Boolean> map = new HashMap<String, Boolean>();
  24.         int steps = 0;
  25.         Integer[] treasure = {tx, ty};
  26.         Integer[] coords = {x,y};
  27.         while(!(coords.equals(treasure))) {
  28.             coords[0] = x;
  29.             coords[1] = y;
  30.             String s = "" + x + y;
  31.             if(coords[0] == tx && coords[1] == ty) {
  32.                 break;
  33.             }
  34.             char ch;
  35.             try{
  36.                 ch = area[x][y];
  37.             }catch (ArrayIndexOutOfBoundsException ex){
  38.                 System.out.println("Out");
  39.                 finished = true;
  40.                 break;
  41.                
  42.             }
  43.            
  44.             if (map.containsKey(s)) {
  45.                
  46.                 System.out.println(("Lost"));
  47.                 finished = true;
  48.                 break;
  49.             }
  50.             else {
  51.                 map.put(s, true);
  52.                 if (ch == 'E') y++;
  53.                 else if (ch == 'W') y--;
  54.                 else if (ch == 'N') x--;
  55.                 else if (ch == 'S') x++;
  56.                
  57.             }
  58.             steps++;
  59.         }
  60.         if(!finished) System.out.println(Integer.toString(steps));
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement