Advertisement
Guest User

treasureHunt

a guest
Mar 20th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.17 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3. import java.text.*;
  4.  
  5. public class treasureHunt
  6. {
  7.     static void check (boolean[] [] traveled, String[] [] maze, int[] key, int[] treasure, int max, int xCoor, int yCoor)
  8.     {
  9.         for (int direction = 0 ; direction < 4 ; direction++)
  10.         {
  11.             int x = xCoor;
  12.             int y = yCoor;
  13.             if (direction == 0)
  14.             {
  15.                 x = xCoor + 1;
  16.             }
  17.             else if (direction == 1)
  18.             {
  19.                 x = xCoor - 1;
  20.             }
  21.             else if (direction == 2)
  22.             {
  23.                 y = yCoor + 1;
  24.             }
  25.             else
  26.             {
  27.                 y = yCoor - 1;
  28.             }
  29.             traveled [xCoor] [yCoor] = true;
  30.  
  31.             if (x < max && x >= 0 && y < max && y >= 0)
  32.             {
  33.                 if (maze [x] [y].equals ("K"))
  34.                 {
  35.                     maze [x] [y] = "0";
  36.                     key [0]++;
  37.                 }
  38.                 else if (maze [x] [y].equals ("T"))
  39.                 {
  40.                     maze [x] [y] = "0";
  41.                     treasure [0]++;
  42.                 }
  43.                 if (!maze [x] [y].equals ("#") && !traveled [x] [y] && Integer.parseInt (maze [x] [y]) <= key [0])
  44.                 {
  45.                     check (traveled, maze, key, treasure, max, x, y);
  46.                 }
  47.             }
  48.         }
  49.     }
  50.  
  51.  
  52.     public static void main (String str[]) throws IOException
  53.     {
  54.         BufferedReader reader = new BufferedReader (new FileReader ("DATA21.txt"));
  55.         for (int j = 0 ; j < 10 ; j++)
  56.         {
  57.             int max = Integer.parseInt (reader.readLine ());
  58.  
  59.             String[] [] maze = new String [max] [max];
  60.             boolean[] [] traveled = new boolean [max] [max];
  61.             int[] key = new int [1];
  62.             int[] treasure = new int [1];
  63.             String line;
  64.             int sX, sY, pastKey;
  65.             sX = sY = key [0] = treasure [0] = 0;
  66.  
  67.             for (int i = 0 ; i < max ; i++)
  68.             {
  69.                 line = reader.readLine ();
  70.                 String[] x = line.split ("");
  71.                 for (int y = 0 ; y < max ; y++)
  72.                 {
  73.                     if (x [y + 1].equals ("."))
  74.                     {
  75.                         maze [i] [y] = "0";
  76.                     }
  77.                     else if (x [y + 1].equals ("S"))
  78.                     {
  79.                         maze [i] [y] = "0";
  80.                         sX = i;
  81.                         sY = y;
  82.                     }
  83.                     else
  84.                     {
  85.                         maze [i] [y] = x [y + 1];
  86.                     }
  87.                 }
  88.             }
  89.  
  90.             do
  91.             {
  92.                 pastKey = key [0];
  93.                 for (int i = 0 ; i < max ; i++)
  94.                 {
  95.                     for (int y = 0 ; y < max ; y++)
  96.                     {
  97.                         traveled [i] [y] = false;
  98.                     }
  99.                 }
  100.                 check (traveled, maze, key, treasure, max, sX, sY);
  101.             }
  102.             while (key [0] > pastKey);
  103.  
  104.             System.out.println (treasure [0]);
  105.         }
  106.     }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement