14503번: 로봇 청소기

로봇 청소기가 주어졌을 때, 청소하는 영역의 개수를 구하는 프로그램을 작성하시오. 로봇 청소기가 있는 장소는 N×M 크기의 직사각형으로 나타낼 수 있으며, 1×1크기의 정사각형 칸으로 나누어

www.acmicpc.net

 

 

 


 

 

 

 

  • 내용

NxM 크기의 직사각형의 공간을 로봇 청소기가 청소할 수 있다. 각각의 칸은 벽 또는 빈칸이다. 청소기는 동, 서, 남, 북으로 바라보는 방향이 있다.
지도의 각 칸은 (r,c)로 나타낼 수 이쏙, r은 북쪽으로부터 떨어진 칸의 개수, c는 서쪽으로 부터 떨어진 칸의 개수이다.

로봇 청소기는 이미 청소되어있는 칸을 또 청소하지 않으며, 벽을 통과할 수 없다.
로봇 청소기의 작동 방법은 다음과 같다.
1. 현재 위치를 청소한다.
2. 현재 위치에서 현재 방향을 기준으로 왼쪽방향부터 차례대로 탐색을 진행한다.
   a. 왼쪽 방향에 아직 청소하지 않은 공간이 존재한다면, 그 방향으로 회전한 다음 한 칸을 전진하고 1번부터 진행한다.
   b. 왼쪽 방향에 청소할 공간이 없다면, 그 방향을 ㅗ회전하고 2번으로 돌아간다.
   c. 네 방향 모두 청소가 이미 되어있거나 벽인 경우에는, 바라보는 방향을 유지한 채로 한 칸 후진을 하고 2번으로 돌       아간다.
   d. 네 방향 모두 청소가 이미 되어있거나 벽이면서, 뒤쪽 방향이 벽이라 후진도 할 수 없는 경우에는 작동을 멈춘다.


 

 

 

  • 생각

 

queue를 이용해서 풀면 쉽게 풀 수 있을 것 같다. queue로 현재 위치와 방향을 offer해가면서 이동 가능할때만 queue에 넣고 4방향 모두 가지못할땐 뒤의 방향이 벽인지 확인해주고 벽이 아니면 queue에 offer, 벽이면 종료

 

 

 

 

  • 코드

 

import java.io.IOException;
import java.io.InputStream;
import java.util.InputMismatchException;
import java.util.LinkedList;
import java.util.Queue;

public class Main {
	static int[] x =  { -1, 0, 1, 0 };
    static int[] y = { 0, 1, 0, -1 };
	static int[][] array;
	static Queue<int[]> queue;
	static int N, M, answer;

	public static void main(String[] args) throws Exception {
		SetData();
		System.out.println(answer);
	}

	// 데이터
	private static void SetData() throws Exception {
		InputReader in = new InputReader(System.in);

		N = in.nextInt();
		M = in.nextInt();
		array = new int[N][M];
		queue = new LinkedList<>();
		queue.offer(new int[] {in.nextInt(), in.nextInt(), in.nextInt()});
		answer = 1;

		for (int i = 0; i < N; i++) {
			for (int j = 0; j < M; j++) {
				array[i][j] = in.nextInt();
			}
		}

		OperateRobot();
	}

	private static void OperateRobot() {
		while (!queue.isEmpty()) {
			int[] robot = queue.poll();
			int robotX = robot[0];
			int robotY = robot[1];
			int d = robot[2];
			// 청소
			array[robotX][robotY] = 2;

            boolean check = false;    // 4 방향 모두 갈 수 없을 때
            int r;
            int c;
            int nextD;
 
            for (int i = 0; i < 4; i++) {
                d = (d + 3) % 4;    
                r = robotX + x[d];    
                c = robotY + y[d];    

                if (r < 0 || c < 0 || r >= N || c >= M)   continue;
                
                //다음 이동할 위치가  청소되지 않은 곳이라면 간다.
                if (array[r][c] == 0) {
                    queue.add(new int[] {r,c, d});
        			answer++;
                    check = true;
                    break;
                }
            }
            
            // 네 방향 모두 청소됐거나 벽일 경우 후진
            if (!check) {
                nextD = (d + 2) % 4;
                r = robotX + x[nextD];
                c = robotY + y[nextD];
 
                // 후진을 못할 경우
                if (array[r][c] != 1) {
                    queue.add(new int[] {r, c, d});
                }
            }

		}
	}
}

class InputReader {
	private final InputStream stream;
	private final byte[] buf = new byte[8192];
	private int curChar, snumChars;

	public InputReader(InputStream st) {
		this.stream = st;
	}

	public int read() {
		if (snumChars == -1)
			throw new InputMismatchException();
		if (curChar >= snumChars) {
			curChar = 0;
			try {
				snumChars = stream.read(buf);
			} catch (IOException e) {
				throw new InputMismatchException();
			}
			if (snumChars <= 0)
				return -1;
		}
		return buf[curChar++];
	}

	public int nextInt() {
		int c = read();
		while (isSpaceChar(c)) {
			c = read();
		}
		int sgn = 1;
		if (c == '-') {
			sgn = -1;
			c = read();
		}
		int res = 0;
		do {
			res *= 10;
			res += c - '0';
			c = read();
		} while (!isSpaceChar(c));
		return res * sgn;
	}

	public long nextLong() {
		int c = read();
		while (isSpaceChar(c)) {
			c = read();
		}
		int sgn = 1;
		if (c == '-') {
			sgn = -1;
			c = read();
		}
		long res = 0;
		do {
			res *= 10;
			res += c - '0';
			c = read();
		} while (!isSpaceChar(c));
		return res * sgn;
	}

	public int[] nextIntArray(int n) {
		int a[] = new int[n];
		for (int i = 0; i < n; i++) {
			a[i] = nextInt();
		}
		return a;
	}

	public String nextLine() {
		int c = read();
		while (isSpaceChar(c))
			c = read();
		StringBuilder res = new StringBuilder();
		do {
			res.appendCodePoint(c);
			c = read();
		} while (!isEndOfLine(c));
		return res.toString();
	}

	public boolean isSpaceChar(int c) {
		return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
	}

	private boolean isEndOfLine(int c) {
		return c == '\n' || c == '\r' || c == -1;
	}
}

 

13460번: 구슬 탈출 2

첫 번째 줄에는 보드의 세로, 가로 크기를 의미하는 두 정수 N, M (3 ≤ N, M ≤ 10)이 주어진다. 다음 N개의 줄에 보드의 모양을 나타내는 길이 M의 문자열이 주어진다. 이 문자열은 '.', '#', 'O', 'R', 'B'

www.acmicpc.net

 

 

 


 

 

 

  • 내용

 

스타트링크가 있다. 스타트링크의 테두리는 막혀있고, 스타트링크 안에는 빨간 구슬, 파란 구슬, 스타트링크를 빠져나갈 수 있는 구멍이 있다. 게임의 목표는 빨간 구슬을 스타트링크를 빠져나갈 수 있는 구멍이다.

 

빼내는 방법은 중력을 이용해서 빼내야한다. 상, 하, 좌, 우로 기울이면 중력에따라서 구슬이 움직이는 구조이다.

 

최소 몇번의 상, 하, 좌, 우로 기울여서 빨간 구슬을 스타트링크에서 빼낼 수 있는지 출력하면 된다. 단, 10번 이하로 움직여서 빼낼 수 없으면 -1을 출력한다.

 

 

 

  • 생각

 

재귀 형식으로 네 방향을 돌려가면서 10번 이하일때 골인할 때만 answer 변수를 초기화 시켜주었다.

 

 

 

 

  • 코드

 

import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.LinkedList;
import java.util.Queue;

public class Algorithm {
	static int[] x = { -1, 1, 0, 0 };
	static int[] y = { 0, 0, -1, 1 };
	static int N, M, answer;

	public static void main(String[] args) throws Exception {
		SetData();
		System.out.println(answer);
	}

	// 데이터
	private static void SetData() throws Exception {
		InputReader in = new InputReader(System.in);

		N = in.nextInt();
		M = in.nextInt();
		char[][] array = new char[N][M];
		answer = Integer.MAX_VALUE;
		int bX = 0, bY = 0, rX = 0, rY = 0;
		
		String s;
		for(int i = 0; i < N; i++) {
			s = in.nextLine();
			for(int j = 0 ; j < M; j++) {
				array[i][j] = s.charAt(j);
				if(array[i][j] == 'B') {
					bX = i;
					bY = j;
				}
				if(array[i][j] == 'R') {
					rX = i;
					rY = j;
				}
			}
		}
		
		for(int i = 0; i < 4; i++) {
			bfs(array, i, 0, bX, bY, rX, rY);
		}
		
		if(answer == Integer.MAX_VALUE) answer = -1;
	}
	
	private static void bfs(char[][] array,int index, int count, int bX, int bY, int rX, int rY) {
		if(count > 9) {
			return;
		}
		int blueX = bX;
		int blueY = bY;
		int redX = rX;
		int redY = rY;
		/*
		System.out.println("\n");
		for(int i = 0; i < N; i++) {
			for(int j = 0; j < M; j++) {
				System.out.print(array[i][j]);
			}
			System.out.println();
		}*/
			
		
		boolean check = false, check2 = false;
		while(true) {
			// move
			blueX += x[index];
			blueY += y[index];
			redX += x[index];
			redY += y[index];
			
			// 움직일 수 없는 경우
			if((array[blueX][blueY] == '#' || (blueX + x[index] == redX && blueY + y[index] == redY)) &&
					(array[redX][redY] == '#' || (redX + x[index] == blueX && redY + y[index] == blueY))) {
				blueX -= x[index];
				blueY -= y[index];
				redX -= x[index];
				redY -= y[index];
				if(array[blueX+x[index]][blueY+y[index]] == 'O')
					check = true;
				break;
			}
			
			// 앞이 막혀있는 경우
			if(array[blueX][blueY] == '#') {
				blueX -= x[index];
				blueY -= y[index];
			}
			
			if(array[redX][redY] == '#') {
				redX -= x[index];
				redY -= y[index];
			}
			
			// 골인
			if(array[redX][redY] == 'O') {
				check2 = true; 
			}
			
			// 잘못된 골인
			if(array[blueX][blueY] == 'O') {
				check = true;
				break;				
			}
			
		}
		
		if(!check && check2) {
			answer = Math.min(answer, count+1);
			return;
		}
		
		if(!check) {
			char temp = array[blueX][blueY];
			array[blueX][blueY] = array[bX][bY];
			array[bX][bY] = temp;
			temp = array[redX][redY];
			array[redX][redY] = array[rX][rY];
			array[rX][rY] = temp;
			count++;
		} else {
			blueX = bX;
			blueY = bY;
			redX = rX;
			redY = rY;
		}
		
		for(int i = 0; i < 4; i++) {
			if(i == index) continue;
			
			bfs(array, i, count, blueX, blueY, redX, redY);
		}
	}
}

class InputReader {
	private final InputStream stream;
	private final byte[] buf = new byte[8192];
	private int curChar, snumChars;

	public InputReader(InputStream st) {
		this.stream = st;
	}

	public int read() {
		if (snumChars == -1)
			throw new InputMismatchException();
		if (curChar >= snumChars) {
			curChar = 0;
			try {
				snumChars = stream.read(buf);
			} catch (IOException e) {
				throw new InputMismatchException();
			}
			if (snumChars <= 0)
				return -1;
		}
		return buf[curChar++];
	}

	public int nextInt() {
		int c = read();
		while (isSpaceChar(c)) {
			c = read();
		}
		int sgn = 1;
		if (c == '-') {
			sgn = -1;
			c = read();
		}
		int res = 0;
		do {
			res *= 10;
			res += c - '0';
			c = read();
		} while (!isSpaceChar(c));
		return res * sgn;
	}

	public long nextLong() {
		int c = read();
		while (isSpaceChar(c)) {
			c = read();
		}
		int sgn = 1;
		if (c == '-') {
			sgn = -1;
			c = read();
		}
		long res = 0;
		do {
			res *= 10;
			res += c - '0';
			c = read();
		} while (!isSpaceChar(c));
		return res * sgn;
	}

	public int[] nextIntArray(int n) {
		int a[] = new int[n];
		for (int i = 0; i < n; i++) {
			a[i] = nextInt();
		}
		return a;
	}

	public String nextLine() {
		int c = read();
		while (isSpaceChar(c))
			c = read();
		StringBuilder res = new StringBuilder();
		do {
			res.appendCodePoint(c);
			c = read();
		} while (!isEndOfLine(c));
		return res.toString();
	}

	public boolean isSpaceChar(int c) {
		return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
	}

	private boolean isEndOfLine(int c) {
		return c == '\n' || c == '\r' || c == -1;
	}
}

 

 

  • 다른 사람의 더 좋은 코드

큐와 노드를 통해서 되게 깔끔하게 작성했다.. 똑똑하다...

 

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;

public class Main {

	static int N, M; // N 세로, M 가로
	static int[] dirx = { -1, 0, 1, 0 };
	static int[] diry = { 0, -1, 0, 1 };
	static char map[][];

	static boolean[][][][] visit;

	static Queue<Node> queue = new LinkedList<Node>();

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

		InputStreamReader ir = new InputStreamReader(System.in);
		BufferedReader br = new BufferedReader(ir);

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

		N = Integer.parseInt(st.nextToken());
		M = Integer.parseInt(st.nextToken());

		map = new char[M][N];
		visit = new boolean[M][N][M][N];

		Node initNode = new Node(0, 0, 0, 0, 0);

		for (int n = 0; n < N; n++) {
			String token = br.readLine();
			for (int m = 0; m < M; m++) {
				char tmp = token.charAt(m);
				map[m][n] = tmp;
				if (tmp == 'R') {
					initNode.rx = m;
					initNode.ry = n;
				}
				if (tmp == 'B') {
					initNode.bx = m;
					initNode.by = n;
				}
			}
		}

		queue.offer(initNode);

		while (!queue.isEmpty()) {
			Node node = queue.poll();

			visit[node.rx][node.ry][node.bx][node.by] = true;

			if (node.count >= 10)
			{
				continue;
			}

			for (int i = 0; i < 4; i++) {
				int bx = node.bx;
				int by = node.by;
				int rx = node.rx;
				int ry = node.ry;
				// 파란색 구슬부터 굴림.
				while (map[bx + dirx[i]][by + diry[i]] != '#') {
					bx += dirx[i];
					by += diry[i];
					if (map[bx][by] == 'O')
						break;
				}

				if (map[bx][by] == 'O')
					continue;

				while (map[rx + dirx[i]][ry + diry[i]] != '#') {
					rx += dirx[i];
					ry += diry[i];
					if (map[rx][ry] == 'O')
						break;
				}

				if (map[rx][ry] == 'O') {
					System.out.println(node.count + 1);
					return;
				}

				// 겹치는 경우 처리
				if (bx == rx && by == ry) {
					switch (i) {
					case 0:
						if (node.bx > node.rx) {
							bx += 1;
						} else
							rx += 1;
						break;
					case 1:
						if (node.by > node.ry) {
							by += 1;
						} else
							ry += 1;
						break;
					case 2:
						if (node.bx > node.rx) {
							rx -= 1;
						} else
							bx -= 1;
						break;
					case 3:
						if (node.by > node.ry) {
							ry -= 1;
						} else
							by -= 1;
						break;
					}
				}

				if (!visit[rx][ry][bx][by]) {
					queue.offer(new Node(rx, ry, bx, by, node.count + 1));
				}
			}
		}
		System.out.println(-1);
	}
}

class Node {
	public int rx;
	public int ry;
	public int bx;
	public int by;
	public int count;

	public Node() {
	}

	public Node(int rx, int ry, int bx, int by, int count) {
		this.rx = rx;
		this.ry = ry;
		this.bx = bx;
		this.by = by;
		this.count = count;
	}
}

 

 

17144번: 미세먼지 안녕!

미세먼지를 제거하기 위해 구사과는 공기청정기를 설치하려고 한다. 공기청정기의 성능을 테스트하기 위해 구사과는 집을 크기가 R×C인 격자판으로 나타냈고, 1×1 크기의 칸으로 나눴다. 구사

www.acmicpc.net

 

 

 

 


 

 

  • 내용

공기청정기(1열에 2칸을 차지하고있음)랑 A(r,c)위치에 미세먼지 양이 적혀있다.

 

A(r,c)에 있는 미세먼지 양은 턴 한번마다 네방향(상, 하, 좌, 우)로 퍼질 수 있고 퍼지는 양은 A(r,c)/5만큼 퍼진다.(소수점 버림) 퍼진 뒤 A(r,c)의 미세먼지 양은 A(r,c) - A(r,c)/5 X (퍼진방향수)로 바뀐다.

공기청정기는 위에 2칸이 있다고 있는데 위칸은 시계 반대방향으로 불고, 아래칸은 시계 방향으로 분다.

 

한 턴마다 미세먼지가 먼저 퍼지고 공기청정기의 바람이 분다.

이때, T턴 후 남아있는 미세먼지의 양을 출력한다.

 

 

 

  • 생각

 

T초마다 미세먼지 퍼뜨리고 공기청정기 돌리면 된다. (딱히 문법 사용할거 없이 구현하면 될 것 같다.)

 

미세먼지 양은 네 방향으로 퍼뜨려도 총 미세먼지의 양은 변화가 없다. (공기청정기가 불어서 미세먼지가 줄어드는 2칸만 총양에서 빼주면 됨.)

 

 

 

  • 코드

 

import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.InputMismatchException;

public class Main {
	static int[] x = { -1, 1, 0, 0 };
	static int[] y = { 0, 0, -1, 1 };
	static int[][] array, temp;
	static int r, c, answer, airCleaner1, airCleaner2;

	public static void main(String[] args) throws Exception {
		SetData();
		System.out.println(answer);
	}

	// 데이터
	private static void SetData() throws Exception {
		InputReader in = new InputReader(System.in);

		answer = 2;
		r = in.nextInt();
		c = in.nextInt();
		int testcase = in.nextInt();
		array = new int[r][c];
		temp = new int[r][c];
		
		for (int i = 0; i < r; i++) {
			for (int j = 0; j < c; j++) {
				array[i][j] = in.nextInt();
				answer += array[i][j];
				if (array[i][j] < 0) {
					if (airCleaner1 == 0) {
						airCleaner1 = i;
					} else {
						airCleaner2 = i;
					}
				}

			}
		}

		while (testcase-- > 0) {
			SpreadFineDust();
			PlayAirCleaner();
			CopyMap(temp, array);
		}
	}

	private static void SpreadFineDust() {
		for(int i = 0; i < r; i++)
			Arrays.fill(temp[i], 0);
		
		for (int i = 0; i < r; i++) {
			for (int j = 0; j < c; j++) {
				temp[i][j] += array[i][j];

				if(temp[i][j] == -1) continue;
				if(array[i][j] == 0) continue;
				
				for (int direction = 0; direction < 4; direction++) {
					int R = i + x[direction];
					int C = j + y[direction];

					if (R < 0 || C < 0 || R >= r || C >= c)	continue;
					if (array[R][C] == -1)	continue;

					temp[R][C] += (array[i][j] / 5);
					temp[i][j] -= (array[i][j] / 5);
				}
			}
		}
	}

	private static void PlayAirCleaner() {
		// 위쪽 공기청정기는 반시계방향
		int top = airCleaner1;

		answer -= temp[top-1][0];
		for (int x = top - 1; x > 0; x--) {
			temp[x][0] = temp[x - 1][0];
		}

		for (int y = 0; y < c - 1; y++) {
			temp[0][y] = temp[0][y + 1];
		}

		for (int x = 0; x < top; x++) {
			temp[x][c - 1] = temp[x + 1][c - 1];
		}

		for (int y = c - 1; y > 1; y--) {
			temp[top][y] = temp[top][y - 1];
		}

		temp[top][1] = 0;

		// 아래쪽 공기청정기는 시계 방향
		int bottom = airCleaner2;

		answer -= temp[bottom + 1][0];
		for (int x = bottom + 1; x < r - 1; x++) {
			temp[x][0] = temp[x + 1][0];
		}

		for (int y = 0; y < c - 1; y++) {
			temp[r - 1][y] = temp[r - 1][y + 1];
		}

		for (int x = r - 1; x > bottom; x--) {
			temp[x][c - 1] = temp[x - 1][c - 1];
		}

		for (int y = c - 1; y > 1; y--) {
			temp[bottom][y] = temp[bottom][y - 1];
		}

		temp[bottom][1] = 0;
	}
	
	static void CopyMap(int[][] one,int[][] two) {
		for(int i=0;i<one.length;++i) {
			System.arraycopy(one[i], 0, two[i], 0, two[i].length);
		}
	}
}

class InputReader {
	private final InputStream stream;
	private final byte[] buf = new byte[8192];
	private int curChar, snumChars;

	public InputReader(InputStream st) {
		this.stream = st;
	}

	public int read() {
		if (snumChars == -1)
			throw new InputMismatchException();
		if (curChar >= snumChars) {
			curChar = 0;
			try {
				snumChars = stream.read(buf);
			} catch (IOException e) {
				throw new InputMismatchException();
			}
			if (snumChars <= 0)
				return -1;
		}
		return buf[curChar++];
	}

	public int nextInt() {
		int c = read();
		while (isSpaceChar(c)) {
			c = read();
		}
		int sgn = 1;
		if (c == '-') {
			sgn = -1;
			c = read();
		}
		int res = 0;
		do {
			res *= 10;
			res += c - '0';
			c = read();
		} while (!isSpaceChar(c));
		return res * sgn;
	}

	public long nextLong() {
		int c = read();
		while (isSpaceChar(c)) {
			c = read();
		}
		int sgn = 1;
		if (c == '-') {
			sgn = -1;
			c = read();
		}
		long res = 0;
		do {
			res *= 10;
			res += c - '0';
			c = read();
		} while (!isSpaceChar(c));
		return res * sgn;
	}

	public int[] nextIntArray(int n) {
		int a[] = new int[n];
		for (int i = 0; i < n; i++) {
			a[i] = nextInt();
		}
		return a;
	}

	public String nextLine() {
		int c = read();
		while (isSpaceChar(c))
			c = read();
		StringBuilder res = new StringBuilder();
		do {
			res.appendCodePoint(c);
			c = read();
		} while (!isEndOfLine(c));
		return res.toString();
	}

	public boolean isSpaceChar(int c) {
		return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
	}

	private boolean isEndOfLine(int c) {
		return c == '\n' || c == '\r' || c == -1;
	}
}

 

1525번: 퍼즐

세 줄에 걸쳐서 표에 채워져 있는 아홉 개의 수가 주어진다. 한 줄에 세 개의 수가 주어지며, 빈 칸은 0으로 나타낸다.

www.acmicpc.net

 

 

 

 


 

 

 

 

  • 생각

 

입력한 배열에서 0인 곳이 비어있는 곳인데 비어있는 곳에서 상, 하, 좌, 우로 한번씩 움직이면서

1 2 3

4 5 6

7 8 0

 

처럼 출력이 나오게 만들면 되는 문제이다. (단, 만들지 못할시 -1 출력)

 

푼 방식은 bfs 방식이다.

 

배열로 입력을 받으려 했는데 문제가 많았다. (메모리, 시간문제)

 

변경한 방법은 정수형으로 받아서 hashSet을 사용했다.

hashSet에 지나온 정수형을 모두 추가한다. (ex, 123456789, 312456897) 0이 없는 이유는 01234568로 추가를 하면 1234568로 추가가 되기 때문에 0을 9로 바꾸어 주었다.

 

위치를 바꿀 수 있는 것은 9의 위치이기 때문에 해당 위치를 찾고 상, 하, 좌, 우 중에서 움직일 수 있는 곳으로 움직인다. (단, hashSet에 추가되어있는 것들은 이미 지나와서 123456789를 만들거나 만들지 못했던 것들이기 때문에 건너띄어준다.)

 

 

 

 

  • 코드

 

import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.HashSet;
import java.util.InputMismatchException;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Set;

public class Main {
	static int[] x = { -1, 1, 0, 0 };
	static int[] y = { 0, 0, -1, 1 };
	static int answer, count;
	static Set<Integer> check;
	static Queue<int[]> queue;

	public static void main(String[] args) throws Exception {
		SetData();
		System.out.println(answer);
	}

	// 데이터
	private static void SetData() throws Exception {
		InputReader in = new InputReader(System.in);

		answer = Integer.MAX_VALUE;
		count = 0;
		check = new HashSet<Integer>();
		queue = new LinkedList<>();
		int temp = 0, input = 0;

		for (int i = 0; i < 3; i++) {
			for (int j = 0; j < 3; j++) {
				input = in.nextInt();
				if (input == 0)
					input = 9;
				temp = temp * 10 + input;
			}
		}

		check.add(temp);
		queue.offer(new int[] {temp, 0});

		bfs();
		
		if(answer == Integer.MAX_VALUE) answer = -1;
	}

