[BOJ] ๋ฑ(3190)
[๋ฐฑ์ค(BOJ)] 3190_๋ฑ JAVA
๋ฌธ์ : BOJ_3190๋ฒ ๋ฑ
๋ฌธ์ ์ค๋ช
๋งค์ด ์์ง์ด๋ฉด์ ๊ผฌ๋ฆฌ๋ฅผ ๋๋ ค๊ฐ๋ ๋ฑ ๊ฒ์์ ๊ตฌํํ๋ ๋ฌธ์ ์
๋๋ค.N*N
ํฌ๊ธฐ์ ๋งต์ด ์ฃผ์ด์ง๊ณ , ์ฌ๊ณผ๊ฐ ์๋ ๊ฐ๊ฐ์ ์์น์ ๋ฑ์ด ๋ฐฉํฅ์ ๋ฐ๊พธ๋ ์๊ฐ์ด ์ฃผ์ด์ง๋๋ค.
์ด๋ ๋ฑ์ด ์์ง์ธ ๋ฐฉํฅ์ ์ฌ๊ณผ๊ฐ ์๋ค๋ฉด ์ฌ๊ณผ๋ฅผ ๋จน๊ณ ๊ผฌ๋ฆฌ๊ฐ ๋์ด๋์ง๋ง, ์ฌ๊ณผ๊ฐ ์๋ค๋ฉด ๊ธฐ์กด์ ๊ผฌ๋ฆฌ ๋ถ๋ถ์ด ์ฌ๋ผ์ง๊ณ ๋ฑ์ ๋จธ๋ฆฌ ๋ถ๋ถ์ด ๋์ด๋ฉ๋๋ค.
๋ํ ๋ฐฉํฅ์ ์ต์ด์ ์ค๋ฅธ์ชฝ์ผ๋ก ์ด๋ํ๋ฉฐ ์ด๋๋ฐฉํฅ์ ๊ธฐ์ค์ผ๋ก ์ค๋ฅธ์ชฝ์ด๋ ์ผ์ชฝ์ผ๋ก 90๋ ํ์ ํ ์ ์์ต๋๋ค.
Solution
๊ตฌํ
๋ฌธ์ ์์ ์ฃผ์ด์ง ๋๋ก ๊ตฌํ์ ํ๋ฉด ๋ฉ๋๋ค.
๋ฑ์ ํ์ฌ ๋จธ๋ฆฌ ์์น์ ๊ผฌ๋ฆฌ ์์น๋ฅผ ๊ณ์ ๊ธฐ๋กํด ์ฃผ๋ฉด์, ์ด๋ํ ๊ณณ์ ์ฌ๊ณผ๊ฐ ์์ผ๋ฉด ๋จธ๋ฆฌ ์์น๋ง ์์ ํด ์ฃผ๊ณ , ์ฌ๊ณผ๊ฐ ์์ ์์ ๊ผฌ๋ฆฌ ์์น๋ ํจ๊ป ์์ ํด ์ค์ผ ํฉ๋๋ค.
์ด๋ ๊ผฌ๋ฆฌ ์์น๋ง๋ค ์ด๋ ๋ฐฉํฅ์ผ๋ก ์ด๋ํ๋์ง๋ ๊ธฐ๋กํด ์ฃผ์ด์ผ ํฉ๋๋ค.(lastMove)
๋ํ ๊ผฌ๋ฆฌ ์์น๊ฐ ๋ฐ๋๋ค๋ฉด ๋ง์ง๋ง ๊ผฌ๋ฆฌ ์์น์๋ ๋ฑ์ด ์กด์ฌํ์ง ์๋ ๊ฒ์ด๋ฏ๋ก ๋น ๊ณต๊ฐ์ผ๋ก ์์ ํด ์ฃผ์ด์ผ ํฉ๋๋ค.
์ด๋ ๋ฑ์ด ์กด์ฌํ๋ค๊ณ ์ฒดํฌํ ๋ถ๋ถ์ผ๋ก ์ด๋ํ๊ฑฐ๋, ์ง๋ ๋ฐ์ผ๋ก ๋ฒ์ด๋๋ ์๊ฐ์ ์ ๋ต์ผ๋ก ์ถ๋ ฅํ๋ฉด ๋ฉ๋๋ค.
Description
- dx[], dy[] ๋ฐฐ์ด์ ํตํด ์ด๋๋ฐฉํฅ์ ์ ํ ๋, ๋ฐฐ์ด์ ๊ฐ์์ +1์ผ ์์๋ ์ค๋ฅธ์ชฝ์ผ๋ก ํ์ , -1์ผ ์์๋ ์ผ์ชฝ์ผ๋ก ํ์ ํ๊ฒ ์ขํ๊ฐ์ ์ง์ ํด ์ฃผ์์ต๋๋ค.
- ๋ฑ์ ๋ฐฉํฅ ์ ๋ณด์์ ์๊ฐ์ธ X๋ X ์ด์ ๋ฐฉํฅ์ ํ์ ์ํค๋ ๊ฒ ์๋
X ์ด๊ฐ ๋๋ ๋ค์
๋ฐฉํฅ์ ์ ํํ๋ ๊ฒ์ ๋๋ค.(result==moveCnt+1)
import java.io.*;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
class Pair{
private int cnt;
private String direction;
Pair(int cnt, String direction){
this.cnt = cnt;
this.direction = direction;
}
public int getCnt() {
return cnt;
}
public String getDirection() {
return direction;
}
}
public class Main {
public static int[][] map;
public static int[][] lastMove;
public static int[] dx = {0,1,0,-1};
public static int[] dy = {1,0,-1,0};
public static int dir = 0;
public static int changeDir(int dir, String command){
if(command.equals("L")){
if(dir==0) return 3;
else return dir-1;
}
else {
if(dir==3) return 0;
else return dir+1;
}
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
map = new int[n+1][n+1];
lastMove = new int[n+1][n+1];
st = new StringTokenizer(br.readLine());
int k = Integer.parseInt(st.nextToken());
for(int i=0;i<k;i++){
st = new StringTokenizer(br.readLine());
int x = Integer.parseInt(st.nextToken());
int y = Integer.parseInt(st.nextToken());
map[x][y] = 2;
}
Queue<Pair> q = new LinkedList<>();
st = new StringTokenizer(br.readLine());
int l = Integer.parseInt(st.nextToken());
for(int i=0;i<l;i++){
st = new StringTokenizer(br.readLine());
int cnt = Integer.parseInt(st.nextToken());
String direction = st.nextToken();
q.offer(new Pair(cnt,direction));
}
dir = 0;
int result = 0;
map[1][1] = 1;
lastMove[1][1] = dir;
int nowX = 1;
int nowY = 1;
int endX = 1;
int endY = 1;
int moveCnt = 0;
String moveDir = "";
if(!q.isEmpty()) {
moveCnt = q.peek().getCnt();
moveDir = q.peek().getDirection();
}
while(true){
result++;
if(result==moveCnt+1){
q.poll();
dir = changeDir(dir,moveDir);
if(!q.isEmpty()) {
moveCnt = q.peek().getCnt();
moveDir = q.peek().getDirection();
}
}
int nextX = nowX + dx[dir];
int nextY = nowY + dy[dir];
if(nextX<=0 || nextY<=0 || nextX > n || nextY > n) break;
if(map[nextX][nextY]==1) break;
lastMove[nowX][nowY] = dir;
nowX = nextX;
nowY = nextY;
if(map[nextX][nextY]==2){
map[nextX][nextY] = 1;
continue;
}
else {
map[nextX][nextY] = 1;
map[endX][endY] = 0;
int nowDir= lastMove[endX][endY];
endX += dx[nowDir];
endY += dy[nowDir];
}
}
bw.write(String.valueOf(result));
bw.flush();
br.close();
bw.close();
}
}