-
Notifications
You must be signed in to change notification settings - Fork 2
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
24-H0ngJu #233
Conversation
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.
ํํ ๊ฐ ์์ง์๋ง๋ค BFS ๋๋ ค์ ์ต๋จ๊ฑฐ๋ฆฌ ๊ตฌํ ์๊ฐ์ด ์ค์ผ ์๋ค์์๊น์...
ํ์ฐธ ์ฝ์งํ๋ค๊ฐ ๊ฒจ์ฐ ๋ ์ฌ๋ ธ๋ค์ ใ
์ ์ฒด ์ฝ๋
#include <iostream>
#include <vector>
#include <queue>
#include <tuple>
#include <algorithm>
using namespace std;
constexpr int BABY = 9;
const vector<pair<int, int>> Offset{{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
int main()
{
cin.tie(nullptr)->sync_with_stdio(false);
int N; cin >> N;
vector<vector<int>> Space(N, vector(N, 0));
int X = 0, Y = 0;
int SizeofBaby = 2, NumFishEaten = 0;
for(int i = 0; i < N; ++i)
{
for(int j = 0; j < N; ++j)
{
cin >> Space[i][j];
if(Space[i][j] == BABY)
{
X = i, Y = j;
Space[i][j] = 0;
}
}
}
deque<tuple<int, int, int>> Q;
vector<vector<bool>> Visited;
auto CanEatFish = [&](int x, int y)
{
return 1 <= Space[x][y] && Space[x][y] < SizeofBaby;
};
auto CanMoveTo = [&](int x, int y)
{
if(x < 0 || x >= N || y < 0 || y >= N) return false;
if(Visited[x][y]) return false;
if(Space[x][y] > SizeofBaby) return false;
return true;
};
int Tick = 0;
while(true)
{
Q.clear();
Visited.assign(N, vector(N, false));
Q.emplace_back(X, Y, 0);
Visited[X][Y] = true;
int MinDistance = 400;
vector<pair<int, int>> Candidates;
while(!Q.empty())
{
const auto [x, y, d] = Q.front();
Q.pop_front();
if(CanEatFish(x, y))
{
if(d < MinDistance)
{
MinDistance = d;
Candidates = {{x, y}};
}
else if(d == MinDistance)
{
Candidates.emplace_back(x, y);
}
}
for(const auto& [dx, dy] : Offset)
{
int nx = x + dx, ny = y + dy;
if(!CanMoveTo(nx, ny)) continue;
Visited[nx][ny] = true;
Q.emplace_back(nx, ny, d + 1);
}
}
if(Candidates.empty()) // Cannot Eat Fish Anymore
{
break;
}
pair<int, int> MinPoint = *min_element(Candidates.begin(), Candidates.end());
Space[MinPoint.first][MinPoint.second] = 0;
Tick += MinDistance;
NumFishEaten++;
if(SizeofBaby == NumFishEaten)
{
SizeofBaby++;
NumFishEaten = 0;
}
X = MinPoint.first, Y = MinPoint.second;
}
cout << Tick;
return 0;
}
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.
์ ์ ์ฒจ์ BFS๋ก ํ๋ค๊ฐ ๋๋ฌด ์ ๊ฒฝ์จ์ผํ ๋ถ๋ถ์ด ๋ง์์,
๋ฌผ๊ณ ๊ธฐ ํฌ๊ธฐ(์ต๋), ๋ฌผ๊ณ ๊ธฐ ์คํ(์ต๋), ๊ฑธ๋ฆฐ ์๊ฐ(์ต์), y(์ต์), x(์ต์) ํ์ผ๋ก ๋๋ ธ๋๋ฐ...
BFS๋ ๊ฐ๋ฅํ๊ตฐ์.........
from heapq import *
import sys
def input(): return sys.stdin.readline().rstrip()
N = int(input())
board = []
start = None
for row_idx in range(N):
row = list(map(int, input().split()))
if 9 in row: start = row_idx, (row.index(9))
board.append(row)
board[start[0]][start[1]] = 0
dx = [0, -1, 1, 0]
dy = [-1, 0, 0, 1]
deq = []
heappush(deq,(-2, 0, 0, start[0], start[1]))
visited = set(start)
answer = 0
max_stack = 0
max_size = 2
while deq:
now_size, now_stack, now_time, now_y, now_x = heappop(deq)
now_size *= -1
now_stack *= -1
if max_size >= now_size and max_stack > now_stack: continue
if max_size > now_size: continue
if board[now_y][now_x] != 0 and board[now_y][now_x] < now_size:
board[now_y][now_x] = 0
visited = set()
if now_stack + 1 == now_size:
now_size += 1
now_stack = 0
else: now_stack += 1
max_size = now_size
max_stack = now_stack
answer += now_time
now_time = 0
for dir in range(4):
new_y = now_y + dy[dir]
new_x = now_x + dx[dir]
if new_x < 0 or new_x >= N: continue
if new_y < 0 or new_y >= N: continue
if (new_x, new_y) in visited: continue
if board[new_y][new_x] > max_size: continue
heappush(deq,(now_size * -1, now_stack * -1, now_time + 1, new_y, new_x))
visited.add((new_x, new_y))
print(answer)
์์ค ๋ฌธ์ ์๋ชป์ฝ์ด์ ์๊ฐ์ ์์ฅ์ฐฝ ๋ ๋ ธ๋ค์.......... ใ ใ ใ ใ ใ ใ ใ ใ ใ ใ ใ ใ ใ ใ ใ ใ ใ ใ ใ ใ ใ
๊ฐ๊ฑฐ๋ ์์ ๊ฑฐ๋ง ์ง๋๊ฐ ์ ์๋ ๊ฑฐ์๊ตฐ์....
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.
์ ๋ชจ์์ ??????
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.
ใ ใ ฃ ์์ด๋ ์ ๋ฒ์ฃผ ์์์ผ ๋ด๋ค๋ฒ๋ฆฐ pr..........
if 0 < space[cx][cy] < baby: | ||
if dis < min_dis: | ||
min_dis = dis | ||
tmp = [(cx, cy)] | ||
elif dis == min_dis: | ||
tmp.append((cx, cy)) | ||
|
||
for dx, dy in dir: | ||
nx, ny = cx + dx, cy + dy | ||
if check(nx, ny) and not visited[nx][ny] and space[nx][ny] <= baby: | ||
visited[nx][ny] = True | ||
q.append((nx, ny, dis + 1)) | ||
|
||
if not tmp: | ||
break | ||
|
||
tx, ty = min(tmp) | ||
space[tx][ty] = 0 | ||
time += min_dis | ||
fish += 1 | ||
if fish == baby: | ||
baby += 1 | ||
fish = 0 | ||
x, y = tx, ty | ||
|
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.
๊ฐ ์ ์๋ ๊ณณ tmp
์ ๋ค ๋ฃ์ด๋๊ณ
๋ง์ง๋ง์ ์ต์ ๊ฑฐ๋ฆฌ๋ ์กฐ๊ฑด์ ๋ถํฉํ๋ ๊ณณ์ผ๋ก ์๊ฐ์ด๋ ใ ใทใทใทใทใทใทใท
์ด๊ฑธ ์ ์๊ฐ ๋ชปํ์ง...
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.
tmp๋ฅผ ์ด๋ ๊ฒ ๊น๋ํ ์ธ ์ ์๊ตฐ์ ์ ๋ ๋๋ฌด ๋๋ฝ๊ฒ ์ป๋ค์ ใทใทใทใทใท....
์ ๋ ์ฝ๊ฐ ์ ์์ ๋ฐฉ๋ฒ์ ์ฌ์ฉํ์ด์!! ๊ทผ๋ฐ ๋ฌผ๊ณ ๊ธฐ ์ฐพ๊ณ ๋จน๋ ๋ถ๋ถ์์ ์กฐ๊ธ ์ค๋ฅ๊ฐ ๊ฑธ๋ ธ๋...
์์ผ๋ก ํ์์ต๋๋ค! from collections import deque
dx, dy = [0,0,-1,1], [1,-1,0,0]
N = int(input())
map = [list(map(int, input().split())) for _ in range(N)]
baby_shark = 2
shark_eaten = 0
for i in range(N):
for j in range(N):
if map[i][j] == 9:
now_x, now_y = i, j
map[i][j] = 0
def bfs():
global now_x, now_y, baby_shark, shark_eaten
queue = deque()
queue.append((now_x, now_y))
visited = [[False] * N for _ in range(N)]
visited[now_x][now_y] = True
distance = [[0] * N for _ in range(N)]
temp = []
while queue:
x, y = queue.popleft()
for i in range(4):
nx, ny = x + dx[i], y + dy[i]
if 0 <= nx < N and 0 <= ny < N:
if not visited[nx][ny] and map[nx][ny] <= baby_shark:
queue.append((nx, ny))
visited[nx][ny] = True
distance[nx][ny] = distance[x][y] + 1
if 0 < map[nx][ny] < baby_shark:
temp.append((nx, ny, distance[nx][ny]))
if temp:
# ๊ฐ์ฅ ๊ฐ๊น์ด ๋ฌผ๊ณ ๊ธฐ ์ฐพ๊ธฐ
temp.sort(key=lambda x: (x[2], x[0], x[1]))
target_x, target_y, dist = temp[0]
# ๋ฌผ๊ณ ๊ธฐ ๋จน๊ธฐ
map[target_x][target_y] = 0
shark_eaten += 1
if shark_eaten == baby_shark:
baby_shark += 1
shark_eaten = 0
now_x, now_y = target_x, target_y
return dist # ์ด๋ํ ๊ฑฐ๋ฆฌ ๋ฐํ
return 0 # ๋ ์ด์ ๋จน์ ๋ฌผ๊ณ ๊ธฐ๊ฐ ์์ ๋
total_time = 0
while True:
time = bfs()
if time == 0:
break
total_time += time
print(total_time) |
if 0 < space[cx][cy] < baby: | ||
if dis < min_dis: | ||
min_dis = dis | ||
tmp = [(cx, cy)] | ||
elif dis == min_dis: | ||
tmp.append((cx, cy)) | ||
|
||
for dx, dy in dir: | ||
nx, ny = cx + dx, cy + dy | ||
if check(nx, ny) and not visited[nx][ny] and space[nx][ny] <= baby: | ||
visited[nx][ny] = True | ||
q.append((nx, ny, dis + 1)) | ||
|
||
if not tmp: | ||
break | ||
|
||
tx, ty = min(tmp) | ||
space[tx][ty] = 0 | ||
time += min_dis | ||
fish += 1 | ||
if fish == baby: | ||
baby += 1 | ||
fish = 0 | ||
x, y = tx, ty | ||
|
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.
tmp๋ฅผ ์ด๋ ๊ฒ ๊น๋ํ ์ธ ์ ์๊ตฐ์ ์ ๋ ๋๋ฌด ๋๋ฝ๊ฒ ์ป๋ค์ ใทใทใทใทใท....
๐ ๋ฌธ์ ๋งํฌ
์๊ธฐ์์ด
โ๏ธ ์์๋ ์๊ฐ
1.5H
โจ ์๋ ์ฝ๋
๋ฌธ์ ์์ฝ
์๊ธฐ์์ด๋ ์์ ๋ณด๋ค ์์ ๋ฌผ๊ณ ๊ธฐ๋ง ๋จน์ ์ ์๋ค. ์ฃผ์ด์ง ๊ณต๊ฐ์์ ๋ช ์ด ๋์ ์๊ธฐ์์ด๋ ๋ฌผ๊ณ ๊ธฐ๋ฅผ ๋จน์ ์ ์๋๊ฐ?
์๊ฐ์ ์ด๋ํ๋๋ฐ ๊ฑธ๋ฆฐ ์๊ฐ๋ง ๊ณ์ฐํ๋ค. ๋จน์ ์ ์๋ ๋ฌผ๊ณ ๊ธฐ๊ฐ ์ฌ๋ฌ ๋ง๋ฆฌ์ธ ๊ฒฝ์ฐ, ๊ฐ์ฅ ๊ฐ๊น์ด ๋ฌผ๊ณ ๊ธฐ๋ฅผ ๋จน๋๋ค.
๋ฌธ์ ๋ฅผ ๋ณด์๋ง์ BFS๋ผ๋ ๊ฒ์ ๋ค๋ค ์๊ฐํ์ จ์ ๊ฒ ๊ฐ์ต๋๋ค.
์ด ๋ฌธ์ ๊ฐ ๊น๋ค๋กญ๋ค๊ณ ๋๋๊ฑด ๋จ์ํ BFS๊ฐ ์๋๋ผ, ๊ฐ ์ ์๋ ๋ ธ๋(๋ฌผ๊ณ ๊ธฐ)์ ๋ฆฌ์คํธ๊ฐ ์๊ธฐ๋ฉด, ๊ฐ์ฅ ๊ฐ๊น์ด ๊ณณ์ ๊ฐ์ผ ํ๋ค๋ ๊ฒ์ ์ถ๊ฐ๋ก ๊ด๋ฆฌ ํด์ค์ผ ํ๋ค๋ ์ ์ ๋๋ค.
๊ทธ๋์ ์ ๋ ์ผ๋จ tmp๋ผ๋ ์์ ๋ฆฌ์คํธ๋ค์ ๋ง๋ค์ด์ฃผ๊ณ ,
์๊ธฐ ์์ด๊ฐ ๋จน์ ์ ์๋ ๋ฌผ๊ณ ๊ธฐ๋ค์ ์์น์ ๊ฑฐ๋ฆฌ๋ฅผ ๋ฃ์ด์คฌ์ต๋๋ค.
tmp ๋ฆฌ์คํธ์ ๋ค์ด๊ฐ ์ ์๋ ์กฐ๊ฑด์
a. tmp ์ต์๊ฑฐ๋ฆฌ๋ณด๋ค ๋ ์์ ๊ฒฝ์ฐ : tmp๋ฅผ ์ด๊ธฐํ
b. tmp ์ต์๊ฑฐ๋ฆฌ์ ๊ฐ์ ๊ฒฝ์ฐ : tmp.appned()
์ฆ,
์ด๋ ๊ฒ ๋ฉ๋๋ค.
๊ทธ๋ฆฌ๊ณ , ๊ฐ ์ ์๋ ์์น๋ค์ ๋ค ์กฐ์ฌํ๋ค๋ฉด,
tmp ๋ฆฌ์คํธ ์ค์์ ๊ฐ์ฅ ์์ ์์ ๊ฒ์ ๋ฝ์์, ์๊ธฐ ์์ด๋ฅผ ์ฎ๊ฒจ์ฃผ๋ฉด ๋ฉ๋๋ค.
์ฌ๊ธฐ๊น์ง ์ฒ๋ฆฌํ๊ณ ,,
๋น์ฐํ ํต๊ณผ๊ฒ ์ง?ํ๊ณ ์ ์ถํ๋๋ฐ ์คํจ๊ฐ ๋จ๋๋ผ๊ณ ์
์ฒ์ ์์์ธ 9์์,, 0์ผ๋ก ์ฒ๋ฆฌํด์ค์ผ ํ๋ค๋ ๊ฑธ ๊น๋จน๊ณ
๋ ํ~์ฐธ ๋ค์ฌ๋ค ๋ดค์ต๋๋ค..
๐คฏ๐จ
๐ ์๋กญ๊ฒ ์๊ฒ๋ ๋ด์ฉ