import java.io.BufferedReader;

import java.io.File;

import java.io.FileReader;

import java.io.InputStreamReader;

import java.util.StringTokenizer;



class Solution {


static int DataLen;

static String[][] map;

static boolean Debug = false;


public static void main(String args[]) throws Exception {


BufferedReader br;

if(Debug) {

File f = new File("C:\\Users\\wvimi\\eclipse-workspace\\HelloWorld2\\input", "input (0).txt");

FileReader reader = new FileReader(f);

br = new BufferedReader(reader);

}else {

br = new BufferedReader(new InputStreamReader(System.in)); // 직접입력시 이용

}

DataLen = Integer.parseInt(br.readLine());

int CommandLen = Integer.parseInt(br.readLine());


// 입력부

StringTokenizer st; 

map = new String[DataLen][];

for (int i = 0; i < DataLen; i++) {

map[i] = new String[DataLen];

st = new StringTokenizer(br.readLine());

for (int j = 0; j < DataLen; j++) {

map[i][j] = st.nextToken();

}

}


// 실행부

String result = "";

int y, x, num;

for (int i = 0; i < CommandLen; i++) {

st = new StringTokenizer(br.readLine());

y = Integer.parseInt(st.nextToken()) -1;

x = Integer.parseInt(st.nextToken()) -1;

num = Integer.parseInt(st.nextToken());

Amount = 0;

goPath(y,x, num);

result = String.format("#%d %d", i+1, Amount);

System.out.println(result);

}


}


static int rotX[] = { 1, 0, -1, 0 };

static int rotY[] = { 0, 1, 0, -1 };

static int Amount;

static private void goPath(int y, int x, int num) {

if(num==0) {

Amount = (map[y][x].charAt(1) - '0') * 1000 ;

return;

}


int move_len = map[y][x].charAt(1) - '0'; // 거리

int rot; // 방위

switch (map[y][x].charAt(0)) {

case 'E': // +x

rot = 0;

break;

case 'W': // -x

rot = 2;

break;

case 'S': // y+

rot = 1;

break;

case 'N': // y-

rot = 3;

break;

default: // input case Error

rot = -1; 

}

int move_y = y + move_len* rotY[rot];

int move_x = x + move_len* rotX[rot];

if(isRight(move_y, move_x)) {

goPath(move_y, move_x, num-1);

}else {

Amount = 10000;

}

}


static private boolean isRight(int y, int x) {

if (x < 0 || x > DataLen - 1 || y < 0 || y > DataLen - 1) {

return false;

}


return true;

}


private static void log(String input) {

if (true) {

System.out.println(input);

}

}


}

+ Recent posts