SDJ( 수돈재 아님 ㅎ )

[Java] 8911 - 거북이 본문

알고리즘/Backjoon

[Java] 8911 - 거북이

ShinDongJun 2020. 7. 1. 16:58

문제 링크 : https://www.acmicpc.net/problem/8911

 

8911번: 거북이

문제 상근이는 2차원 평면 위에서 움직일 수 있는 거북이 로봇을 하나 가지고 있다. 거북이 로봇에게 내릴 수 있는 명령은 다음과 같이 네가지가 있다. F: 한 눈금 앞으로 B: 한 눈금 뒤로 L: 왼쪽��

www.acmicpc.net

 

JAVA공부한겸 한번 객체로 만들어서 풀어보고싶어서 끄적거리면서 풀어본 문제

아직 뭐가 뭐하는놈인지 몰라서 일단 작성하고봤는데 빨리 익숙해져야할듯

 

import java.util.*;

public class Main {

	static class turtle{
		private int x;
		private int y;
		
		private int MAX_X;
		private int MAX_Y;
		
		private int MIN_X;
		private int MIN_Y;
		
		private int[][] dir;
		private int[] cur;
		private int idx;
		
		public turtle() {
			this.x = 0;
			this.y = 0;
			this.MAX_X = 0;
			this.MAX_Y = 0;
			this.MIN_X = 0;
			this.MIN_Y = 0;
			
			this.idx = 0;
			this.dir = new int[][] {{0, 1}, {-1, 0}, {0, -1}, {1, 0}};
			this.cur = dir[idx];
		}
		
		public void F() {
			this.x += cur[0];
			this.y += cur[1];
			
			if(this.MAX_X < this.x)
				this.MAX_X = this.x;
			else if(this.x < this.MIN_X)
				this.MIN_X = this.x;
			
			if(this.MAX_Y < this.y)
				this.MAX_Y = this.y;
			else if(this.y < this.MIN_Y)
				this.MIN_Y = this.y;
		}
		
		public void B() {
			this.x -= cur[0];
			this.y -= cur[1];
			
			if(this.MAX_X < this.x)
				this.MAX_X = this.x;
			else if(this.x < this.MIN_X)
				this.MIN_X = this.x;
			
			if(this.MAX_Y < this.y)
				this.MAX_Y = this.y;
			else if(this.y < this.MIN_Y)
				this.MIN_Y = this.y;
		}
		
		public void L() {
			this.idx = (this.idx+1)%4;
			this.cur = dir[idx];
		}

		public void R() {
			this.idx = ((this.idx-1)%4 >= 0 ? (this.idx-1)%4 : 3);
			this.cur = dir[idx];
		}
		
		public int end() {
			return Math.abs((this.MAX_Y-this.MIN_Y)*(this.MAX_X-this.MIN_X));
		}
	}
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		int T;
		String command = "";
		
		T = sc.nextInt();
		while(T != 0)
		{
			turtle Turtle = new turtle();
			command = sc.next();
			for(int i = 0; i < command.length(); ++i)
			{
				if(command.charAt(i) == 'F')
					Turtle.F();
				else if(command.charAt(i) == 'B')
					Turtle.B();
				else if(command.charAt(i) == 'L')
					Turtle.L();
				else if(command.charAt(i) == 'R')
					Turtle.R();
			}
			System.out.println(Turtle.end());
			T -= 1;
		}
		
		
	}

}

'알고리즘 > Backjoon' 카테고리의 다른 글

[C++] 17267 - 상남자  (0) 2020.07.01
[C++] 1013 - Contact  (0) 2020.06.30
[C] 1021 - 회전하는 큐  (0) 2020.06.30
[C++] 12904 - A와 B  (0) 2020.06.30
[C++] 1806 - 부분합  (0) 2020.02.01
Comments