01-10 04:27
Recent Posts
Recent Comments
Tags
- JOBνκ³
- Java
- ict곡λͺ¨μ
- API MarketPlace κΈλ‘λ² μν¬ν°μ¦
- APIλ§μΌνλ μ΄μ€
- νλ‘λ³΄λ Έ
- μ‘νκ³
- μλμ΄λ Έ
- appetizer
- μλ°
- μ΄λΈμ
- νμ΄μ
- linux
- ICT
- λ°μ΄ν°λ² μ΄μ€
- μ€ν½μ€λΉ
- νμ΄μ¬
- mysql
- TSQL
- SQL
- μ¨μΌλν
- Naver Cloud
- DATABASE
- DB
- RaspberryPi
- Spring
- μ€ν½λ ν
- ICTλ©ν λ§
- νμ΄μ곡λͺ¨μ
- python
- Today
- Total
miinsun
[BAEKJOON] λ°±μ€ κ·Έλν 2178 :: λ―Έλ‘ νμ λ³Έλ¬Έ
π¬ λ¬Έμ μ€λͺ
N×Mν¬κΈ°μ λ°°μ΄λ‘ ννλλ λ―Έλ‘κ° μλ€.
λ―Έλ‘μμ 1μ μ΄λν μ μλ μΉΈμ λνλ΄κ³ , 0μ μ΄λν μ μλ μΉΈμ λνλΈλ€.
μ΄λ¬ν λ―Έλ‘κ° μ£Όμ΄μ‘μ λ, (1, 1)μμ μΆλ°νμ¬ (N, M)μ μμΉλ‘ μ΄λν λ
μ§λμΌ νλ μ΅μμ μΉΈ μλ₯Ό ꡬνλ νλ‘κ·Έλ¨μ μμ±νμμ€.
ν μΉΈμμ λ€λ₯Έ μΉΈμΌλ‘ μ΄λν λ, μλ‘ μΈμ ν μΉΈμΌλ‘λ§ μ΄λν μ μλ€.
μμ μμμλ 15μΉΈμ μ§λμΌ (N, M)μ μμΉλ‘ μ΄λν μ μλ€. μΉΈμ μ λμλ μμ μμΉμ λμ°© μμΉλ ν¬ν¨νλ€.
π¨ μ μΆλ ₯ μ
μ λ ₯
- 첫째 μ€μ λ μ μ N, M(2 ≤ N, M ≤ 100)μ΄ μ£Όμ΄μ§λ€. λ€μ Nκ°μ μ€μλ Mκ°μ μ μλ‘ λ―Έλ‘κ° μ£Όμ΄μ§λ€.
- κ°κ°μ μλ€μ λΆμ΄μ μ λ ₯μΌλ‘ μ£Όμ΄μ§λ€.
μΆλ ₯
- 첫째 μ€μ μ§λμΌ νλ μ΅μμ μΉΈ μλ₯Ό μΆλ ₯νλ€.
- νμ λμ°©μμΉλ‘ μ΄λν μ μλ κ²½μ°λ§ μ λ ₯μΌλ‘ μ£Όμ΄μ§λ€.
μμ μ λ ₯ 1)
4 6
101111
101010
101011
111011
μμ μΆλ ₯ 1)
15
μμ μ λ ₯ 2)
4 6
110110
110110
111111
111101
μμ μΆλ ₯ 2)
9
β
μμ μ λ ₯ 3)
2 25
1011101110111011101110111
1110111011101110111011101
μμ μΆλ ₯ 3)
38
β
μμ μ λ ₯ 4)
7 7
1011111
1110001
1000001
1000001
1000001
1000001
1111111
μμ μΆλ ₯ 4)
13
β
π» Main.java
- DFSμ BFS λͺ¨λ μ¬μ©κ°λ₯νμ§λ§, DFSλ₯Ό μ΄μ©ν΄ νλ©΄ μκ°μ΄κ³Όκ° λ¬λ€.
- λλ DFSλ‘ νλ€κ° μκ°μ΄κ³Όκ° λ μ BFSλ‘ λ€μ νλ² νμ΄μ€¬λ€.
BFS_ver
/* λ°±μ€ κ·Έλν - 2178 :: λ―Έλ‘ νμ - BFS*/
import java.util.*;
import java.io.*;
class Point {
public int x, y;
Point (int x, int y){
this.x = x;
this.y = y;
}
}
public class Main {
static int[][] board, dis;
static int n, m;
// μΈμ ν μΉΈ(μ’μ° μ μλ) μ’ν μ 보
static int[] dx = {-1, 0, 1, 0};
static int[] dy = {0, 1, 0, -1};
static public void BFS(int x, int y) {
Queue<Point> Q = new LinkedList<>();
Q.offer(new Point(x, y));
board[x][y] = 1;
//νκ° λ€ λ¨μ΄μ§λκΉμ§ λ°λ³΅
while(!Q.isEmpty()) {
Point tmp = Q.poll();
for(int i = 0; i < 4; i++) {
int nx = tmp.x + dx[i];
int ny = tmp.y + dy[i];
// κ²½κ³μ κ²μ¬
if(nx >= 0 && nx <= n - 1 && ny >= 0 && ny <= m - 1 && board[nx][ny] == 1) {
board[nx][ny] = 0;
Q.offer(new Point(nx, ny));
// μλ 거리μμ + 1
dis[nx][ny] = dis[tmp.x][tmp.y] + 1;
}
}
}
}
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
n = Integer.parseInt(st.nextToken());
m = Integer.parseInt(st.nextToken());
board = new int[n][m];
dis = new int[n][m];
for(int i = 0; i < n; i++) {
char[] cList = br.readLine().toCharArray();
for(int j = 0; j < m; j++) {
board[i][j] = cList[j] - '0';
}
}
BFS(0, 0);
System.out.println(dis[n-1][m-1] + 1);
br.close();
return ;
}
}
DFS_ver
/* λ°±μ€ κ·Έλν - 2178 :: λ―Έλ‘ νμ - DFS */
import java.util.*;
import java.io.*;
public class Main {
static int[][] board;
static int cnt = 1;
static int min = Integer.MAX_VALUE;
// μΈμ ν μΉΈ(μ’μ° μ μλ) μ’ν μ 보
static int[] dx = {-1, 0, 1, 0};
static int[] dy = {0, 1, 0, -1};
static void DFS(int x, int y, int n, int m) {
if(x == n - 1 && y == m - 1)
min = Math.min(min, cnt);
else {
for(int i = 0; i < 4; i++) {
// λ€μμ μ¬ μ’ν
int nx = x + dx[i];
int ny = y + dy[i];
// μ§ν κ²½κ³ κ° κ²μ¬
if(nx >= 0 && ny >= 0 && nx < n && ny < m && board[nx][ny] == 1) {
cnt++;
board[nx][ny] = 0;
DFS(nx, ny, n, m);
cnt--;
board[nx][ny] = 1;
}
}
}
}
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int m = Integer.parseInt(st.nextToken());
board = new int[n][m];
for(int i = 0; i < n; i++) {
char[] cList = br.readLine().toCharArray();
for(int j = 0; j < m; j++) {
board[i][j] = cList[j] - '0';
}
}
DFS(0, 0, n, m);
System.out.println(min);
br.close();
return ;
}
}
'Algorithm > Baekjoon' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[BAEKJOON] λ°±μ€ κ·Έλν1991 :: νΈλ¦¬ μν (0) | 2022.04.04 |
---|---|
[BAEKJOON] λ°±μ€ κ·Έλν 2146 :: λ€λ¦¬ λ§λ€κΈ° (0) | 2022.04.04 |
[BAEKJOON] λ°±μ€ κ·Έλν 7576 :: ν λ§ν (0) | 2022.04.02 |
[BAEKJOON] λ°±μ€ κ·Έλν 4963 :: μ¬μ κ°μ (0) | 2022.04.02 |
[BAEKJOON] λ°±μ€ κ·Έλν 2667 :: λ¨μ§λ²νΈλΆμ΄κΈ° (0) | 2022.04.02 |
Comments