-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #101 from HYU-PS-STUDY/clean2001
[week-14] 2179, 14890
- Loading branch information
Showing
3 changed files
with
219 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |