Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 127 additions & 0 deletions kmj-99/202509/18428.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
```java

import java.io.*;
import java.util.*;


public class Main {
static class Node{
int x;
int y;

public Node(int x, int y) {
this.x = x;
this.y = y;
}
}

static final int[] dx = {0, 0, 1, -1};
static final int[] dy = {1, -1, 0, 0};
static ArrayList<Node> student = new ArrayList<>();
static int N;
static String[][] map;

public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
map = new String[N][N];

for(int i=0; i<N; i++){
st = new StringTokenizer(br.readLine());
for(int j=0; j<N; j++){
map[i][j] = st.nextToken();
if(map[i][j].equals("S")){
student.add(new Node(i, j));
}
}
}

dfs(0);

bw.write("NO");
bw.flush();
bw.close();
}

static public void dfs(int depth){
if(depth == 3){
bfs();
return;
}

for(int i=0; i<N; i++){
for(int j=0; j<N; j++){
if(map[i][j].equals("X")){
map[i][j] = "O";
dfs(depth+1);
map[i][j] = "X";
}
}
}

}

private static void bfs() {

Queue<Node> q = new LinkedList<>();
String[][] copyMap = new String[N][N];
boolean[][] check = new boolean[N][N];

for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
copyMap[i][j] = map[i][j];
}
}

for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (copyMap[i][j].equals("T")) {
q.add(new Node(i, j));
check[i][j] = true;
}
}
}

while (!q.isEmpty()) {
Node now = q.poll();
int x = now.x;
int y = now.y;

for (int k = 0; k < 4; k++) {
int nx = x + dx[k];
int ny = y + dy[k];

while(0 <= nx && nx < N && 0 <= ny && ny < N) {
if (!copyMap[nx][ny].equals("O")) {
check[nx][ny] = true;
nx += dx[k];
ny += dy[k];
}else{
break;
}
}
}
}
if(catchStudent(check)){
System.out.println("YES");
System.exit(0);
}
}

private static boolean catchStudent(boolean[][] check) {

for (Node node : student) {
if (check[node.x][node.y] == true) {
return false;
}
}
return true;
}

}


```