20251014 baekjoon
This commit is contained in:
parent
d7ca0cb2d4
commit
4e9f4ef644
46
code_study/Baekjoon/python/2638.py
Normal file
46
code_study/Baekjoon/python/2638.py
Normal file
@ -0,0 +1,46 @@
|
||||
from collections import deque
|
||||
|
||||
N, M = map(int, input().split())
|
||||
paper = []
|
||||
cheese = 0
|
||||
for i in range(N) :
|
||||
paper.append(list(map(int, input().split())))
|
||||
for n in paper[i] :
|
||||
if n == 1 :
|
||||
cheese += 1
|
||||
|
||||
dx = [1,-1,0,0]
|
||||
dy = [0,0,1,-1]
|
||||
q = deque()
|
||||
time = 0
|
||||
|
||||
while cheese != 0 :
|
||||
visited = [[0]*M for _ in range(N)]
|
||||
q.append((0,0))
|
||||
visited[0][0] = 1
|
||||
|
||||
while q :
|
||||
cx, cy = q.popleft()
|
||||
|
||||
for i in range(4) :
|
||||
nx, ny = cx + dx[i], cy + dy[i]
|
||||
|
||||
if 0<=nx<M and 0<=ny<N and paper[ny][nx] == 0 and visited[ny][nx] == 0 :
|
||||
q.append((nx, ny))
|
||||
visited[ny][nx] = 1
|
||||
|
||||
for j in range(4) :
|
||||
nnx, nny = nx+dx[j], ny+dy[j]
|
||||
|
||||
if 0<=nnx<M and 0<=nny<N and paper[nny][nnx] == 1 :
|
||||
visited[nny][nnx] += 1
|
||||
|
||||
for i in range(N) :
|
||||
for j in range(M) :
|
||||
if paper[i][j] == 1 and visited[i][j] >= 2 :
|
||||
paper[i][j] = 0
|
||||
cheese -= 1
|
||||
|
||||
time += 1
|
||||
|
||||
print(time)
|
||||
146
code_study/Baekjoon/swift/1238.swift
Normal file
146
code_study/Baekjoon/swift/1238.swift
Normal file
@ -0,0 +1,146 @@
|
||||
// 그냥 큐를 써도 통과가 되긴 하다만 연습삼아서 우선순위큐를 직접 구현해서 풀어봄
|
||||
struct PriorityQueue {
|
||||
private var heap: [(now: Int, time: Int)] = []
|
||||
|
||||
public var isEmpty: Bool {
|
||||
return heap.isEmpty
|
||||
}
|
||||
|
||||
public var count: Int {
|
||||
return heap.count
|
||||
}
|
||||
|
||||
public func peek() -> (now: Int, time: Int)? {
|
||||
return heap.first
|
||||
}
|
||||
|
||||
public mutating func enqueue(_ element: (now: Int, time: Int)) {
|
||||
heap.append(element)
|
||||
siftUp(from: heap.count - 1)
|
||||
}
|
||||
|
||||
public mutating func dequeue() -> (now: Int, time: Int)? {
|
||||
guard !heap.isEmpty else { return nil }
|
||||
if heap.count == 1 {
|
||||
return heap.removeLast()
|
||||
}
|
||||
|
||||
heap.swapAt(0, heap.count - 1)
|
||||
let dequeuedElement = heap.removeLast()
|
||||
siftDown(from: 0)
|
||||
|
||||
return dequeuedElement
|
||||
}
|
||||
|
||||
private func parentIndex(of i: Int) -> Int { (i - 1) / 2 }
|
||||
private func leftChildIndex(of i: Int) -> Int { 2 * i + 1 }
|
||||
private func rightChildIndex(of i: Int) -> Int { 2 * i + 2 }
|
||||
|
||||
private mutating func siftUp(from index: Int) {
|
||||
var childIndex = index
|
||||
var parentIndex = self.parentIndex(of: childIndex)
|
||||
|
||||
while childIndex > 0 && heap[childIndex].time < heap[parentIndex].time {
|
||||
heap.swapAt(childIndex, parentIndex)
|
||||
childIndex = parentIndex
|
||||
parentIndex = self.parentIndex(of: childIndex)
|
||||
}
|
||||
}
|
||||
|
||||
private mutating func siftDown(from index: Int) {
|
||||
var parentIndex = index
|
||||
|
||||
while true {
|
||||
let leftIndex = leftChildIndex(of: parentIndex)
|
||||
let rightIndex = rightChildIndex(of: parentIndex)
|
||||
var smallestIndex = parentIndex
|
||||
|
||||
if leftIndex < heap.count && heap[leftIndex].time < heap[smallestIndex].time {
|
||||
smallestIndex = leftIndex
|
||||
}
|
||||
|
||||
if rightIndex < heap.count && heap[rightIndex].time < heap[smallestIndex].time {
|
||||
smallestIndex = rightIndex
|
||||
}
|
||||
|
||||
if smallestIndex == parentIndex {
|
||||
return
|
||||
}
|
||||
|
||||
heap.swapAt(parentIndex, smallestIndex)
|
||||
parentIndex = smallestIndex
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if let input = readLine(),
|
||||
let nums = input.split(separator: " ").compactMap({Int($0)}) as? [Int],
|
||||
nums.count == 3
|
||||
{
|
||||
let N = nums[0]
|
||||
let M = nums[1]
|
||||
let X = nums[2]
|
||||
|
||||
var graph: [[(now: Int, time: Int)]] = Array(repeating: [], count: N+1)
|
||||
var reverse_graph: [[(now: Int, time: Int)]] = Array(repeating: [], count: N+1)
|
||||
|
||||
for _ in 0..<M {
|
||||
if let line = readLine(),
|
||||
let info = line.split(separator: " ").compactMap({Int($0)}) as? [Int],
|
||||
info.count == 3
|
||||
{
|
||||
let u = info[0]
|
||||
let v = info[1]
|
||||
let t = info[2]
|
||||
|
||||
graph[u].append((now: v, time: t))
|
||||
reverse_graph[v].append((now: u, time: t))
|
||||
}
|
||||
}
|
||||
|
||||
var gotime = Array(repeating: Int.max, count: N+1) // N -> X
|
||||
var comingtime = Array(repeating: Int.max, count: N+1) // X -> N
|
||||
|
||||
var pq = PriorityQueue()
|
||||
pq.enqueue((now: X, time: 0))
|
||||
comingtime[X] = 0
|
||||
|
||||
while !pq.isEmpty {
|
||||
if let current = pq.dequeue() {
|
||||
if comingtime[current.now] < current.time {
|
||||
continue
|
||||
}
|
||||
|
||||
for i in 0..<reverse_graph[current.now].count {
|
||||
if comingtime[reverse_graph[current.now][i].now] > current.time + reverse_graph[current.now][i].time {
|
||||
comingtime[reverse_graph[current.now][i].now] = current.time + reverse_graph[current.now][i].time
|
||||
pq.enqueue((now: reverse_graph[current.now][i].now, time: current.time + reverse_graph[current.now][i].time))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pq.enqueue((now: X, time: 0))
|
||||
gotime[X] = 0
|
||||
while !pq.isEmpty {
|
||||
if let current = pq.dequeue() {
|
||||
if gotime[current.now] < current.time {
|
||||
continue
|
||||
}
|
||||
|
||||
for i in 0..<graph[current.now].count {
|
||||
if gotime[graph[current.now][i].now] > current.time + graph[current.now][i].time {
|
||||
gotime[graph[current.now][i].now] = current.time + graph[current.now][i].time
|
||||
pq.enqueue((now: graph[current.now][i].now, time: current.time + graph[current.now][i].time))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var maxTime = 0;
|
||||
for n in 1...N {
|
||||
maxTime = max(maxTime, comingtime[n] + gotime[n])
|
||||
}
|
||||
|
||||
print(maxTime)
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user