Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.*;
- public class treasure {
- public static void main(String[] args) throws IOException{
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- StringTokenizer st = new StringTokenizer(br.readLine());
- int r = Integer.parseInt(st.nextToken());
- int c = Integer.parseInt(st.nextToken());
- char[][] area = new char[r][c];
- boolean finished = false;
- int x= 0, y=0, tx=0, ty=0;
- for (int i = 0; i<r; i++) {
- String input = br.readLine();
- if (input.contains("T")) {
- tx = i;
- ty = input.indexOf("T");
- }
- area[i] = input.toCharArray();
- }
- HashMap<String, Boolean> map = new HashMap<String, Boolean>();
- int steps = 0;
- Integer[] treasure = {tx, ty};
- Integer[] coords = {x,y};
- while(!(coords.equals(treasure))) {
- coords[0] = x;
- coords[1] = y;
- String s = "" + x + y;
- if(coords[0] == tx && coords[1] == ty) {
- break;
- }
- char ch;
- try{
- ch = area[x][y];
- }catch (ArrayIndexOutOfBoundsException ex){
- System.out.println("Out");
- finished = true;
- break;
- }
- if (map.containsKey(s)) {
- System.out.println(("Lost"));
- finished = true;
- break;
- }
- else {
- map.put(s, true);
- if (ch == 'E') y++;
- else if (ch == 'W') y--;
- else if (ch == 'N') x--;
- else if (ch == 'S') x++;
- }
- steps++;
- }
- if(!finished) System.out.println(Integer.toString(steps));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement