Advertisement
Guest User

Untitled

a guest
Aug 12th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.87 KB | None | 0 0
  1. import java.io.*;
  2. //import java.util.*;
  3.  
  4. public class _0M01_000187 {
  5.     static BufferedReader fi;
  6.     static BufferedWriter fo;
  7.  
  8.     static int i=0, j;
  9.     static int maxi,maxj;
  10.  
  11.     static String line;
  12.     static int linelen;
  13.  
  14.     static char[][] matrix;
  15.  
  16.     static boolean entry,beast;
  17.     static int[] entryij = new int[2];
  18.     static int[] beastij = new int[2];
  19.  
  20.     static StringBuffer out;
  21.     static boolean found = false;
  22.  
  23.     static int nodes = 0;
  24.     static int[][] visited = new int[1024][2];
  25.  
  26.     public static void main(String[] args) throws IOException {
  27.  
  28.         long start = System.nanoTime();
  29.  
  30.         fi = new BufferedReader(new FileReader(args[0]));
  31.         // ------------------------------------->
  32.         fo = new BufferedWriter(new FileWriter(",M01_000187.txt"));
  33.         // -------------------------------------^
  34.        
  35.         while((line = fi.readLine()) != null) {
  36.  
  37.             linelen = line.length();
  38.             doIJ();
  39.  
  40.             matrix = new char[maxi][maxj];
  41.  
  42.             entry = false;
  43.             beast = false;
  44.  
  45.  
  46.             for(int e=0; e<maxi && !found; e++) {
  47.                 i=e;
  48.                 line = fi.readLine();
  49.                 linelen = line.length();
  50.  
  51.                 if (e == 0 || e == maxi-1) // prima e ultima linea
  52.                     parseLineBorder();
  53.                 else
  54.                     parseLineInside();
  55.  
  56.                 if (entry && beast) {
  57.                     //try to search a path with incomplete matrix
  58.                    
  59.                     //printMatrix();
  60.                     String l = searchPath(entryij[0], entryij[1]);
  61.                     if (found) {
  62.                         createPath(l);
  63.                         //System.out.println(l);
  64.                         jump();
  65.                     }
  66.                 }
  67.  
  68.                 if (!found) {
  69.                     nodes=0;
  70.                     visited = new int[1024][2];
  71.                     String l = searchPath(entryij[0], entryij[1]);
  72.                     createPath(l);
  73.                 }
  74.             }
  75.             fi.skip(1);
  76.  
  77.  
  78.             entry = false;
  79.             beast = false;
  80.  
  81.             found = false;
  82.  
  83.         }
  84.  
  85.         fi.close();
  86.         //fo.write(out.toString());
  87.         fo.close();
  88.         System.out.println("time: "+((System.nanoTime()-start)/1000000)+"ms");
  89.  
  90.     }
  91.  
  92.     static void printMatrix() {
  93.         for(int r=0;r<matrix.length;r++) {
  94.             System.out.print("->");
  95.             for(int g=0;g<matrix[r].length;g++)
  96.                 if (visited(r,g))
  97.                     System.out.print("# ");
  98.                 else
  99.                     System.out.print(matrix[r][g]+" ");
  100.             System.out.println();
  101.         }
  102.     }
  103.  
  104.     static String searchPath(int _i, int _j) {
  105.         String prova="";
  106.         if (matrix[_i][_j] == 'X')
  107.             nodes--;   // MARKED.
  108.  
  109.         if(matrix[_i][_j] == 'B') {
  110.             found = true;
  111.             //System.out.println("FOUND");
  112.         } else {
  113.             visited[nodes][0] = _i;
  114.             visited[nodes++][1] =_j;
  115.             if (_j < maxj-2 && !found) {
  116.                 prova = searchWay(_i, _j, 'r');
  117.             }
  118.             if (_j > 1 && !found) {
  119.                 prova = searchWay(_i, _j, 'l');
  120.  
  121.             }
  122.             if (_i > 1 && !found) {
  123.                 prova = searchWay(_i, _j,'u');
  124.             }
  125.             if (_i < maxi-2 && !found) {
  126.                 prova = searchWay(_i, _j, 'l');  
  127.             }
  128.         }
  129.         return prova;
  130.     }
  131.  
  132.     static String searchWay(int _i, int _j, char dir) {
  133.         if (!found) {
  134.             if(dir == 'l' && !visited(_i, _j-1)) {
  135.                 return "l"+searchPath(_i, _j-1);
  136.             }
  137.             else if(dir == 'r' && !visited(_i, _j+1)) {
  138.                 return "r"+searchPath(_i, _j+1);
  139.             }
  140.             else if(dir == 'u' && !visited(_i-1, _j)) {
  141.                 return "u"+searchPath(_i-1, _j);
  142.             }
  143.             else if(dir == 'd' && !visited(_i+1, _j)) {
  144.                 return "d"+searchPath(_i+1, _j);
  145.             }
  146.         }
  147.         return "";
  148.     }
  149.    
  150.     static boolean visited(int a, int b) {
  151.         for(int e=0;e<nodes+1;e++)
  152.             if (visited[e][0] == a && visited[e][1] == b) {
  153.                 return true;
  154.             }
  155.         return false;
  156.     }
  157.  
  158.     static void createPath(String l) throws IOException {
  159.         out = new StringBuffer();
  160.         char c;
  161.         for(int e=0;e<l.length();e++) {
  162.             c = l.charAt(e);
  163.             if (c == 'u')
  164.                 out.append("UP ");
  165.             else if(c== 'd')
  166.                 out.append("DOWN ");
  167.             else if(c=='l')
  168.                 out.append("LEFT ");
  169.             else if(c=='r')
  170.                 out.append("RIGHT ");
  171.         }
  172.         for(int e=l.length()-1;e>=0;e--) {
  173.             c = l.charAt(e);
  174.             if (c == 'u')
  175.                 out.append("DOWN ");
  176.             else if(c== 'd')
  177.                 out.append("UP ");
  178.             else if(c=='l')
  179.                 out.append("RIGHT ");
  180.             else if(c=='r')
  181.                 out.append("LEFT ");
  182.         }
  183.         out.append('\n');
  184.         fo.write(out.toString());
  185.  
  186.     }
  187.  
  188.     static void jump() throws IOException {
  189.         fi.skip((maxi-i-1)*(maxj*2));
  190.         System.out.println("skipped "+(maxi-i-1)+" lines");
  191.         System.out.print("jumped to: ");
  192.     }
  193.  
  194.     static void parseLineInside() throws IOException {
  195.         // parsa solo linea 1 e ultima
  196.         if (line.charAt(0) == ' ' && entry == false)
  197.             foundEntry(i,0);
  198.         else if (line.charAt(linelen-1) == ' ' && entry == false)
  199.             foundEntry(i, maxj-1);
  200.  
  201.         char c;
  202.         for (int e=0, t=0; e<linelen; e+=2, t++) {
  203.             c = line.charAt(e);
  204.             matrix[i][t] = c;
  205.             if (c == 'B')
  206.                 foundBeast(i, t);
  207.         }
  208.     }
  209.  
  210.     // todo inverti x,y con i,j
  211.  
  212.     static void parseLineBorder() throws IOException {
  213.         // parsa di tutta la linea interna
  214.         if (line.charAt(0) == ' ' && entry == false)
  215.             foundEntry(i,0);
  216.         else if (line.charAt(linelen-1) == ' ' && entry == false)
  217.             foundEntry(i, maxj-1);
  218.  
  219.         // parsing line
  220.         for (int e=0, t=0; e<linelen; e+=2, t++) {
  221.             matrix[i][t] = line.charAt(e);
  222.         }
  223.  
  224.         for(int e=0;e<matrix[i].length;e++) {
  225.             if(matrix[i][e] == ' ' && entry == false)
  226.                 foundEntry(i, e);
  227.         }
  228.  
  229.     }
  230.  
  231.     static void doIJ() throws IOException {
  232.         /*boolean first = true;
  233.         int dec = 1;
  234.         char c;*/
  235.         int target=0;
  236.         for(int e=0;e<linelen;e++)
  237.             if (line.charAt(e) == 'x') {
  238.                 target = e;
  239.                 break;
  240.             }
  241.  
  242.         maxj = Integer.parseInt(line.substring(0, target));
  243.         maxi = Integer.parseInt(line.substring(target+1, linelen));
  244.         /*
  245.         for(int i=0;i < linelen; i++) {
  246.             if ((c = line.charAt(i)) == 'x') {
  247.                 first = false;
  248.                 dec = 1;
  249.                 System.out.println(line.charAt(i+2));
  250.             } else if (first == true) {
  251.                 maxx += (c-48)*dec;
  252.                 dec *= 10;
  253.             } else if (first == false) {
  254.                 maxy += (c-48)*dec;
  255.                 dec *= 10;
  256.             }
  257.  
  258.         }*/
  259.  
  260.         //fo.write("matrix:("+maxi+"*"+maxj+")\n");
  261.     }
  262.  
  263.     static void foundEntry(int _i, int _j) throws IOException {
  264.         entryij[0] = _i;
  265.         entryij[1] = _j;
  266.         entry = true;
  267. //        System.out.println("found entry: ("+_i+","+_j+")");
  268.         //fo.write("found entry: ("+_i+","+_j+")\n");
  269.     }
  270.  
  271.     static void foundBeast(int _i, int _j) throws IOException {
  272.         beastij[0] = _i;
  273.         beastij[1] = _j;
  274.         beast = true;
  275. //        System.out.println
  276.         //fo.write("found beast: ("+_i+","+_j+")\n");
  277.     }
  278.  
  279. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement