Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- //import java.util.*;
- public class _0M01_000187 {
- static BufferedReader fi;
- static BufferedWriter fo;
- static int i=0, j;
- static int maxi,maxj;
- static String line;
- static int linelen;
- static char[][] matrix;
- static boolean entry,beast;
- static int[] entryij = new int[2];
- static int[] beastij = new int[2];
- static StringBuffer out;
- static boolean found = false;
- static int nodes = 0;
- static int[][] visited = new int[1024][2];
- public static void main(String[] args) throws IOException {
- long start = System.nanoTime();
- fi = new BufferedReader(new FileReader(args[0]));
- // ------------------------------------->
- fo = new BufferedWriter(new FileWriter(",M01_000187.txt"));
- // -------------------------------------^
- while((line = fi.readLine()) != null) {
- linelen = line.length();
- doIJ();
- matrix = new char[maxi][maxj];
- entry = false;
- beast = false;
- for(int e=0; e<maxi && !found; e++) {
- i=e;
- line = fi.readLine();
- linelen = line.length();
- if (e == 0 || e == maxi-1) // prima e ultima linea
- parseLineBorder();
- else
- parseLineInside();
- if (entry && beast) {
- //try to search a path with incomplete matrix
- //printMatrix();
- String l = searchPath(entryij[0], entryij[1]);
- if (found) {
- createPath(l);
- //System.out.println(l);
- jump();
- }
- }
- if (!found) {
- nodes=0;
- visited = new int[1024][2];
- String l = searchPath(entryij[0], entryij[1]);
- createPath(l);
- }
- }
- fi.skip(1);
- entry = false;
- beast = false;
- found = false;
- }
- fi.close();
- //fo.write(out.toString());
- fo.close();
- System.out.println("time: "+((System.nanoTime()-start)/1000000)+"ms");
- }
- static void printMatrix() {
- for(int r=0;r<matrix.length;r++) {
- System.out.print("->");
- for(int g=0;g<matrix[r].length;g++)
- if (visited(r,g))
- System.out.print("# ");
- else
- System.out.print(matrix[r][g]+" ");
- System.out.println();
- }
- }
- static String searchPath(int _i, int _j) {
- String prova="";
- if (matrix[_i][_j] == 'X')
- nodes--; // MARKED.
- if(matrix[_i][_j] == 'B') {
- found = true;
- //System.out.println("FOUND");
- } else {
- visited[nodes][0] = _i;
- visited[nodes++][1] =_j;
- if (_j < maxj-2 && !found) {
- prova = searchWay(_i, _j, 'r');
- }
- if (_j > 1 && !found) {
- prova = searchWay(_i, _j, 'l');
- }
- if (_i > 1 && !found) {
- prova = searchWay(_i, _j,'u');
- }
- if (_i < maxi-2 && !found) {
- prova = searchWay(_i, _j, 'l');
- }
- }
- return prova;
- }
- static String searchWay(int _i, int _j, char dir) {
- if (!found) {
- if(dir == 'l' && !visited(_i, _j-1)) {
- return "l"+searchPath(_i, _j-1);
- }
- else if(dir == 'r' && !visited(_i, _j+1)) {
- return "r"+searchPath(_i, _j+1);
- }
- else if(dir == 'u' && !visited(_i-1, _j)) {
- return "u"+searchPath(_i-1, _j);
- }
- else if(dir == 'd' && !visited(_i+1, _j)) {
- return "d"+searchPath(_i+1, _j);
- }
- }
- return "";
- }
- static boolean visited(int a, int b) {
- for(int e=0;e<nodes+1;e++)
- if (visited[e][0] == a && visited[e][1] == b) {
- return true;
- }
- return false;
- }
- static void createPath(String l) throws IOException {
- out = new StringBuffer();
- char c;
- for(int e=0;e<l.length();e++) {
- c = l.charAt(e);
- if (c == 'u')
- out.append("UP ");
- else if(c== 'd')
- out.append("DOWN ");
- else if(c=='l')
- out.append("LEFT ");
- else if(c=='r')
- out.append("RIGHT ");
- }
- for(int e=l.length()-1;e>=0;e--) {
- c = l.charAt(e);
- if (c == 'u')
- out.append("DOWN ");
- else if(c== 'd')
- out.append("UP ");
- else if(c=='l')
- out.append("RIGHT ");
- else if(c=='r')
- out.append("LEFT ");
- }
- out.append('\n');
- fo.write(out.toString());
- }
- static void jump() throws IOException {
- fi.skip((maxi-i-1)*(maxj*2));
- System.out.println("skipped "+(maxi-i-1)+" lines");
- System.out.print("jumped to: ");
- }
- static void parseLineInside() throws IOException {
- // parsa solo linea 1 e ultima
- if (line.charAt(0) == ' ' && entry == false)
- foundEntry(i,0);
- else if (line.charAt(linelen-1) == ' ' && entry == false)
- foundEntry(i, maxj-1);
- char c;
- for (int e=0, t=0; e<linelen; e+=2, t++) {
- c = line.charAt(e);
- matrix[i][t] = c;
- if (c == 'B')
- foundBeast(i, t);
- }
- }
- // todo inverti x,y con i,j
- static void parseLineBorder() throws IOException {
- // parsa di tutta la linea interna
- if (line.charAt(0) == ' ' && entry == false)
- foundEntry(i,0);
- else if (line.charAt(linelen-1) == ' ' && entry == false)
- foundEntry(i, maxj-1);
- // parsing line
- for (int e=0, t=0; e<linelen; e+=2, t++) {
- matrix[i][t] = line.charAt(e);
- }
- for(int e=0;e<matrix[i].length;e++) {
- if(matrix[i][e] == ' ' && entry == false)
- foundEntry(i, e);
- }
- }
- static void doIJ() throws IOException {
- /*boolean first = true;
- int dec = 1;
- char c;*/
- int target=0;
- for(int e=0;e<linelen;e++)
- if (line.charAt(e) == 'x') {
- target = e;
- break;
- }
- maxj = Integer.parseInt(line.substring(0, target));
- maxi = Integer.parseInt(line.substring(target+1, linelen));
- /*
- for(int i=0;i < linelen; i++) {
- if ((c = line.charAt(i)) == 'x') {
- first = false;
- dec = 1;
- System.out.println(line.charAt(i+2));
- } else if (first == true) {
- maxx += (c-48)*dec;
- dec *= 10;
- } else if (first == false) {
- maxy += (c-48)*dec;
- dec *= 10;
- }
- }*/
- //fo.write("matrix:("+maxi+"*"+maxj+")\n");
- }
- static void foundEntry(int _i, int _j) throws IOException {
- entryij[0] = _i;
- entryij[1] = _j;
- entry = true;
- // System.out.println("found entry: ("+_i+","+_j+")");
- //fo.write("found entry: ("+_i+","+_j+")\n");
- }
- static void foundBeast(int _i, int _j) throws IOException {
- beastij[0] = _i;
- beastij[1] = _j;
- beast = true;
- // System.out.println
- //fo.write("found beast: ("+_i+","+_j+")\n");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement