-
Notifications
You must be signed in to change notification settings - Fork 0
3주차 기본 문제 #9
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
The head ref may contain hidden characters: "be/b/\uC804\uCC2C\uC758_working"
3주차 기본 문제 #9
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
#include<bits/stdc++.h> | ||
#define xx first | ||
#define yy second | ||
using namespace std; | ||
using ll = long long; | ||
using pii = pair<int,int>; | ||
using pll = pair<ll,ll>; | ||
using tpi = tuple<int,int,int>; | ||
using tpl = tuple<ll,ll,ll>; | ||
using dpi = pair<pii,pii>; | ||
using dpl = pair<pll,pll>; | ||
using pis = pair<int,string>; | ||
using psi = pair<string,int>; | ||
|
||
const int INF = 0x3f3f3f3f; | ||
const ll INF64 = 0x3f3f3f3f3f3f3f3f; | ||
|
||
int N,M; | ||
vector<int> adj[10001]{}; | ||
bool vi[10001]{}; | ||
vector<pii> arr; | ||
|
||
int bfs(int st){ | ||
memset(vi,0,sizeof(vi)); | ||
queue<int> q; | ||
q.push(st); | ||
vi[st] = true; | ||
|
||
int ret = 1; | ||
while (!q.empty()) { | ||
int cn = q.front(); | ||
q.pop(); | ||
|
||
for(auto next : adj[cn]){ | ||
if(vi[next]) continue; | ||
q.push(next); | ||
vi[next] = true; | ||
ret++; | ||
} | ||
} | ||
return ret; | ||
} | ||
|
||
int main() { | ||
ios_base::sync_with_stdio(0); | ||
cin.tie(0); | ||
cin>>N>>M; | ||
int a,b; | ||
for(int i=0; i<M; i++){ | ||
cin>>a>>b; | ||
adj[b].push_back(a); | ||
} | ||
|
||
for(int i=1; i<=N; i++) arr.push_back({bfs(i), i}); | ||
|
||
sort(arr.begin(), arr.end(), [&](pii a, pii b) -> bool{ | ||
if(a.xx == b.xx) return a.yy < b.yy; | ||
return a.xx > b.xx; | ||
}); | ||
|
||
int pre = -1; | ||
for(auto val : arr){ | ||
if(pre!=-1 && pre!=val.xx) break; | ||
pre = val.xx; | ||
cout<<val.yy<<'\n'; | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import java.io.BufferedReader; | ||
import java.io.BufferedWriter; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.io.OutputStreamWriter; | ||
import java.util.ArrayDeque; | ||
import java.util.ArrayList; | ||
import java.util.Queue; | ||
import java.util.StringTokenizer; | ||
|
||
public class Main{ | ||
static int N, K, S, X, Y; | ||
static int[][] m; | ||
static boolean[][] vi; | ||
static ArrayList<ArrayList<Tiii>> adj; // 바이러스 1~K 의 위치 저장 | ||
static int dy[] = {-1,1,0,0}; | ||
static int dx[] = {0,0,-1,1}; | ||
|
||
public static void main(String[] args) throws IOException { | ||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); // 입력 | ||
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); // 출력 | ||
StringTokenizer st = new StringTokenizer(br.readLine());; | ||
|
||
N = Integer.parseInt(st.nextToken()); | ||
K = Integer.parseInt(st.nextToken()); | ||
|
||
m = new int[N][N]; | ||
vi = new boolean[N][N]; | ||
adj = new ArrayList<>(); | ||
for(int i=0; i<=K; i++) adj.add(new ArrayList<>()); | ||
for(int i=0; i<N; i++){ | ||
st = new StringTokenizer(br.readLine()); | ||
for(int j=0; j<N; j++){ | ||
m[i][j] = Integer.parseInt(st.nextToken()); | ||
// m[i][j]번 바이러스들의 위치를 adj.get(m[i][j])에 인접리스트 형태로 저장 | ||
if(m[i][j] != 0) adj.get(m[i][j]).add(new Tiii(i,j,0)); // Tiii(y,x,d) : y축좌표, x축좌표, 현재 지난 시간(초) | ||
} | ||
} | ||
st = new StringTokenizer(br.readLine()); | ||
S = Integer.parseInt(st.nextToken()); | ||
X = Integer.parseInt(st.nextToken()); | ||
Y = Integer.parseInt(st.nextToken()); | ||
|
||
bfs(); | ||
|
||
bw.write(m[X-1][Y-1]+""); // 정답 bufferWriter에 저장 | ||
bw.flush(); | ||
bw.close(); | ||
br.close(); | ||
} | ||
|
||
static void bfs(){ | ||
Queue<Tiii> q = new ArrayDeque<>(); | ||
for(int i=1; i<=K; i++){ | ||
for (Tiii next : adj.get(i)) { // 1번 바이러스들 부터 큐에 순차적으로 저장 | ||
q.add(next); | ||
vi[next.y][next.x] = true; // 방문처리 | ||
} | ||
} | ||
|
||
while(!q.isEmpty()){ | ||
Tiii poll = q.poll(); | ||
int cy = poll.y; | ||
int cx = poll.x; | ||
int cd = poll.d; | ||
|
||
if(cd == S) continue; // S초일때 답을 구해야 하므로 S초라면 증식 중지 | ||
|
||
for(int i=0; i<4; i++){ | ||
int ny = cy + dy[i]; | ||
int nx = cx + dx[i]; | ||
if(ny<0 || nx<0 || ny>=N || nx>=N || vi[ny][nx] || m[ny][nx]!=0) continue; | ||
q.add(new Tiii(ny,nx,cd+1)); // 증식할 수 있으면, 다음 y축과 x좌표 그리고 현재 지난시간(초)를 큐에 저장 | ||
vi[ny][nx] = true; | ||
m[ny][nx] = m[cy][cx]; // 다음 위치에 바이러스 번호 업데이트 | ||
} | ||
} | ||
} | ||
|
||
static class Tiii{ // y축, x축, 지난 시간(초) | ||
int y,x,d; | ||
|
||
public Tiii(int y, int x, int d) { | ||
this.y = y; | ||
this.x = x; | ||
this.d = d; | ||
} | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import java.io.BufferedReader; | ||
import java.io.BufferedWriter; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.io.OutputStreamWriter; | ||
import java.util.ArrayDeque; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.Queue; | ||
import java.util.StringTokenizer; | ||
|
||
public class Main{ | ||
static int N, M; | ||
static ArrayList<ArrayList<Integer>> adj; | ||
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. 풀이 자체는 비슷한 것 같은데 제 코드는 메모리가 47404KB, 찬의님 코드는 메모리가 33872KB가 나오더라구요. 왜 그런가 찾아봤는데 저는 Map을 사용하고 찬의님은 ArrayList를 사용해서 그런 것 같습니다. 스택오버플로우의 답변을 보니까 HashMap이 키, 값을 둘 다 저장해야 하고 load factor를 유지하기 위해서 메모리를 좀 더 많이 소모한다고 하더라구요. 저도 ArrayList를 활용하는 방안으로 풀이를 작성해야겠습니다. |
||
static int[] vi; | ||
|
||
public static void main(String[] args) throws IOException { | ||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); // 입력 | ||
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); // 출력 | ||
StringTokenizer st = new StringTokenizer(br.readLine());; | ||
|
||
N = Integer.parseInt(st.nextToken()); | ||
M = Integer.parseInt(st.nextToken()); | ||
|
||
vi = new int[N+1]; | ||
Arrays.fill(vi, -1); | ||
adj = new ArrayList<>(); | ||
for(int i=0; i<=N; i++) adj.add(new ArrayList<>()); | ||
|
||
int a,b; | ||
for(int i=0; i<M; i++){ | ||
st = new StringTokenizer(br.readLine());; | ||
a = Integer.parseInt(st.nextToken()); | ||
b = Integer.parseInt(st.nextToken()); | ||
adj.get(a).add(b); | ||
adj.get(b).add(a); | ||
} | ||
|
||
int resNum = -1; | ||
int resDist = bfs(); | ||
int resCnt = 0; | ||
for(int i=1; i<=N; i++){ | ||
if(vi[i]!=resDist) continue; | ||
if(resNum==-1) resNum = i; | ||
resCnt++; | ||
} | ||
|
||
bw.write(resNum + " " + resDist + " " + resCnt); | ||
bw.flush(); | ||
bw.close(); | ||
br.close(); | ||
} | ||
|
||
static int bfs(){ | ||
Queue<Pii> q = new ArrayDeque<>(); | ||
q.add(new Pii(1,0)); | ||
vi[1] = 0; | ||
|
||
int dist = 0; | ||
while (!q.isEmpty()) { | ||
Pii poll = q.poll(); | ||
int cn = poll.n; | ||
int cd = poll.d; | ||
dist = Math.max(dist, cd); | ||
|
||
for (Integer next : adj.get(cn)) { | ||
if(vi[next]>=0) continue; | ||
q.add(new Pii(next, cd+1)); | ||
vi[next] = cd+1; | ||
} | ||
} | ||
return dist; | ||
} | ||
|
||
static class Pii{ | ||
int n,d; | ||
|
||
public Pii(int n, int d) { | ||
this.n = n; | ||
this.d = d; | ||
} | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import java.io.BufferedReader; | ||
import java.io.BufferedWriter; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.io.OutputStreamWriter; | ||
import java.util.ArrayDeque; | ||
import java.util.Queue; | ||
import java.util.StringTokenizer; | ||
|
||
public class Main{ | ||
static int N, M; | ||
static int[][] m; | ||
static boolean[][] vi; | ||
static int dy[] = {-1,1,0,0}; | ||
static int dx[] = {0,0,-1,1}; | ||
|
||
public static void main(String[] args) throws IOException { | ||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); // 입력 | ||
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); // 출력 | ||
StringTokenizer st = new StringTokenizer(br.readLine());; | ||
|
||
M = Integer.parseInt(st.nextToken()); | ||
N = Integer.parseInt(st.nextToken()); | ||
|
||
Queue<Tiii> q = new ArrayDeque<>(); | ||
m = new int[N][M]; | ||
vi = new boolean[N][M]; | ||
for(int i=0; i<N; i++){ | ||
st = new StringTokenizer(br.readLine());; | ||
for(int j=0; j<M; j++){ | ||
m[i][j] = Integer.parseInt(st.nextToken()); | ||
if(m[i][j]==1) { // 익은 토마토 큐에 삽입 | ||
q.add(new Tiii(0, i, j)); | ||
vi[i][j] = true; | ||
} | ||
} | ||
} | ||
|
||
int ans = 0; | ||
while (!q.isEmpty()) { // bfs | ||
Tiii poll = q.poll(); | ||
int cd = poll.d; | ||
int cy = poll.y; | ||
int cx = poll.x; | ||
|
||
ans = Math.max(ans, cd); | ||
|
||
for(int i=0; i<4; i++){ | ||
int ny = cy + dy[i]; | ||
int nx = cx + dx[i]; | ||
if(ny<0 || nx<0 || ny>=N || nx>=M || vi[ny][nx] || m[ny][nx]==-1) continue; | ||
q.add(new Tiii(cd + 1, ny, nx)); | ||
vi[ny][nx] = true; | ||
} | ||
} | ||
|
||
int res = ans; | ||
for(int i=0; i<N; i++){ // 익지않은 토마토 검사 | ||
for(int j=0; j<M; j++){ | ||
if(m[i][j]==-1) continue; | ||
if(!vi[i][j]) { | ||
System.out.println(-1); | ||
System.exit(0); | ||
} | ||
} | ||
} | ||
|
||
bw.write(res+""); | ||
bw.flush(); | ||
bw.close(); | ||
br.close(); | ||
} | ||
|
||
static class Tiii{ // 시간, y좌표, x좌표 | ||
int d,y,x; | ||
|
||
public Tiii(int d, int y, int x) { | ||
this.d = d; | ||
this.y = y; | ||
this.x = x; | ||
} | ||
} | ||
|
||
} |
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.
저는 우선순위 큐로 날짜까지 저장해서 순차적으로 바이러스 셀에 접근할 수 있도록 했는데 이걸 보니 그냥 1번 바이러스부터 큐에 넣어주기만 하면 되는 거였네요... 간단한 방법인데 막상 풀 때는 생각이 안났네요 ㅋㅋㅋ