Skip to content
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 5 commits into from
Apr 21, 2024
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
66 changes: 22 additions & 44 deletions src/main/kotlin/byeonghee/week57/아이템 줍기.java
Original file line number Diff line number Diff line change
@@ -1,91 +1,69 @@
package byeonghee.week57;

class GETITEM {
class BYEONGHEE_GETITEM {

static int[] dr = { -1, 0, 1, 0 };
static int[] dc = { 0, 1, 0, -1 };

static int[][] map = new int[52][52];
static int[][] visited = new int[52][52];
static int[][] rectangles;

static int recId = 1;
static int answer = 0;
static int around = 0;
static int r = 0;
static int c = 0;
static int d = 0;

static int x = 0;
static int xbm = 0;
static int d = 0;
static int nr = 0;
static int nc = 0;
static int nd = 0;

public static int solution(int[][] rectangle, int cc, int rr, int itemC, int itemR) {
for(int[] rect : rectangle) {
rectangles = rectangle;

for(int[] rect : rectangles) {
for(int i = rect[1]; i <= rect[3]; i++) {
for(int j = rect[0]; j <= rect[2]; j++) {
map[i][j] = 1 << recId; //110
}
map[i][rect[0]] += 1 << recId;
map[i][rect[2]] += 1 << recId;
}
for(int j = rect[0]+1; j < rect[2]; j++) {
map[rect[1]][j] += 1 << recId;
map[rect[3]][j] += 1 << recId;
}
recId++;
}

r = rr; c = cc; visited[r][c]++;
x = getNextRecId(r, c, 0); xbm = 1 << x;
while(visited[r][c] < 2) {
nr = r + dr[d]; nc = c + dc[d];

if ((map[nr][nc] & map[r][c]) == 0 || !isOuter(nr, nc)) {
if ((map[nr][nc] & map[r][c]) == 0 || isInner((r + nr) / 2f, (c + nc) / 2f)) {
nd = (d + 1) % 4;
d = isOuter(r + dr[nd], c + dc[nd]) ? nd : (nd + 2) % 4;
nr = r + dr[d]; nc = c + dc[d];
if ((map[r + dr[nd]][c + dc[nd]] & map[r][c]) == 0 || isInner(r + 0.5f * dr[nd], c + 0.5f * dc[nd])) {
nd = (nd + 2) % 4;
}
d = nd; nr = r + dr[d]; nc = c + dc[d];
}

r = nr; c = nc;
visited[r][c]++;
around++;
if(r == itemR && c == itemC) answer = around;

if (map[r][c] - xbm > 0) {
x = getNextRecId(r, c, x);
xbm = 1 << x;
}
}

return answer < (around - answer) ? answer : around - answer;
}

public static boolean isOuter(int r, int c) {
if (map[r][c] == 0) return false;

int outer = 0;
int nr = r;
int nc = c;

for(int d = 0; d < 4; d++) {
if (nr < 0 || nc < 0 || nr > 51 || nc > 51) return false;
nr = r + dr[d];
nc = c + dc[d];
if((map[nr][nc] & map[r][c]) == 0) outer++;
}

return outer > 0 && outer < 4;
}

public static int getNextRecId(int r, int c, int v) {
int ret = 1;
int bm = 2;
while((map[r][c] & bm) == 0) {
bm <<= 1;
ret++;
if (ret == v) {
bm <<= 1;
ret++;
public static boolean isInner(float midR, float midC) {
for(int[] rec : rectangles) {
if (midR > rec[1] && midR < rec[3] && midC > rec[0] && midC < rec[2]) {
return true;
}
}

return ret;
return false;
}

public static void main(String[] args) {
Expand Down
69 changes: 69 additions & 0 deletions src/main/kotlin/byeonghee/week58/BYEONGHEE_BJ_3187.java
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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오 엘비스 예뻐요

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);
}
}
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);
Copy link
Member

Choose a reason for hiding this comment

The 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();
}
}
Loading