Advertisement
Guest User

Untitled

a guest
Nov 26th, 2015
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.63 KB | None | 0 0
  1.  
  2.  import java.util.LinkedList;  
  3.  import java.util.Deque;  
  4.  import java.util.Queue;  
  5.  import java.util.Scanner;  
  6.    
  7.  public class Safe {  
  8.    
  9.    static class sequence_t {  
  10.    
  11.      public sequence_t(int n, sequence_t p, char c, int a) {  
  12.        currNum = n;  
  13.        prev = p;  
  14.        code = c;  
  15.        action = a;  
  16.      }  
  17.    
  18.      int currNum;  
  19.      char code;  // U or D  
  20.      int action; // Digit moved  
  21.      sequence_t prev;  
  22.    }  
  23.    
  24.    // Follow the links in a sequence ending at s, and display  
  25.    // the sequence  
  26.    static void print_sequence(sequence_t s, int codeLength, int depth) {  
  27.      LinkedList<String> out = new LinkedList<String>();  
  28.    
  29.      while (s.prev != null) {  
  30.        out.addFirst(String.format("%s%d %0" + codeLength + "d", s.code, codeLength - s.action, s.currNum));  
  31.        depth++;  
  32.        s = s.prev;  
  33.      }  
  34.      System.out.println(depth);  
  35.      for (String move : out) {  
  36.        System.out.println(move);  
  37.      }  
  38.    }  
  39.    
  40.    //Return number that results from taking the number in currNum,  
  41.    //moving digit specified by action, either 'U' or 'D' as determined  
  42.    //by value of c, for a code of length codeLength  
  43.    static int expandNode(int currNum, char c, int action, int codeLength) {  
  44.      int base;  
  45.      switch (action) {  
  46.        case 0:  
  47.          base = 1;  
  48.          break;  
  49.        case 1:  
  50.          base = 10;  
  51.          break;  
  52.        case 2:  
  53.          base = 100;  
  54.          break;  
  55.        case 3:  
  56.          base = 1000;  
  57.          break;  
  58.        case 4:  
  59.          base = 10000;  
  60.          break;  
  61.        case 5:  
  62.          base = 100000;  
  63.          break;  
  64.        case 6:  
  65.          base = 1000000;  
  66.          break;  
  67.        default:  
  68.          base = 10000000;  
  69.          break;  
  70.      }  
  71.      int digit = (currNum / base) % 10;  
  72.    
  73.      // Special cases  
  74.      if (digit == 9 && c == 'U') {  
  75.        return currNum - 9 * base;  
  76.      }  
  77.      if (digit == 0 && c == 'D') {  
  78.        return currNum + 9 * base;  
  79.      }  
  80.    
  81.      if (c == 'D') {  
  82.        return currNum - base;  
  83.      }  
  84.    
  85.      return currNum + base;  
  86.    }  
  87.    
  88.    //Direct Access Table that determines if numbers have already been visited  
  89.    static boolean[] prevNums;  
  90.    
  91.    public static void main(String[] args) {  
  92.      // The start code and target  
  93.      int nStart, nTarget;  
  94.      Scanner sc = new Scanner(System.in);  
  95.    
  96.      // Get start code and the code length  
  97.      String code;  
  98.      code = sc.next();  
  99.      int codeLength = code.length();  
  100.      prevNums = new boolean[(int) Math.pow(10, code.length())];  
  101.      nStart = Integer.valueOf(code);  
  102.    
  103.      // Get end code  
  104.      nTarget = Integer.valueOf(sc.next());  
  105.    
  106.      // Get forbidden codes  
  107.      while (sc.hasNext()) {  
  108.        prevNums[Integer.valueOf(sc.next())] = true;  
  109.      }  
  110.    
  111.      // Immediately output -1 if start or target are forbidden  
  112.      if (prevNums[nStart] || prevNums[nTarget]) {  
  113.        System.out.println("-1");  
  114.        return;  
  115.      }  
  116.    
  117.      // Store expanded nodes in a queue, so that we do a breadth first search of the code space  
  118.      Queue<sequence_t> Actions = new LinkedList<sequence_t>();  
  119.    
  120.      // Put the start code in queue  
  121.      sequence_t state = new sequence_t(nStart, null, 'n', -1);  
  122.      prevNums[nStart] = true;  
  123.      Actions.add(state);  
  124.    
  125.      // While there are items in the queue  
  126.      while (Actions.size() > 0) {  
  127.        // Grab front of queue  
  128.        sequence_t s = Actions.element();  
  129.        Actions.remove();  
  130.    
  131.        // Expand the current node by storing all possible targets in the queue  
  132.        for (int i = 0; i < codeLength; i++) {  
  133.          for (int j = 0; j < 2; j++) {  
  134.            int n;  
  135.            if (j == 0) {  
  136.              n = expandNode(s.currNum, 'U', i, codeLength);  
  137.            } else {  
  138.              n = expandNode(s.currNum, 'D', i, codeLength);  
  139.            }  
  140.            if (!prevNums[n]) {  
  141.              sequence_t next;  
  142.              if (j == 0) {  
  143.                next = new sequence_t(n, s, 'U', i);  
  144.              } else {  
  145.                next = new sequence_t(n, s, 'D', i);  
  146.              }  
  147.    
  148.              // If we have hit the target we're done  
  149.              if (n == nTarget) {  
  150.                print_sequence(next, codeLength, 0);  
  151.                return;  
  152.              }  
  153.              prevNums[n] = true;  
  154.              Actions.add(next);  
  155.            }  
  156.          }  
  157.        }  
  158.      }  
  159.      // The queue is empty, so we print out a -1  
  160.      System.out.println(-1);  
  161.    }  
  162.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement