Seren dev's blog
article thumbnail

https://www.acmicpc.net/problem/20436

 

20436번: ZOAC 3

첫 번째 줄에는 두 알파벳 소문자 sL, sR이 주어진다. sL, sR은 각각 왼손 검지손가락, 오른손 검지손가락의 처음 위치이다. 그 다음 줄에는 알파벳 소문자로 구성된 문자열이 주어진다. 문자열의

www.acmicpc.net

 

풀이

일단 입력한 알파벳 글자한글 자음 부분인지 모음 부분인지 구분해야 하고, 각 알파벳마다 위치 정보를 저장해야 한다.

 

각 글자마다의 위치 정보를 찾는 것을 용이하게 하기 위해 Map을 사용해서 키로 알파벳 글자를 저장하고, 값으로 해당하는 알파벳 글자의 위치 정보를 저장한다.

 

위치 정보를 저장하는 클래스 Position을 생성하고, Position 클래스는 int x, y, boolean flag를 필드 변수로 갖는다. x는 행 번호, y는 열 번호, flag는 왼손으로 치면 true, 오른손으로 치면 false이다.

 

q부터 시작해서 위치 정보 (x, y)는 (0,0) 부터 시작해서 마치 배열에 저장하듯이 위치 정보를 저장한다.

 

로직

1. HashMap<Character, Position> map 을 선언한다.

2. 키보드 첫줄("qwertyuiop")부터 시작해서 세번째 줄까지 map에 (알파벳, 위치 정보)를 저장한다.

3. 왼손 검지손가락, 오른손 검지손가락의 처음 위치를 입력받고, Position left, right에 각각 왼손, 오른손 처음 위치의 Position을 저장한다.

4.  문자열을 입력받고 문자열의 각 글자마다 아래 과정을 수행한다.

  • map에서 현재 글자의 위치 정보 를 찾고 p에 저장한다.
  • 각 키를 누르는 데에는 1의 시간이 걸리므로 cnt에 1을 더한다.
  • flag가 true면, p와 left의 거리 차이를 구해서 cnt에 더한다.
  • flag가 false면, p와 right의 거리 차이를 구해서 cnt에 더한다.

5. cnt를 출력한다.

 

코드

import java.io.*;
import java.util.*;

public class Main {
	
	static class Position {
		int x, y;
		boolean flag; //왼손이면 true, 오른손이면 false
		
		public Position(int x, int y, boolean flag) {
			this.x = x;
			this.y = y;
			this.flag = flag;
		}
	}
	

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

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        //BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        
        HashMap<Character, Position> map = new HashMap<>();
        int i = 0;
        
        // 키보드 첫번째 줄
        String tmp = "qwertyuiop";
        char[] arr = tmp.toCharArray();
        for (int j = 0; j < arr.length; j++) {
        	if (j <= 4)
        		map.put(arr[j], new Position(i, j, true));
        	else
        		map.put(arr[j], new Position(i, j, false));
        }
        
        // 키보드 두번째 줄
        i++;
        tmp = "asdfghjkl";
        arr = tmp.toCharArray();
        for (int j = 0; j < arr.length; j++) {
        	if (j <= 4)
        		map.put(arr[j], new Position(i, j, true));
        	else
        		map.put(arr[j], new Position(i, j, false));
        }
        
        // 키보드 세번째 줄
        i++;
        tmp = "zxcvbnm";
        arr = tmp.toCharArray();
        for (int j = 0; j < arr.length; j++) {
        	if (j <= 3)
        		map.put(arr[j], new Position(i, j, true));
        	else
        		map.put(arr[j], new Position(i, j, false));
        }
        
        
        StringTokenizer st = new StringTokenizer(br.readLine());
        
        char l = st.nextToken().charAt(0);
        char r = st.nextToken().charAt(0);
        
        Position left = map.get(l);
        Position right = map.get(r);
        
        String str = br.readLine();
        int cnt = 0;
        
        for (i = 0; i < str.length(); i++) {
        	char ch = str.charAt(i);
        	Position p = map.get(ch);
        	cnt++;
        	if (p.flag) {
        		cnt += Math.abs(p.x - left.x) + Math.abs(p.y - left.y); 
        		left = p;
        	}
        	else {
        		cnt += Math.abs(p.x - right.x) + Math.abs(p.y - right.y); 
        		right = p;
        	}
        }
        
        System.out.println(cnt);
        
        //bw.flush();
        //bw.close();
        //br.close();

    }
}

 


수정한 버전

map을 구성하고 시간을 계산하는 코드를 각각 makeMap(), calTime() 함수로 작성하여 가독성을 높이고 코드를 깔끔하게 다듬었다.

import java.io.*;
import java.util.*;

public class Main {
	
	static class Position {
		int x, y;
		boolean flag; //왼손이면 true, 오른손이면 false
		
		public Position(int x, int y, boolean flag) {
			this.x = x;
			this.y = y;
			this.flag = flag;
		}
	}

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

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        //BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        
        HashMap<Character, Position> map = makeMap();
        
        StringTokenizer st = new StringTokenizer(br.readLine());
        
        char l = st.nextToken().charAt(0);
        char r = st.nextToken().charAt(0);
        
        Position left = map.get(l);
        Position right = map.get(r);
        
        String str = br.readLine();
        
        int cnt = calTime(str, map, left, right);
        
        System.out.println(cnt);
        
        //bw.flush();
        //bw.close();
        //br.close();

    }
    
    public static HashMap<Character, Position> makeMap() {
		HashMap<Character, Position> map = new HashMap<>();
		int i = 0;
        
        // 키보드 첫번째 줄
        String tmp = "qwertyuiop";
        char[] arr = tmp.toCharArray();
        for (int j = 0; j < arr.length; j++) {
        	if (j <= 4)
        		map.put(arr[j], new Position(i, j, true));
        	else
        		map.put(arr[j], new Position(i, j, false));
        }
        
        // 키보드 두번째 줄
        i++;
        tmp = "asdfghjkl";
        arr = tmp.toCharArray();
        for (int j = 0; j < arr.length; j++) {
        	if (j <= 4)
        		map.put(arr[j], new Position(i, j, true));
        	else
        		map.put(arr[j], new Position(i, j, false));
        }
        
        // 키보드 세번째 줄
        i++;
        tmp = "zxcvbnm";
        arr = tmp.toCharArray();
        for (int j = 0; j < arr.length; j++) {
        	if (j <= 3)
        		map.put(arr[j], new Position(i, j, true));
        	else
        		map.put(arr[j], new Position(i, j, false));
        }
        
        return map;
	}
	
	public static int calTime(String str, HashMap<Character, Position> map, Position left, Position right) {
		int cnt = 0;
		
		for (int i = 0; i < str.length(); i++) {
        	char ch = str.charAt(i);
        	Position p = map.get(ch);
        	cnt++;
        	if (p.flag) {
        		cnt += Math.abs(p.x - left.x) + Math.abs(p.y - left.y); 
        		left = p;
        	}
        	else {
        		cnt += Math.abs(p.x - right.x) + Math.abs(p.y - right.y); 
        		right = p;
        	}
        }
		
		return cnt;
	}
}
728x90
profile

Seren dev's blog

@Seren dev

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!