-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[소병희] - 양치기 꿍, 포탑 부수기, 토끼와 경주 #242
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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,69 @@ | ||
package byeonghee.week58; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.InputStreamReader; | ||
import java.util.ArrayDeque; | ||
import java.util.StringTokenizer; | ||
|
||
// 양치기 꿍 | ||
public class BYEONGHEE_BJ_3187 { | ||
|
||
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
static StringTokenizer stk; | ||
|
||
static void newline() throws Exception { stk = new StringTokenizer(br.readLine()); } | ||
static int input() { return Integer.parseInt(stk.nextToken()); } | ||
|
||
static int R, C; | ||
static char[][] farm; | ||
static boolean[][] visited; | ||
static ArrayDeque<int[]> q = new ArrayDeque<>(R * C); | ||
static int[] top; | ||
static int r, c, nr, nc; | ||
static int ansS, ansW, curS, curW; | ||
|
||
public static final char SHEEP = 'k'; | ||
public static final char WOLF = 'v'; | ||
public static final char FENSE = '#'; | ||
public static final char EMPTY = '.'; | ||
public static final int[] dr = { 1, 0, -1, 0 }; | ||
public static final int[] dc = { 0, 1, 0, -1 }; | ||
|
||
public static void main(String[] args) throws Exception { | ||
newline(); | ||
R = input(); | ||
C = input(); | ||
farm = new char[R][C]; | ||
visited = new boolean[R][C]; | ||
|
||
for(int i = 0; i < R; i++) { | ||
farm[i] = br.readLine().toCharArray(); | ||
} | ||
|
||
for(int i = 0; i < R; i++) for(int j = 0; j < C; j++) { | ||
if (farm[i][j] == FENSE) continue; | ||
if (visited[i][j]) continue; | ||
|
||
visited[i][j] = true; | ||
curS = farm[i][j] == SHEEP ? 1 : 0; | ||
curW = farm[i][j] == WOLF ? 1 : 0; | ||
q.add(new int[] {i, j}); | ||
while(!q.isEmpty()) { | ||
top = q.poll(); r = top[0]; c = top[1]; | ||
for(int d = 0; d < 4; d++) { | ||
nr = r + dr[d]; nc = c + dc[d]; | ||
if (nr < 0 || nr >= R || nc < 0 || nc >= C) continue; | ||
if (visited[nr][nc] || farm[nr][nc] == FENSE) continue; | ||
visited[nr][nc] = true; | ||
if (farm[nr][nc] == SHEEP) curS++; | ||
else if (farm[nr][nc] == WOLF) curW++; | ||
q.add(new int[] { nr, nc }); | ||
} | ||
} | ||
if (curS > curW) ansS += curS; | ||
else ansW += curW; | ||
} | ||
|
||
System.out.println(ansS + " " + ansW); | ||
} | ||
} |
157 changes: 157 additions & 0 deletions
157
src/main/kotlin/byeonghee/week58/BYEONGHEE_CODETREE_DESTROY_THE_TURRET.java
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,157 @@ | ||
package byeonghee.week58; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.InputStreamReader; | ||
import java.util.ArrayDeque; | ||
import java.util.Comparator; | ||
import java.util.HashMap; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.StringTokenizer; | ||
import java.util.stream.Collectors; | ||
|
||
public class BYEONGHEE_CODETREE_DESTROY_THE_TURRET { | ||
|
||
public static final int[] dr = { 0, 1, 0, -1 }; | ||
public static final int[] dc = { 1, 0, -1, 0 }; | ||
|
||
public static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
public static StringTokenizer stk; | ||
|
||
public static void readLine() throws Exception { stk = new StringTokenizer(br.readLine()); } | ||
public static int input() { return Integer.parseInt(stk.nextToken()); } | ||
|
||
public static int N, M, K, T; | ||
|
||
public static class Po { | ||
public int r, c, attack, lastTurn; | ||
|
||
public Po(int r, int c, int attack, int lastTurn) { | ||
this.r = r; | ||
this.c = c; | ||
this.attack = attack; | ||
this.lastTurn = lastTurn; | ||
} | ||
} | ||
|
||
public static class Laser { | ||
public int r, c; | ||
public HashSet<Integer> attacked; | ||
|
||
public Laser(int r, int c) { | ||
this.r = r; | ||
this.c = c; | ||
attacked = new HashSet<>(N * M); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 오 초기 크기를 정해놓고 푸셨군요! |
||
} | ||
} | ||
|
||
public static Comparator<Map.Entry<Integer, Po>> cmp = (a, b) -> { | ||
Po p1 = a.getValue(); Po p2 = b.getValue(); | ||
|
||
if (p1.attack == p2.attack) { | ||
if (p1.lastTurn == p2.lastTurn) { | ||
if (p1.r + p1.c == p2.r + p2.c) { | ||
return p2.c - p1.c; | ||
} | ||
else return p2.r + p2.c - p1.r - p1.c; | ||
} | ||
else return p2.lastTurn - p1.lastTurn; | ||
} | ||
else return p1.attack - p2.attack; | ||
}; | ||
|
||
public static HashMap<Integer, Po> pos; | ||
public static List<Map.Entry<Integer, Po>> list; | ||
public static Po offense, defense; | ||
public static ArrayDeque<Laser> q; | ||
public static Laser tmp, nxt; | ||
public static HashSet<Integer> attacked; | ||
public static boolean[][] visited; | ||
|
||
public static void main(String[] args) throws Exception { | ||
readLine(); | ||
N = input(); M = input(); K = input(); | ||
pos = new HashMap<>(N * M); | ||
q = new ArrayDeque<>(N * M); | ||
attacked = new HashSet<>(N * M); | ||
|
||
for(int i = 0; i < N; i++) { | ||
readLine(); | ||
for(int j = 0; j < M; j++) { | ||
int v = input(); | ||
if (v > 0) pos.put(i * M + j, new Po(i, j, v, 0)); | ||
} | ||
} | ||
|
||
for(T = 1; T <= K; T++) { | ||
list = pos.entrySet().stream().sorted(cmp).collect(Collectors.toList()); | ||
offense = list.get(0).getValue(); | ||
if (list.size() < 2) break; | ||
defense = list.get(list.size()-1).getValue(); | ||
|
||
offense.attack += N + M; | ||
offense.lastTurn = T; | ||
if (!tryLaser()) { | ||
tryBomb(defense.r, defense.c); | ||
} | ||
|
||
getReady(); | ||
} | ||
|
||
list = pos.entrySet().stream().sorted(cmp).collect(Collectors.toList()); | ||
System.out.println(list.get(list.size()-1).getValue().attack); | ||
} | ||
|
||
public static boolean tryLaser() { | ||
visited = new boolean[N][M]; | ||
q.clear(); | ||
q.add(new Laser(offense.r, offense.c)); | ||
visited[offense.r][offense.c] = true; | ||
|
||
while(!q.isEmpty()) { | ||
tmp = q.removeFirst(); | ||
for(int d = 0; d < 4; d++) { | ||
int nr = (tmp.r + dr[d] + N) % N; | ||
int nc = (tmp.c + dc[d] + M) % M; | ||
if (!pos.containsKey(nr * M + nc)) continue; | ||
if (visited[nr][nc]) continue; | ||
visited[nr][nc] = true; | ||
nxt = new Laser(nr, nc); | ||
nxt.attacked.addAll(tmp.attacked); | ||
nxt.attacked.add(nr * M + nc); | ||
if (nr == defense.r && nc == defense.c) { | ||
attacked.addAll(nxt.attacked); | ||
return true; | ||
} | ||
q.add(nxt); | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
public static void tryBomb(int r, int c) { | ||
for(int i = r-1; i <= r+1; i++) for(int j = c-1; j <= c+1; j++) { | ||
int rr = (i + N) % N; | ||
int cc = (j + M) % M; | ||
if (rr == offense.r && cc == offense.c) continue; | ||
if (!pos.containsKey(rr * M + cc)) continue; | ||
attacked.add(rr * M + cc); | ||
} | ||
} | ||
|
||
public static void getReady() { | ||
int k; Po v; | ||
for(Map.Entry<Integer, Po> entry : list) { | ||
k = entry.getKey(); v = entry.getValue(); | ||
if (attacked.contains(k)) { | ||
v.attack -= ((defense.r * M + defense.c == k) ? offense.attack : (offense.attack / 2)); | ||
if (v.attack <= 0) pos.remove(k); | ||
} | ||
else if (offense.r != v.r || offense.c != v.c) { | ||
v.attack++; | ||
} | ||
} | ||
attacked.clear(); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
오 엘비스 예뻐요