[백준] 15797번 기차가 어둠을 헤치고 은하수를 (구현, java)

728x90
반응형

www.acmicpc.net/problem/15787

 

15787번: 기차가 어둠을 헤치고 은하수를

입력의 첫째 줄에 기차의 수 N(1 ≤ N ≤ 100000)과 명령의 수 M(1 ≤ M ≤ 100000)가 주어진다. 이후 두 번째 줄부터 M+1번째 줄까지 각 줄에 명령이 주어진다. 

www.acmicpc.net

package algo0428;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

public class B_15785_기차가어둠을헤치고_Main {
	static int n,m;
	static int[][] train;
	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		
		n=sc.nextInt();
		m=sc.nextInt();
		
		train=new int[n][20];
		for (int i = 0; i < m; i++) {
			int command=sc.nextInt();
			int num=sc.nextInt()-1;

			if(command==1) {
				int pos=sc.nextInt()-1;
				train[num][pos]=1;
			}else if(command==2) {
				int pos=sc.nextInt()-1;
				train[num][pos]=0;
			}else if(command==3) {
				for (int j = 18; j>=0; j--) {
					train[num][j+1]=train[num][j];//뒤로 옮기기
				}
				train[num][0]=0;
			}else {
				for (int j = 1; j < 20; j++) {
					train[num][j-1]=train[num][j];
				}
				train[num][19]=0;
			}
		}
		Set<String> set=new HashSet<>();
		for (int i = 0; i < n; i++) {
			String s=Arrays.toString(train[i]);
			System.out.println(s);
			set.add(s);
		}
		
		System.out.println(set.size());
	}
}
728x90
반응형
TAGS.

Comments