	private static void bfs() {
		int nowNumber[], nowIndex, nowRow, nowColumn, nextNumber;
		String nowString;
		while (!queue.isEmpty()) {
			nowNumber = queue.poll();
			if (nowNumber[0] == 123456789)
				answer = Math.min(answer, nowNumber[1]);

			nowString = String.valueOf(nowNumber[0]);
			nowIndex = nowString.indexOf('9');
			nowRow = nowIndex / 3;
			nowColumn = nowIndex % 3;

			for (int direction = 0; direction < 4; direction++) {
				int r = nowRow + x[direction];
				int c = nowColumn + y[direction];

				if (r < 0 || c < 0 || r >= 3 || c >= 3)			continue;

				int nextIndex = r * 3 + c;	// 이동할 index

				StringBuilder next = new StringBuilder(nowString);
				char temp = next.charAt(nextIndex);
				next.setCharAt(nextIndex, '9');
				next.setCharAt(nowIndex, temp);
				nextNumber = Integer.parseInt(next.toString());	//이동한 값
				
				if(check.contains(nextNumber)) continue;
				
				count++;
				check.add(nextNumber);
				queue.offer(new int[] {nextNumber, nowNumber[1] + 1});
				count--;
			}

		}
	}
}

class InputReader {
	private final InputStream stream;
	private final byte[] buf = new byte[8192];
	private int curChar, snumChars;

	public InputReader(InputStream st) {
		this.stream = st;
	}

	public int read() {
		if (snumChars == -1)
			throw new InputMismatchException();
		if (curChar >= snumChars) {
			curChar = 0;
			try {
				snumChars = stream.read(buf);
			} catch (IOException e) {
				throw new InputMismatchException();
			}
			if (snumChars <= 0)
				return -1;
		}
		return buf[curChar++];
	}

	public int nextInt() {
		int c = read();
		while (isSpaceChar(c)) {
			c = read();
		}
		int sgn = 1;
		if (c == '-') {
			sgn = -1;
			c = read();
		}
		int res = 0;
		do {
			res *= 10;
			res += c - '0';
			c = read();
		} while (!isSpaceChar(c));
		return res * sgn;
	}

	public long nextLong() {
		int c = read();
		while (isSpaceChar(c)) {
			c = read();
		}
		int sgn = 1;
		if (c == '-') {
			sgn = -1;
			c = read();
		}
		long res = 0;
		do {
			res *= 10;
			res += c - '0';
			c = read();
		} while (!isSpaceChar(c));
		return res * sgn;
	}

	public int[] nextIntArray(int n) {
		int a[] = new int[n];
		for (int i = 0; i < n; i++) {
			a[i] = nextInt();
		}
		return a;
	}

	public String nextLine() {
		int c = read();
		while (isSpaceChar(c))
			c = read();
		StringBuilder res = new StringBuilder();
		do {
			res.appendCodePoint(c);
			c = read();
		} while (!isEndOfLine(c));
		return res.toString();
	}

	public boolean isSpaceChar(int c) {
		return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
	}

	private boolean isEndOfLine(int c) {
		return c == '\n' || c == '\r' || c == -1;
	}
}

 

1963번: 소수 경로

소수를 유난히도 좋아하는 창영이는 게임 아이디 비밀번호를 4자리 ‘소수’로 정해놓았다. 어느 날 창영이는 친한 친구와 대화를 나누었는데: “이제 슬슬 비번 바꿀 때도 됐잖아” “응 지금

www.acmicpc.net

 

 

 


 

 

 

  • 생각

 

testcase만큼 4자리인 왼쪽 수를 오른쪽 수로 한자리씩 소수로만 만들면서 변경해야한다.

 

dfs방식으로 basecase를 왼쪽 수와 오른쪽 수가 같아질때로 만든 뒤 한자리씩 소수로만 변경하면서 돌리면 어떨까??

- 4자리의 숫자를 한개씩 바꾸며 재귀방식으로 구현하면 4자리의 수에서 한개를 골라 변경한다. (이 때 변경한 수는 소수) basecase는 첫번째 입력한 수와 두번째 입력이 같을 때

==>> 무한루프에 빠진다. 이유는 첫번째 수가 두번째 수로 가지 못할 확률이 있는데 그 부분에서 basecase처리를 못해서 계속 재귀에 빠지게 된다.

 

bfs방식으로 해서 queue가 다 돌았는데 첫번째 수가 두번째 수로 가지못할 때 Impossible을 출력해주는 방향으로 가면 위의 재귀처럼 무한루프에 빠지지 않을 것 같다.

 

 

 

  • 코드

 

 

import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.LinkedList;
import java.util.Queue;

public class Main {
	static boolean[] primes;
	static Queue<int[]> queue;
	static StringBuilder sb;

	public static void main(String[] args) throws Exception {
		SetData();
		System.out.println(sb);
	}

	// 데이터
	private static void SetData() throws Exception {
		InputReader in = new InputReader(System.in);

		sb = new StringBuilder();

		int testcase = in.nextInt();
		primes = new boolean[10000];
		
		for (int i = 0; i < testcase; i++) {
			Arrays.fill(primes, false);
			bfs(in.nextInt(), in.nextInt());
		}
	}

	private static void bfs(int preNumber, int postNumber) {
		queue = new LinkedList<>();
		queue.add(new int[] { preNumber, 0 });
		if (preNumber == postNumber) {
			sb.append(0).append("\n");
			return;
		}
		while (!queue.isEmpty()) {
			int[] primeRoot = queue.poll();
			if (!primes[primeRoot[0]]) {
				// 지나온건 다시 안돌아가도록
				primes[primeRoot[0]] = true;
				
				char[] numbers = Integer.toString(primeRoot[0]).toCharArray();

				for (int i = 0; i <= 9; i++) {
					for (int j = 0; j < 4; j++) {
						if (j == 0 && i == 0)   continue;
						
						char temp = numbers[j];
						numbers[j] = (char) (i + '0');
						int changedPrimeNumber = Integer.parseInt(String.valueOf(numbers));
						numbers[j] = temp;
						if (primes[changedPrimeNumber])		continue;
						if (check(changedPrimeNumber)) {
							if (changedPrimeNumber == postNumber) {
								sb.append(primeRoot[1] + 1).append("\n");
								return;
							}
							queue.add(new int[] { changedPrimeNumber, primeRoot[1] + 1 });
						}
					}
				}
			}
		}
		sb.append("Impossible").append("\n");
	}

