Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 1st, 2012  |  syntax: None  |  size: 2.38 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1.  
  2. import java.util.Scanner;
  3. import java.io.*;
  4.  
  5. public class Bugbunny {
  6.   public static void main(String[] args) throws IOException {
  7.     Scanner in = new Scanner(System.in);
  8.     int a = in.nextInt();
  9.     int a1 = 0;
  10.    
  11.     while (a1 < a) {
  12.       int elmer = in.nextInt();
  13.       int b = in.nextInt();
  14.       int inbug = in.nextInt();
  15.       String[][] move = new String[b][elmer + 1];
  16.       for (int i = 0; i < b; i++) {
  17.         String c = in.next();
  18.         for (int j = 0; j < c.length(); j++) {
  19.           move[i][j] = c.substring(j, j + 1);
  20.         }
  21.       }
  22.      
  23.       boolean[][] tree = new boolean[elmer + 1][(int)Math.pow(2,(b + 1))+1];
  24.  
  25.      
  26.       tree[0][inbug] = true;
  27.       for (int i = 1; i < elmer + 1; i++) {
  28.         tree[i][1] = true;
  29.       }
  30.       boolean d = false;
  31.       for (int i = 0; i < b; i++) {
  32.         for (int j = 0; j < elmer + 1; j++) {
  33.           if (samebugnode(tree) > 0) {
  34.             System.out.print((i+1) + " " + samebugnode(tree));
  35.             d = true;
  36.             break;
  37.           }
  38.           if (move[i][j].equals("L")) lmove(tree, posbugel(tree, j), j);
  39.           if (move[i][j].equals("R")) rmove(tree, posbugel(tree, j), j);
  40.           if (move[i][j].equals("U")) umove(tree, posbugel(tree, j), j);
  41.          
  42.           if (samebugnode(tree) > 0) {
  43.             System.out.print((i+1) + " " + samebugnode(tree));
  44.             d = true;
  45.             break;
  46.           }
  47.         }
  48.         if (d) break;
  49.       }
  50.      if(a1<a-1) System.out.println("");
  51.     if(!d)System.out.print("0 0");
  52.  
  53.     a1++;
  54.    
  55.     }
  56.  
  57.  
  58.   }
  59.  
  60.   public static void lmove(boolean[][] tree, int pos, int no) {
  61.     tree[no][pos] = false;
  62.     tree[no][pos * 2] = true;
  63.   }
  64.  
  65.   public static void rmove(boolean[][] tree, int pos, int no) {
  66.     tree[no][pos] = false;
  67.     tree[no][pos * 2 + 1] = true;
  68.   }
  69.  
  70.   public static void umove(boolean[][] tree, int pos, int no) {
  71.     tree[no][pos] = false;
  72.     tree[no][pos / 2] = true;
  73.   }
  74.  
  75.   public static int posbugel(boolean[][] tree,int no) {
  76.     for (int i = 0; i < tree[no].length; i++) {
  77.       if (tree[no][i]) return i;
  78.     }
  79.     return 0;
  80.   }
  81.    
  82.   public static int samebugnode(boolean[][] tree) {
  83.     for (int i = 0; i < tree[0].length; i++) {
  84.       for(int j =1;j<tree.length;j++){
  85.         if (tree[0][i]) if (tree[j][i]) return j;
  86.       }
  87.     }
  88.     return 0;
  89.   }
  90.  
  91.  
  92.  
  93.  
  94. }