Skip to content

Commit

Permalink
Merge pull request #101 from HYU-PS-STUDY/clean2001
Browse files Browse the repository at this point in the history
[week-14] 2179, 14890
  • Loading branch information
clean2001 authored Apr 29, 2024
2 parents 6d6c83e + 929e67c commit 7e862b3
Show file tree
Hide file tree
Showing 3 changed files with 219 additions and 0 deletions.
Empty file removed week14/clean2001/.gitkeep
Empty file.
155 changes: 155 additions & 0 deletions week14/clean2001/B14890.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
import java.io.*;
import java.util.*;
// 경사로
class Main {
static int N, L;
static int[][] map;
static boolean[][] isHeel;
static class Node {
int y, x;
Node(int y, int x) {
this.y = y;
this.x = x;
}
}
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
L = Integer.parseInt(st.nextToken());

map = new int[N][N];
isHeel = new boolean[N][N];

for(int i=0; i<N; ++i) {
st = new StringTokenizer(br.readLine());
for(int j=0; j<N; ++j) {
map[i][j] = Integer.parseInt(st.nextToken());
}
}

int ans = 0;

// 가로
for(int i=0; i<N; ++i) {
boolean[] rowHeel = new boolean[N];
boolean flag = true;
for(int j=0; j<N-1; ++j) {
int diff = Math.abs(map[i][j] - map[i][j+1]);
if(diff > 1) {
flag = false; break; // 이 길은 안됨
}

if(diff == 1 && map[i][j] < map[i][j+1]) { // 왼쪽 검사
int h = map[i][j];
for(int d = 0; d<L; ++d) {
int nx = j - d;
if(nx < 0 || rowHeel[nx] || isHeel[i][nx] || h != map[i][nx]) {
flag = false;
break; // 이 길은 안됨
}
}

if(flag) {
for(int d = 0; d<L; ++d) {
rowHeel[j-d] = true;
}
} else {
break;
}

} else if(diff == 1 && map[i][j] > map[i][j+1]) { // 오른쪽 검사

int h = map[i][j+1];
for(int d=1; d<=L; ++d) {
int nx = j + d;
if(nx >= N || rowHeel[nx] || isHeel[i][nx] || h != map[i][nx]) {
flag = false;
break; // 이 길은 안됨
}
}

if(flag) {
for(int d=1; d<=L; ++d) {
rowHeel[j+d] = true;
}
} else break;
}
}

//
if(flag) {
// System.out.println("가로: " + i);
ans++;
for(int j=0; j<N; ++j) {
if(rowHeel[j]) isHeel[i][j] = rowHeel[j];
}
}
}


// 세로
isHeel = new boolean[N][N];
for(int i=0; i<N; ++i) {
boolean[] rowHeel = new boolean[N];
boolean flag = true;
for(int j=0; j<N-1; ++j) {
int diff = Math.abs(map[j][i] - map[j+1][i]);
if(diff > 1) {
flag = false; break; // 이 길은 안됨
}

if(diff == 1 && map[j][i] < map[j+1][i]) { // 위쪽 검사
int h = map[j][i];
for(int d = 0; d<L; ++d) {
int ny = j - d;
if(ny < 0 || rowHeel[ny] || isHeel[ny][i] || h != map[ny][i]) {
flag = false;
break; // 이 길은 안됨
}
}

if(flag) {
for(int d = 0; d<L; ++d) {
rowHeel[j-d] = true;
}
} else {
break;
}
} else if(diff == 1 && map[j][i] > map[j+1][i]) { // 아래쪽 검사



int h = map[j+1][i];
for(int d=1; d<=L; ++d) {
int ny = j + d;

if(ny >= N || rowHeel[ny] || isHeel[ny][i] || h != map[ny][i]) {

flag = false;
break; // 이 길은 안됨
}
}

if(flag) {
for(int d=1; d<=L; ++d) {
rowHeel[j+d] = true;
}
} else break;
}
}

//
if(flag) {
// System.out.println("세로: " + i);
ans++;
for(int j=0; j<N; ++j) {
if(rowHeel[j]) isHeel[j][i] = rowHeel[j];
}
}
}

System.out.println(ans);
}
}
64 changes: 64 additions & 0 deletions week14/clean2001/B2179.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import java.io.*;
import java.util.*;

class Main {
static int N;
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

N = Integer.parseInt(br.readLine());
String[] arr = new String[N];
String[] original = new String[N];
Map<String,Integer> map = new HashMap<>();
for(int i=0; i<N; ++i) {
arr[i] = br.readLine();
original[i] = arr[i];
map.put(arr[i], i);
}

Arrays.sort(arr);
String prefix = "";
int idx1=Integer.MAX_VALUE, idx2=Integer.MAX_VALUE;
for(int i=0; i<N-1; ++i) {
StringBuilder sb = new StringBuilder();
int len = Math.min(arr[i].length(), arr[i+1].length());
for(int j=0; j<len; ++j) {
if(arr[i].charAt(j) == arr[i+1].charAt(j)) sb.append(arr[i].charAt(j));
else break;
}

String curPrefix = sb.toString();
if(curPrefix.length() > prefix.length()) {
prefix = curPrefix;
idx1 = map.get(arr[i]);
idx2 = map.get(arr[i+1]);
} else if(curPrefix.length() == prefix.length() && !prefix.equals(curPrefix)) {
// 길이가 같으면 원래 인덱스가 더 작은 것을 선택
int tempIdx1 = map.get(arr[i]);
int tempIdx2 = map.get(arr[i+1]);

int[] tempArr = {idx1, idx2, tempIdx1, tempIdx2};
Arrays.sort(tempArr);


if(tempArr[0] == tempIdx1 || tempArr[0] == tempIdx2) {
idx1 = tempIdx1;
idx2 = tempIdx2;
prefix = curPrefix;
}
}
}

int cnt = 0;
// System.out.println(prefix);
for(int i=0; i<N; ++i) {
if(original[i].length() < prefix.length()) continue;
if(prefix.equals(original[i].substring(0, prefix.length()))) {
cnt++;
System.out.println(original[i]);
}

if(cnt == 2) break;
}
}
}

0 comments on commit 7e862b3

Please sign in to comment.