	private static boolean check(int number) {
		for (int i = 2; i <= (int) Math.sqrt(number); i++) {
			if (number % i == 0)
				return false;
		}
		return true;
	}
}

class InputReader {
	private final InputStream stream;
	private final byte[] buf = new byte[8192];
	private int curChar, snumChars;

	public InputReader(InputStream st) {
		this.stream = st;
	}

	public int read() {
		if (snumChars == -1)
			throw new InputMismatchException();
		if (curChar >= snumChars) {
			curChar = 0;
			try {
				snumChars = stream.read(buf);
			} catch (IOException e) {
				throw new InputMismatchException();
			}
			if (snumChars <= 0)
				return -1;
		}
		return buf[curChar++];
	}

	public int nextInt() {
		int c = read();
		while (isSpaceChar(c)) {
			c = read();
		}
		int sgn = 1;
		if (c == '-') {
			sgn = -1;
			c = read();
		}
		int res = 0;
		do {
			res *= 10;
			res += c - '0';
			c = read();
		} while (!isSpaceChar(c));
		return res * sgn;
	}

	public long nextLong() {
		int c = read();
		while (isSpaceChar(c)) {
			c = read();
		}
		int sgn = 1;
		if (c == '-') {
			sgn = -1;
			c = read();
		}
		long res = 0;
		do {
			res *= 10;
			res += c - '0';
			c = read();
		} while (!isSpaceChar(c));
		return res * sgn;
	}

	public int[] nextIntArray(int n) {
		int a[] = new int[n];
		for (int i = 0; i < n; i++) {
			a[i] = nextInt();
		}
		return a;
	}

	public String nextLine() {
		int c = read();
		while (isSpaceChar(c))
			c = read();
		StringBuilder res = new StringBuilder();
		do {
			res.appendCodePoint(c);
			c = read();
		} while (!isEndOfLine(c));
		return res.toString();
	}

	public boolean isSpaceChar(int c) {
		return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
	}

	private boolean isEndOfLine(int c) {
		return c == '\n' || c == '\r' || c == -1;
	}
}

 

 

  • 더 빠른 다른 사람 코드

 

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {
	private static boolean[] primeTable = new boolean[10000];
	private static int[] bfsTable = new int[10000];
	private static final int MAX = 10000;
	private static final String IMPOSSIBLE = "Impossible";
	
	private static int T;
	private static StringBuffer answer;
	
	public static void main(String[] args) throws Exception {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st;
		answer = new StringBuffer();
		findPrime();
		
		T = Integer.parseInt(in.readLine());
		
		for (int tc = 1 ; tc <= T ; tc++) {
			st = new StringTokenizer(in.readLine());
			int startPrime = Integer.parseInt(st.nextToken());
			int endPrime = Integer.parseInt(st.nextToken());
			initBfsTable();
			int a = findAnswer(startPrime, endPrime);
			if (a < 0) {
				answer.append(IMPOSSIBLE);
			}
			else {
				answer.append(a);
			}
			answer.append("\n");
		}
		System.out.println(answer.toString());
	}
	private static void findPrime() {
		primeTable[0] = false;
		primeTable[1] = false;
		for (int i = 2 ; i <= 9999 ; i++) {
			primeTable[i] = true;
		}
		
		for (int i = 2 ; i <= 9999 ; i++) {
			if (primeTable[i] == true) {
				for (int j = i * 2 ; j <= 9999 ; j += i) {
					primeTable[j] = false;
				}
			}
		}
	}
	
	private static void initBfsTable() {
		for (int i = 1000 ; i <= 9999 ; i++) {
			bfsTable[i] = (primeTable[i]) ? MAX : -1;
		}
	}
	
	private static int findAnswer(int startPrime, int endPrime) {
		int step = 0;
		boolean findflag = false;
		int currP = startPrime;
		
		int[] stack1 = new int[9000];
		int sp1 = 0;
		int[] stack2 = new int[9000];
		int sp2 = 0;
		
		int[] currStack = stack1;
		int currsp = sp1;
		int[] nextStack = stack2;
		int nextsp = sp2;
		
		currStack[currsp++] = startPrime;
		bfsTable[startPrime] = 0;
		
		if (startPrime == endPrime) {
			return 0;
		}
		
		do {
			step++;
			int seed;
			int nextP;
			findflag = false;
			for (int s = 0 ; s < currsp ; s++) {
				currP = currStack[s];
				// 1000 pos
				seed = currP % 1000;
				for (int i = 1 ; i <= 9 ; i++) {
					nextP = i * 1000 + seed;
					if (bfsTable[nextP] > step) {
						bfsTable[nextP] = step;
						findflag = true;
						nextStack[nextsp++] = nextP;
					}
				}

				// 100 pos
				seed = (currP / 1000) * 1000 + currP % 100;
				for (int i = 0 ; i <= 9 ; i++) {
					nextP = i * 100 + seed;
					if (bfsTable[nextP] > step) {
						bfsTable[nextP] = step;
						findflag = true;
						nextStack[nextsp++] = nextP;
					}
				}

				// 10 pos
				seed = (currP / 100) * 100 + currP % 10;
				for (int i = 0 ; i <= 9 ; i++) {
					nextP = i * 10 + seed;
					if (bfsTable[nextP] > step) {
						bfsTable[nextP] = step;
						findflag = true;
						nextStack[nextsp++] = nextP;
					}
				}

				// 1 pos
				seed = (currP / 10) * 10;
				for (int i = 0 ; i <= 9 ; i++) {
					nextP = i + seed;
					if (bfsTable[nextP] > step) {
						bfsTable[nextP] = step;
						findflag = true;
						nextStack[nextsp++] = nextP;
					}
				}
			}
			
			// exchange stack;
			int[] tempStack = currStack;
			currStack = nextStack;
			nextStack = tempStack;
			currsp = nextsp;
			nextsp = 0;
			
		} while(findflag && bfsTable[endPrime] == MAX);
		
		if (!findflag) {
			return -1;
		}
		
		return step;
	}
}

 

2666번: 벽장문의 이동

첫 번째 줄에 벽장의 개수를 나타내는 3보다 크고 20보다 작거나 같은 하나의 정수, 두 번째 줄에 초기에 열려있는 두 개의 벽장을 나타내는 두 개의 정수, 그리고 세 번째 줄에는 사용할 벽장들

www.acmicpc.net

 

 

 


 

 

 

  • 생각

 

N개의 벽장에서 N-2개의 벽장만 문이 닫혀있고 2개는 문이 열려있다.

 

벽장문의 이동 횟수와, 이동 순서가 주어지는데 해당 이동순서대로 벽장을 이동했을 때 최소로 이동할 수 있는 값을 찾으면 된다.

 

벽장문이 이동하는 경우의 수는 왼쪽, 오른쪽 2가지이다.

해당 문제는 읽어보면 알겠지만 모든 경우의 수를 돌아봐야하는 브루트포스 문제이다.

 

나는 dfs 방식을 선택했다.

 

dfs 방식

- basecase는 모든 index를 돌아서 index가 이동 횟수를 넘었을 때

- 추가적으로 현재 count가 지금까지 모든 경우의 수를 돌았던 count보다 낮을 시 작업을 더이상 하지않는다.

- basecase로 걸러지지 않았을 땐, 왼쪽 오른쪽 모든 경우의수를 돌았다. (왼쪽, 오른쪽을 쉽게 구하기 위해 파라미터로 문이 없는 벽장의 index를 포함시킨다.)

 

쉽게 구하기 위해

- Math.abs을 활용 했다. (Math.abs는 절대값을 씌워주는 함수이다.)

 

 

 

  • 코드

 

import java.io.IOException;
import java.io.InputStream;
import java.util.InputMismatchException;

public class Main {
	static int answer, move, cupboard;
	static int[] moves;

	public static void main(String[] args) throws Exception {
		SetData();
		System.out.println(answer);
	}

	// 데이터
	private static void SetData() throws Exception {
		InputReader in = new InputReader(System.in);

		answer = Integer.MAX_VALUE;
		cupboard = in.nextInt();
		int first = in.nextInt();
		int second = in.nextInt();
		
		move = in.nextInt();
		moves = new int[move];
		
		for(int i = 0; i < move; i++) {
			moves[i] = in.nextInt();
		}
		
		dfs(first, second, 0, 0);
	}
	 
	private static void dfs(int first, int second, int index, int count) {
	    // 분기한정
	    if (count > answer) return;
	 
	    // basecase
	    if (index >= move) {
	        answer = Math.min(answer, count);
	        return;
	    }
	    
	    // left
	    dfs(moves[index], second, index + 1, count + Math.abs(first - moves[index]));
	    // right
	    dfs(first, moves[index], index + 1, count + Math.abs(second - moves[index]));
	}
}

class InputReader {
	private final InputStream stream;
	private final byte[] buf = new byte[8192];
	private int curChar, snumChars;

	public InputReader(InputStream st) {
		this.stream = st;
	}

	public int read() {
		if (snumChars == -1)
			throw new InputMismatchException();
		if (curChar >= snumChars) {
			curChar = 0;
			try {
				snumChars = stream.read(buf);
			} catch (IOException e) {
				throw new InputMismatchException();
			}
			if (snumChars <= 0)
				return -1;
		}
		return buf[curChar++];
	}

	public int nextInt() {
		int c = read();
		while (isSpaceChar(c)) {
			c = read();
		}
		int sgn = 1;
		if (c == '-') {
			sgn = -1;
			c = read();
		}
		int res = 0;
		do {
			res *= 10;
			res += c - '0';
			c = read();
		} while (!isSpaceChar(c));
		return res * sgn;
	}

	public long nextLong() {
		int c = read();
		while (isSpaceChar(c)) {
			c = read();
		}
		int sgn = 1;
		if (c == '-') {
			sgn = -1;
			c = read();
		}
		long res = 0;
		do {
			res *= 10;
			res += c - '0';
			c = read();
		} while (!isSpaceChar(c));
		return res * sgn;
	}

	public int[] nextIntArray(int n) {
		int a[] = new int[n];
		for (int i = 0; i < n; i++) {
			a[i] = nextInt();
		}
		return a;
	}

	public String nextLine() {
		int c = read();
		while (isSpaceChar(c))
			c = read();
		StringBuilder res = new StringBuilder();
		do {
			res.appendCodePoint(c);
			c = read();
		} while (!isEndOfLine(c));
		return res.toString();
	}

	public boolean isSpaceChar(int c) {
		return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
	}

	private boolean isEndOfLine(int c) {
		return c == '\n' || c == '\r' || c == -1;
	}
}

 

2312번: 수 복원하기

첫째 줄에 테스트 케이스의 수가 주어진다. 각 테스트 케이스마다 양의 정수 N (2 ≤ N ≤ 100,000)이 주어진다.

www.acmicpc.net

 

 

 


 

 

 

 

  • 생각

 

미리 소수인 수를 정수 배열에 넣은 다음 count 배열을 통해 해당 소수를 몇번 사용하는지를 통해 소인수 분해를 진행한다.

 

 

 

  • 코드

 

 

import java.io.IOException;
import java.io.InputStream;
import java.util.InputMismatchException;

public class Main {
	static StringBuilder sb;

	public static void main(String[] args) throws Exception {
		SetData();
		System.out.println(sb);
	}

	// 데이터
	private static void SetData() throws Exception {
		InputReader in = new InputReader(System.in);

		sb = new StringBuilder();
		int N = in.nextInt();

		int[] primes = {
				2 ,   3,    5 ,   7  ,  11 ,   13 ,   17 ,   19 ,   23  ,  29,
				31 ,   37  ,  41   , 43 ,   47 ,   53 ,  59   ,61   , 67  ,  71,
				73  ,  79  ,  83  ,  89  ,  97  ,  101 ,   103   , 107 ,   109  ,  113,
				127  ,  131  ,  137   , 139   , 149 ,   151 ,   157 ,   163,    167 ,   173,
				179  ,  181   , 191  ,  193  ,  197  ,  199  ,  211 ,   223 ,   227   , 229,
				233   , 239 ,   241   , 251 ,   257 ,   263 ,   269  ,  271  ,  277   , 281,
				283    ,293  ,  307  ,  311  ,  313  ,  317   
		};
		int[] count = new int[66];
		
		for (int i = 0; i < N; i++) {
			int number = in.nextInt();
			for (int j = 0; j < 66; j++) {
				while (number % primes[j] == 0) {
					number /= primes[j];
					count[j]++;
				}
			}
			for (int c = 0; c < 66; c++) {
				if (count[c] != 0) {
					sb.append(Integer.toString(primes[c]) + " " + Integer.toString(count[c]) + "\n");
					count[c] = 0;
				}
			}
			if (number != 1) sb.append(Integer.toString(number) + " 1\n");
		}
	}
}

class InputReader {
	private final InputStream stream;
	private final byte[] buf = new byte[8192];
	private int curChar, snumChars;

	public InputReader(InputStream st) {
		this.stream = st;
	}

	public int read() {
		if (snumChars == -1)
			throw new InputMismatchException();
		if (curChar >= snumChars) {
			curChar = 0;
			try {
				snumChars = stream.read(buf);
			} catch (IOException e) {
				throw new InputMismatchException();
			}
			if (snumChars <= 0)
				return -1;
		}
		return buf[curChar++];
	}

	public int nextInt() {
		int c = read();
		while (isSpaceChar(c)) {
			c = read();
		}
		int sgn = 1;
		if (c == '-') {
			sgn = -1;
			c = read();
		}
		int res = 0;
		do {
			res *= 10;
			res += c - '0';
			c = read();
		} while (!isSpaceChar(c));
		return res * sgn;
	}

	public long nextLong() {
		int c = read();
		while (isSpaceChar(c)) {
			c = read();
		}
		int sgn = 1;
		if (c == '-') {
			sgn = -1;
			c = read();
		}
		long res = 0;
		do {
			res *= 10;
			res += c - '0';
			c = read();
		} while (!isSpaceChar(c));
		return res * sgn;
	}

	public int[] nextIntArray(int n) {
		int a[] = new int[n];
		for (int i = 0; i < n; i++) {
			a[i] = nextInt();
		}
		return a;
	}

	public String nextLine() {
		int c = read();
		while (isSpaceChar(c))
			c = read();
		StringBuilder res = new StringBuilder();
		do {
			res.appendCodePoint(c);
			c = read();
		} while (!isEndOfLine(c));
		return res.toString();
	}

	public boolean isSpaceChar(int c) {
		return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
	}

	private boolean isEndOfLine(int c) {
		return c == '\n' || c == '\r' || c == -1;
	}
}

 

9421번: 소수상근수

양의 정수 n의 각 자리수의 제곱의 합을 계산한다. 그렇게 해서 나온 합도 각 자리수의 제곱의 합을 계산한다. 이렇게 반복해서 1이 나온다면, n을 상근수라고 한다. 700은 상근수이다. 72 + 02 + 02 =

www.acmicpc.net

 

 

 


 

 

  • 생각

 

 

처음 에라토스테네스의 체 방법으로 소수를 구한 뒤 소수중에서 재귀를 통해 상근수를 찾는다. 

 

 

  • 코드

 

 

import java.io.IOException;
import java.io.InputStream;
import java.util.InputMismatchException;

public class Main {
	static boolean[] prime, flag, number;
	static StringBuilder sb;
	static int N;

	public static void main(String[] args) throws Exception {
		SetData();
		System.out.println(sb);
	}

	// 데이터
	private static void SetData() throws Exception {
		InputReader in = new InputReader(System.in);

		prime = new boolean[1000001];
		flag = new boolean[1000001];
		number = new boolean[1000001];
		sb = new StringBuilder();
		N = in.nextInt();

		getPrimeNumber();
		getSangGeunSoo(N);

		for (int i = 1; i <= N; i++) {
			if (getSangGeunSoo(i) && !prime[i]) {
				sb.append(i).append("\n");
			}
		}
	}

	public static boolean getSangGeunSoo(int index) {
		// basecase
		if (index == 1)	return true;
		
		if (flag[index])  return number[index];
		
		flag[index] = true;

		int sum = 0, div = 1000001, cur = index;

		while (cur > 0) {
			int temp = cur / div;
			sum += Math.pow(temp, 2);
			cur %= div;
			div /= 10;
		}
		return number[index] = getSangGeunSoo(sum);

	}

	private static void getPrimeNumber() {
		prime[0] = prime[1] = true;
		for (int i = 2; i <= 1000000; i++) {
			// true면 이미 소수이므로 pass
			if (prime[i]) {
				continue;
			}

			// 해당 수로 나누어 떨어지는 수는 소수이므로 true로 check
			for (int j = i + i; j <= 1000000; j += i) {
				prime[j] = true;
			}
		}
	}
}

class InputReader {
	private final InputStream stream;
	private final byte[] buf = new byte[8192];
	private int curChar, snumChars;

	public InputReader(InputStream st) {
		this.stream = st;
	}

	public int read() {
		if (snumChars == -1)
			throw new InputMismatchException();
		if (curChar >= snumChars) {
			curChar = 0;
			try {
				snumChars = stream.read(buf);
			} catch (IOException e) {
				throw new InputMismatchException();
			}
			if (snumChars <= 0)
				return -1;
		}
		return buf[curChar++];
	}

	public int nextInt() {
		int c = read();
		while (isSpaceChar(c)) {
			c = read();
		}
		int sgn = 1;
		if (c == '-') {
			sgn = -1;
			c = read();
		}
		int res = 0;
		do {
			res *= 10;
			res += c - '0';
			c = read();
		} while (!isSpaceChar(c));
		return res * sgn;
	}

	public long nextLong() {
		int c = read();
		while (isSpaceChar(c)) {
			c = read();
		}
		int sgn = 1;
		if (c == '-') {
			sgn = -1;
			c = read();
		}
		long res = 0;
		do {
			res *= 10;
			res += c - '0';
			c = read();
		} while (!isSpaceChar(c));
		return res * sgn;
	}

	public int[] nextIntArray(int n) {
		int a[] = new int[n];
		for (int i = 0; i < n; i++) {
			a[i] = nextInt();
		}
		return a;
	}

	public String nextLine() {
		int c = read();
		while (isSpaceChar(c))
			c = read();
		StringBuilder res = new StringBuilder();
		do {
			res.appendCodePoint(c);
			c = read();
		} while (!isEndOfLine(c));
		return res.toString();
	}

	public boolean isSpaceChar(int c) {
		return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
	}

	private boolean isEndOfLine(int c) {
		return c == '\n' || c == '\r' || c == -1;
	}
}

+ Recent posts