νμ΄ μλ μ΄μ μ΄μ©νλ€!
DFSμ μ¬μ©ν λ λ°λμ μ’
λ£μ‘°κ±΄ λ¨Όμ μκ°νκΈ°
0ν ~ λ§μ§λ§ νκΉμ§ νμνκΈ° λλ¬Έμ λ°λμ μμͺ½μΌλ‘ λΆμ΄μ κ²½λ‘λ₯Ό μ νν΄μΌ λ€μ μΆλ°μ£Όμμ λλ¬ κ°λ₯μ±μ ν΄μΉμ§ μλλ€.
β μ€λ₯Έμͺ½ μ, μ€λ₯Έμͺ½, μ€λ₯Έμͺ½ μλ μμλ‘ νμνμ! Greedy
if dfs(nx, ny):
return True
dfs(nx, ny)λ§ ν΄μ£Όλ©΄ λ κ² κ°μλ° μ νλ² λ νμΈν΄μΌνλμ§ κ³μ κ³ λ―Όνλ€.
β ν΄λΉ μΆλ°μ§μμ κ° μ μλ μ΅μ μ κ²½λ‘κ° λ±μ₯νλ€λ©΄ λ μ΄μ μ¬κ·μ μΈ νμμ νμ§ μμμΌνκΈ° λλ¬Έμ΄λ€.
dfs(nx, ny)λ§ ν΄μ€ κ²½μ°, μ΄λ κ°λ₯ν λͺ¨λ κ²½μ°μ μλ₯Ό νμνκΈ° λλ¬Έμ μ΅μ μ κ²½λ‘κ° λ°κ²¬λμ΄λ μ΄λ₯Ό νμΈνκ±°λ μ’
λ£νμ§ μκ³ κ³μν΄μ λ€λ₯Έ κ²½λ‘λ₯Ό νμνλ―λ‘, λ΅μ΄ λ¬λΌμ§λ€.
import sys
r, c = map(int, sys.stdin.readline().split())
graph = []
for _ in range(r):
graph.append(list(sys.stdin.readline().rstrip()))
dx = [-1, 0, 1]
dy = [1, 1, 1]
visited = [[-1 for _ in range(c)] for _ in range(r)]
cnt = 0
def dfs(x, y):
# dfsλ μ’
λ£μ‘°κ±΄μ λ¨Όμ μκ°νκΈ°
if y == c-1:
return True
for i in range(3):
nx, ny = x + dx[i], y + dy[i]
if 0 <= nx < r and 0 <= ny < c:
if graph[nx][ny] != 'x' and visited[nx][ny] == -1:
visited[nx][ny] = 1
# ν΄λΉ μΆλ°μ§μμ κ° μ μλ μ΅μ μ κ²½λ‘κ° λ±μ₯νλ€λ©΄ λ μ΄μ μ¬κ·μ μΈ νμμ νμ§ μλλ€.
if dfs(nx, ny):
return True
return False
for i in range(r):
if dfs(i, 0):
cnt += 1
print(cnt)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
static char[][] graph;
static int r;
static int c;
static int count;
static int[][] visited;
static int[] dx;
static int[] dy;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
r = Integer.parseInt(st.nextToken());
c = Integer.parseInt(st.nextToken());
graph = new char[r][c];
for (int i=0; i<r; i++){
graph[i] = br.readLine().toCharArray();
}
dx = new int[]{-1, 0, 1};
dy = new int[]{1, 1, 1};
visited = new int[r][c];
for (int i = 0; i<r; i++){
for (int j = 0; j<c; j++) {
visited[i][j] = -1;
}
}
for(int i=0; i<r; i++){
if (dfs(i, 0)) {
count++;
}
}
System.out.println(count);
}
public static boolean dfs(int x, int y) {
if (y == c-1) {
return true;
}
for (int i=0; i<3; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
if (0 <= nx && nx < r && 0 <= ny && ny < c) {
if (graph[nx][ny] != 'x' && visited[nx][ny] == -1) {
visited[nx][ny] = 1;
if (dfs(nx, ny)) {
return true;
}
}
}
}
return false;
}
}