18428번: 감시 피하기
NxN 크기의 복도가 있다. 복도는 1x1 크기의 칸으로 나누어지며, 특정한 위치에는 선생님, 학생, 혹은 장애물이 위치할 수 있다. 현재 몇 명의 학생들은 수업시간에 몰래 복도로 빠져나왔는데, 복
www.acmicpc.net
- 풀이
모든 X를 O개로 번갈아가면서 모든 경우의 수를 바꿔주면서 백트래킹을 했다.
- 참고
/*
for(int i = index; i < empty.size(); i++) {
array[empty.get(i)[0]][empty.get(i)[1]] = 'O';
SaveAnswer(count + 1, index + 1);
array[empty.get(i)[0]][empty.get(i)[1]] = 'X';
}*/
int r = index / N;
int c = index % N;
if(array[r][c] == 'X') {
array[r][c] = 'O';
SaveAnswer(count+1, index+1);
array[r][c] = 'X';
}
주석 처리된 부분으로하면 틀렸다고 떠서 바꾼 부분이다.
주석 처리된 부분이 왜 틀렸는지 아직 잘 모르겠음...
empty는 ArrayList로 X의 index만 저장해놓은 데이터다.
- 코드
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.InputMismatchException;
public class Main {
static int N;
static char[][] array;
static ArrayList<int []> teachers;
static ArrayList<int []> empty;
public static void main(String[] args) throws Exception {
SetData();
System.out.println("NO");
}
// 데이터
private static void SetData() throws Exception {
InputReader in = new InputReader(System.in);
N = in.nextInt();
array = new char[N][N];
teachers = new ArrayList<>();
empty = new ArrayList<>();
for(int i = 0; i < N; i++) {
String temp = in.nextLine();
for(int j = 0; j < N; j++) {
array[i][j] = temp.charAt(j*2);
if(array[i][j] == 'T')
teachers.add(new int[] {i,j});
if(array[i][j] == 'X')
empty.add(new int[] {i,j});
}
}
SaveAnswer(0, 0);
}
private static void SaveAnswer(int count, int index) {
// basecase
if(count == 3) {
if(NotMeet()) {
System.out.println("YES");
System.exit(0);
}
return;
}
if(index >=N*N) return;
int r = index / N;
int c = index % N;
if(array[r][c] == 'X') {
array[r][c] = 'O';
SaveAnswer(count+1, index+1);
array[r][c] = 'X';
}
SaveAnswer(count, index+1);
}
private static boolean NotMeet() {
for(int t = 0; t < teachers.size(); t++) {
int i = teachers.get(t)[0];
int j = teachers.get(t)[1];
// 왼
for(int directon = j-1; directon >= 0; directon--) {
if(array[i][directon] == 'S')
return false;
if(array[i][directon] == 'O')
break;
}
// 오른
for(int directon = j+1; directon < N; directon++) {
if(array[i][directon] == 'S')
return false;
if(array[i][directon] == 'O')
break;
}
// 위
for(int directon = i-1; directon >= 0; directon--) {
if(array[directon][j] == 'S')
return false;
if(array[directon][j] == 'O')
break;
}
// 아래
for(int directon = i+1; directon < N; directon++) {
if(array[directon][j] == 'S')
return false;
if(array[directon][j] == 'O')
break;
}
}
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;
}
}
'algorithm' 카테고리의 다른 글
[BOJ/JAVA] 백준 11723번 : 집합 (0) | 2021.08.04 |
---|---|
[BOJ/JAVA] 백준 13335번 : 트럭 (0) | 2021.08.04 |
[BOJ/JAVA] 백준 14569번 : 시간표 짜기 (0) | 2021.08.02 |
[BOJ/JAVA] 백준 1303번 : 전쟁 - 전투 (0) | 2021.08.02 |
[BOJ/JAVA] 백준 2504번 : 괄호의 값 (0) | 2021.08.02 |