Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- import java.text.*;
- public class treasureHunt
- {
- static void check (boolean[] [] traveled, String[] [] maze, int[] key, int[] treasure, int max, int xCoor, int yCoor)
- {
- for (int direction = 0 ; direction < 4 ; direction++)
- {
- int x = xCoor;
- int y = yCoor;
- if (direction == 0)
- {
- x = xCoor + 1;
- }
- else if (direction == 1)
- {
- x = xCoor - 1;
- }
- else if (direction == 2)
- {
- y = yCoor + 1;
- }
- else
- {
- y = yCoor - 1;
- }
- traveled [xCoor] [yCoor] = true;
- if (x < max && x >= 0 && y < max && y >= 0)
- {
- if (maze [x] [y].equals ("K"))
- {
- maze [x] [y] = "0";
- key [0]++;
- }
- else if (maze [x] [y].equals ("T"))
- {
- maze [x] [y] = "0";
- treasure [0]++;
- }
- if (!maze [x] [y].equals ("#") && !traveled [x] [y] && Integer.parseInt (maze [x] [y]) <= key [0])
- {
- check (traveled, maze, key, treasure, max, x, y);
- }
- }
- }
- }
- public static void main (String str[]) throws IOException
- {
- BufferedReader reader = new BufferedReader (new FileReader ("DATA21.txt"));
- for (int j = 0 ; j < 10 ; j++)
- {
- int max = Integer.parseInt (reader.readLine ());
- String[] [] maze = new String [max] [max];
- boolean[] [] traveled = new boolean [max] [max];
- int[] key = new int [1];
- int[] treasure = new int [1];
- String line;
- int sX, sY, pastKey;
- sX = sY = key [0] = treasure [0] = 0;
- for (int i = 0 ; i < max ; i++)
- {
- line = reader.readLine ();
- String[] x = line.split ("");
- for (int y = 0 ; y < max ; y++)
- {
- if (x [y + 1].equals ("."))
- {
- maze [i] [y] = "0";
- }
- else if (x [y + 1].equals ("S"))
- {
- maze [i] [y] = "0";
- sX = i;
- sY = y;
- }
- else
- {
- maze [i] [y] = x [y + 1];
- }
- }
- }
- do
- {
- pastKey = key [0];
- for (int i = 0 ; i < max ; i++)
- {
- for (int y = 0 ; y < max ; y++)
- {
- traveled [i] [y] = false;
- }
- }
- check (traveled, maze, key, treasure, max, sX, sY);
- }
- while (key [0] > pastKey);
- System.out.println (treasure [0]);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement