-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLine5.java
More file actions
75 lines (73 loc) · 1.85 KB
/
Line5.java
File metadata and controls
75 lines (73 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package codingtest;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class Line5 {
static int[] dx = {0, 1};
static int[] dy = {1, 0};
static int[][] arr;
static boolean[][] visited;
static int n, m, x, y;
static int count;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
m = sc.nextInt(); // 모눈종이 공간
x = sc.nextInt();
y = sc.nextInt(); // 도망간 좌표
arr = new int[m+1][n+1];
visited = new boolean[m+1][n+1];
int time=0;
if(x > n || x < 0 || y > m || y < 0) {
System.out.println("fail");
} else {
loop:for(int i=0; i<n; i++) {
for(int j=0; j<m; j++) {
if(!visited[i][j] && i <= y && j <= x) {
time++;
bfs(i, j);
} else
break loop;
}
}
}
System.out.println(time);
System.out.println(count);
sc.close();
}
static void bfs(int i, int j) {
if(visited[i][j] == true || i > y || j > x) return;
visited[i][j] = true;
Queue<Paper> qu = new LinkedList<>();
qu.offer(new Paper(i, j));
while(!qu.isEmpty()) {
Paper tmp = qu.poll();
int x = tmp.x;
int y = tmp.y;
for(int k=0; k<2; k++) {
int nx = x + dx[k];
int ny = y + dy[k];
// System.out.println(nx + " " + ny);
// System.out.println(isRange(nx, ny));
if(isRange(nx, ny) && !visited[nx][ny] && (nx <= y | ny <= x)) {
visited[nx][ny] = true;
qu.add(new Paper(nx, ny));
System.out.println("nx : " + nx +", ny:"+ny+",x:"+x+",y:"+y);
if(nx == y && ny == x) count++;
}
}
}
}
static boolean isRange(int x, int y) {
if(x < 0 || x >= m || y<0 || y >= n) return false;
return true;
}
static class Paper {
int x;
int y;
Paper(int x, int y) {
this.x = x;
this.y = y;
}
}
}