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

 

1343번: 폴리오미노

첫째 줄에 사전순으로 가장 앞서는 답을 출력한다. 만약 덮을 수 없으면 -1을 출력한다.

www.acmicpc.net

코드설명

그리디문제입니다.

문제의 내용을 그대로 구현하면됩니다.

유의해야할점은 검사 중에 map.length - 1 보다는 작아야 arrayoutofindexException이 발생하지 않습니다.

 

 

추가로,

문자열 함수를 사용하여 replaceAll("XXXX","AAAA");  이런식으로 진행해도 됩니다.

코드

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Main {
public static int N;
public static char[] map;
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
map = st.nextToken().toCharArray();
for(int i=0;i<map.length;i++) {
if( i+3 <= map.length -1 ) {
if(map[i] == 'X' && map[i+1] == 'X' && map[i+2] == 'X' && map[i+3] == 'X') {
map[i] = 'A';
map[i+1] = 'A';
map[i+2] = 'A';
map[i+3] = 'A';
}
}
if( i + 1 <= map.length - 1) {
if(map[i] == 'X' && map[i+1] == 'X') {
map[i] = 'B';
map[i+1] = 'B';
}
}
}
for(char value:map) {
if(value == 'X') {
System.out.println("-1");
return;
}
}
for(char value:map) {
System.out.print(value);
}
}
}

+ Recent posts