Skip to content
Merged
Show file tree
Hide file tree
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
68 changes: 68 additions & 0 deletions Baekjoon/hyeonjin/섬의_개수.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import java.io.*;
import java.util.*;

public class 섬의_개수 {
static int[] dx = {-1,-1,-1,0,0,1,1,1};
static int[] dy = {-1,0,1,-1,1,-1,0,1};
public static void main(String[] args) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer str;

while(true){
str = new StringTokenizer(br.readLine());
int W = Integer.valueOf(str.nextToken());
int H = Integer.valueOf(str.nextToken());
if(W == 0 && H == 0) break;

int[][] arr = new int[H][W];
boolean[][] check = new boolean[H][W];
for(int i = 0; i < H; i++){
str = new StringTokenizer(br.readLine());
for(int j = 0; j < W; j++){
arr[i][j] = Integer.valueOf(str.nextToken());
}
}

int cnt = 0;
for(int i = 0; i < H; i++){
for(int j = 0; j < W; j++){
if(check[i][j] || arr[i][j] == 0) continue;

cnt++;
Queue<Point> queue = new LinkedList<>();
queue.add(new Point(i,j));
check[i][j] = true;

while(!queue.isEmpty()){
Point p = queue.poll();
for(int k = 0; k < 8; k++){
int x = p.x + dx[k];
int y = p.y + dy[k];

if(x < 0 || x >= H || y < 0 || y >= W || check[x][y] || arr[x][y] == 0) continue;

queue.add(new Point(x,y));
check[x][y] = true;
}
}
}
}
bw.write(String.valueOf(cnt));
bw.newLine();
}

bw.flush();
bw.close();
}

public static class Point{
int x;
int y;

public Point(int x, int y){
this.x = x;
this.y = y;
}
}
}
44 changes: 44 additions & 0 deletions Baekjoon/hyeonjin/시간_여행.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import java.io.*;
import java.util.*;

public class 시간_여행 {
static Stack<Integer>[] array;
static Stack<Integer> stack;
public static void main(String[] args) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer str;

int N = Integer.valueOf(br.readLine());

array = new Stack[N + 1];
array[0] = new Stack<>();
stack = new Stack<>();

for(int i = 1; i <= N; i++){
str = new StringTokenizer(br.readLine());
switch (str.nextToken().charAt(0)){
case 'a':
stack.add(Integer.valueOf(str.nextToken()));
break;

case 's':
stack.pop();
break;

case 't':
stack.clear();
stack.addAll(array[Integer.valueOf(str.nextToken()) - 1]);
break;
}

array[i] = new Stack<>();
array[i].addAll(stack);
if(stack.isEmpty()) bw.write("-1");
else bw.write(String.valueOf(stack.peek()));
bw.newLine();
}
bw.flush();
bw.close();
}
}