Advertisement
misteraverin

Untitled

Nov 27th, 2014
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.26 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4.  
  5. /**
  6.  * Created by camp on 11/13/2014.
  7.  */
  8. public class TaskD {
  9.  
  10.     public static final int LEFT = 0;
  11.     public static final int UP = 1;
  12.     public static final int RIGHT = 2;
  13.     public static final int DOWN = 3;
  14.  
  15.     public static int h;
  16.     public static int w;
  17.     public static int direction = RIGHT;
  18.  
  19.     public static void main(String[] args) throws IOException {
  20.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  21.         String[] input = reader.readLine().split(" ");
  22.         h = Integer.parseInt(input[0]);
  23.         w = Integer.parseInt(input[1]);
  24.         char[][] matrix = new char[h][w];
  25.         for (int i = 0; i < h; i++) {
  26.             matrix[i] = reader.readLine().toCharArray();
  27.         }
  28.  
  29.         int[] memory = new int[26];
  30.         int current = 0;
  31.  
  32.         int n = Integer.parseInt(reader.readLine());
  33.         int last = 0;
  34.  
  35.         int x = 0;
  36.         int y = 0;
  37.         for (int i = 0; i < 1000000; i++) {
  38.             char command = matrix[x][y];
  39.             switch (command) {
  40.                 case '#':
  41.                     System.exit(0);
  42.                     break;
  43.                 case '.':
  44.                     break;
  45.                 case '?':
  46.                     if (n > 0) {
  47.                         current = Integer.parseInt(reader.readLine());
  48.                         checkNumber(current);
  49.                         n--;
  50.                         last = current;
  51.                     } else {
  52.                         current = last;
  53.                     }
  54.                     break;
  55.                 case '!':
  56.                     System.out.println(current);
  57.                     current = 0;
  58.                     break;
  59.                 case '+':
  60.                     current++;
  61.                     checkNumber(current);
  62.                     break;
  63.                 case '-':
  64.                     current--;
  65.                     checkNumber(current);
  66.                     break;
  67.                 case '@':
  68.                     if (current == 0) {
  69.                         changeDirection(false);
  70.                     } else {
  71.                         changeDirection(true);
  72.                     }
  73.                     break;
  74.                 case '^':
  75.                     direction = UP;
  76.                     break;
  77.                 case '>':
  78.                     direction = RIGHT;
  79.                     break;
  80.                 case 'v':
  81.                     direction = DOWN;
  82.                     break;
  83.                 case '<':
  84.                     direction = LEFT;
  85.                     break;
  86.                 default:
  87.                     int temp = memory[command - 'A'];
  88.                     memory[command - 'A'] = current;
  89.                     current = temp;
  90.                     break;
  91.             }
  92.  
  93.             x = nextX(x);
  94.             y = nextY(y);
  95.  
  96.  
  97.             if (!isCellValid(x, y)) {
  98.                 System.out.println("RUNTIME ERROR");
  99.                 System.exit(0);
  100.             }
  101.         }
  102.  
  103.         System.out.println("TIME LIMIT EXCEEDED");
  104.     }
  105.  
  106.     public static void changeDirection(boolean clock) {
  107.         if (clock) {
  108.             direction++;
  109.             if (direction == 4) {
  110.                 direction = 0;
  111.             }
  112.         } else {
  113.             direction--;
  114.             if (direction == -1) {
  115.                 direction = 3;
  116.             }
  117.         }
  118.     }
  119.  
  120.     public static boolean isCellValid(int x, int y) {
  121.         return (0 <= x && 0 <= y) && (x < h && y < w);
  122.     }
  123.  
  124.     public static void checkNumber(int num) {
  125.         if (!isNumberValid(num)) {
  126.             System.out.println("OVERFLOW ERROR");
  127.             System.exit(0);
  128.         }
  129.     }
  130.  
  131.     public static boolean isNumberValid(int num) {
  132.         return (Math.abs(num) <= 100000);
  133.     }
  134.  
  135.     public static int nextX(int x) {
  136.         if (direction == UP) return x - 1;
  137.         if (direction == DOWN) return x + 1;
  138.         return x;
  139.     }
  140.  
  141.     public static int nextY(int y) {
  142.         if (direction == LEFT) return y - 1;
  143.         if (direction == RIGHT) return y + 1;
  144.         return y;
  145.     }
  146.  
  147. }
  148.  
  149. /*
  150. 4 6
  151. ?A?v>v
  152. >..>@A
  153. -...A!
  154. ^A.+<#
  155. 2
  156. 2
  157. 3
  158.  
  159.  
  160. 2 4
  161. ?!>v
  162. ##^<
  163. 1
  164. 0
  165.  
  166.  
  167. 1 4
  168. ??!#
  169. 1
  170. 1
  171.  */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement