본문 바로가기
Problem Solving/BOJ

백준 -2567 색종이2

by 채니_ 2021. 2. 17.

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

 

2567번: 색종이 - 2

가로, 세로의 크기가 각각 100인 정사각형 모양의 흰색 도화지가 있다. 이 도화지 위에 가로, 세로의 크기가 각각 10인 정사각형 모양의 검은색 색종이를 색종이의 변과 도화지의 변이 평행하도록

www.acmicpc.net


풀이 1 

x,y 좌표값을 계산해서 배열에 배정함


** 주의 ** 

배열의 값을 계산해서 지정할것이기때문에 배열의기존크기를 늘려줬다면 배열의 계산식도 조정 필요

100->102

90-y ~ 100-y -> 92-y ~ 102-y


import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N =sc.nextInt();//색종이의 수
		int [][] arr = new int [102][102];
		int cnt=0;
		for (int i = 0; i < N; i++) {//색종이입력
			int x =sc.nextInt();//열
			int y =sc.nextInt();//100-y
			//x~ x+10 90-y~100-y
			for (int r = 92-y; r <102-y; r++) {
				for (int c = x; c < x+10; c++) {
					arr[r][c]=1;
				}
			}
		}//end for
	
		for (int i = 1; i <=100; i++) {
			for (int j = 1; j <=100; j++) {
				if(arr[i][j]==1) {
					//상
					if(arr[i-1][j]==0) cnt++;
					//하
					if(arr[i+1][j]==0) cnt++;
					//좌
					if(arr[i][j-1]==0) cnt++;
					//우
					if(arr[i][j+1]==0) cnt++;
				}
			}
		}
		System.out.println(cnt);

}
}

풀이 2

배열자체를 뒤집어서 생각함


import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N =sc.nextInt();//색종이의 수
		int [][] arr = new int [101][101];
		int cnt=0;
		for (int i = 0; i < N; i++) {//색종이입력
			int x =sc.nextInt();//열
			int y =sc.nextInt();//100-y
			//x~ x+10 90-y~100-y
			for (int r = y; r < y+10; r++) {
				for (int c = x; c < x+10; c++) {
					arr[r][c]=1;
				}
			}
		}//end for
	
		for (int i = 1; i <=100; i++) {
			for (int j = 1; j <=100; j++) {
				if(arr[i][j]==1) {
					//상
					if(arr[i-1][j]==0) cnt++;
					//하
					if(arr[i+1][j]==0) cnt++;
					//좌
					if(arr[i][j-1]==0) cnt++;
					//우
					if(arr[i][j+1]==0) cnt++;
				}
			}
		}
		System.out.println(cnt);

}
}

배열의값이 1인경우 상,하,좌,우 판단해서 0이면 모서리 

 

'Problem Solving > BOJ' 카테고리의 다른 글

백준- 1987 알파벳  (0) 2021.02.19
백준- 15686 치킨 배달  (0) 2021.02.18
백준 -14891 톱니바퀴  (0) 2021.02.16
백준 3040 백설 공주와 일곱 난쟁이  (0) 2021.02.16
백준 2961 도영이가 만든 맛있는 음식  (0) 2021.02.15

댓글