Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- /**
- * Created by camp on 11/13/2014.
- */
- public class TaskD {
- public static final int LEFT = 0;
- public static final int UP = 1;
- public static final int RIGHT = 2;
- public static final int DOWN = 3;
- public static int h;
- public static int w;
- public static int direction = RIGHT;
- public static void main(String[] args) throws IOException {
- BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
- String[] input = reader.readLine().split(" ");
- h = Integer.parseInt(input[0]);
- w = Integer.parseInt(input[1]);
- char[][] matrix = new char[h][w];
- for (int i = 0; i < h; i++) {
- matrix[i] = reader.readLine().toCharArray();
- }
- int[] memory = new int[26];
- int current = 0;
- int n = Integer.parseInt(reader.readLine());
- int last = 0;
- int x = 0;
- int y = 0;
- for (int i = 0; i < 1000000; i++) {
- char command = matrix[x][y];
- switch (command) {
- case '#':
- System.exit(0);
- break;
- case '.':
- break;
- case '?':
- if (n > 0) {
- current = Integer.parseInt(reader.readLine());
- checkNumber(current);
- n--;
- last = current;
- } else {
- current = last;
- }
- break;
- case '!':
- System.out.println(current);
- current = 0;
- break;
- case '+':
- current++;
- checkNumber(current);
- break;
- case '-':
- current--;
- checkNumber(current);
- break;
- case '@':
- if (current == 0) {
- changeDirection(false);
- } else {
- changeDirection(true);
- }
- break;
- case '^':
- direction = UP;
- break;
- case '>':
- direction = RIGHT;
- break;
- case 'v':
- direction = DOWN;
- break;
- case '<':
- direction = LEFT;
- break;
- default:
- int temp = memory[command - 'A'];
- memory[command - 'A'] = current;
- current = temp;
- break;
- }
- x = nextX(x);
- y = nextY(y);
- if (!isCellValid(x, y)) {
- System.out.println("RUNTIME ERROR");
- System.exit(0);
- }
- }
- System.out.println("TIME LIMIT EXCEEDED");
- }
- public static void changeDirection(boolean clock) {
- if (clock) {
- direction++;
- if (direction == 4) {
- direction = 0;
- }
- } else {
- direction--;
- if (direction == -1) {
- direction = 3;
- }
- }
- }
- public static boolean isCellValid(int x, int y) {
- return (0 <= x && 0 <= y) && (x < h && y < w);
- }
- public static void checkNumber(int num) {
- if (!isNumberValid(num)) {
- System.out.println("OVERFLOW ERROR");
- System.exit(0);
- }
- }
- public static boolean isNumberValid(int num) {
- return (Math.abs(num) <= 100000);
- }
- public static int nextX(int x) {
- if (direction == UP) return x - 1;
- if (direction == DOWN) return x + 1;
- return x;
- }
- public static int nextY(int y) {
- if (direction == LEFT) return y - 1;
- if (direction == RIGHT) return y + 1;
- return y;
- }
- }
- /*
- 4 6
- ?A?v>v
- >..>@A
- -...A!
- ^A.+<#
- 2
- 2
- 3
- 2 4
- ?!>v
- ##^<
- 1
- 0
- 1 4
- ??!#
- 1
- 1
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement