From fd4e11559bf450305ef73b1e56aa51ec2394da5d Mon Sep 17 00:00:00 2001 From: dalsu0222 Date: Thu, 8 May 2025 00:48:18 +0900 Subject: [PATCH 1/3] =?UTF-8?q?=EA=B7=B8=EB=9E=98=ED=94=84=5F=ED=83=90?= =?UTF-8?q?=EC=83=89=5F2=5F14218.java?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 07_graph/shin/14218.java | 86 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 07_graph/shin/14218.java diff --git a/07_graph/shin/14218.java b/07_graph/shin/14218.java new file mode 100644 index 0000000..0f66ab0 --- /dev/null +++ b/07_graph/shin/14218.java @@ -0,0 +1,86 @@ +import java.io.*; +import java.util.*; + +public class 14218 { + static int n,m,q; + static ArrayList[] graph; // 각 배열 원소는 ArrayList + static int[] ans; + static StringBuilder sb = new StringBuilder(); + + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st = new StringTokenizer(br.readLine()); + + n = Integer.parseInt(st.nextToken()); + m = Integer.parseInt(st.nextToken()); + + init(); + + for(int i=0 ; i(); + } + } + + // 이미 방문한 노드라도 더 짧은 경로로 도달했다면 갱신해야함!!! + static void calcDist(int x) { + Queue q = new ArrayDeque<>(); + q.offer(new int[] {x,0}); + Arrays.fill(ans, Integer.MAX_VALUE); + + while(!q.isEmpty()) { + int[] cur = q.poll(); + int curX = cur[0]; + int cnt = cur[1]; + + for(int newX : graph[curX]) { + if(ans[newX] > cnt + 1) { + ans[newX] = cnt + 1; + q.offer(new int[]{newX, cnt + 1}); + } + } + + } + + } + + static void printAns() { + ans[1] = 0; + for(int i=1 ; i<=n ; i++) { + sb.append(ans[i] == Integer.MAX_VALUE ? -1 : ans[i]).append(" "); + } + sb.append("\n"); + } +} From 70571606b7bacceadef47890fa8de378bb990a34 Mon Sep 17 00:00:00 2001 From: dalsu0222 Date: Thu, 8 May 2025 00:50:04 +0900 Subject: [PATCH 2/3] =?UTF-8?q?=EB=82=98=EC=9D=B4=ED=8A=B8=EC=9D=98=5F?= =?UTF-8?q?=EC=9D=B4=EB=8F=99=5F7562.cpp?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 07_graph/shin/7562.cpp | 68 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 07_graph/shin/7562.cpp diff --git a/07_graph/shin/7562.cpp b/07_graph/shin/7562.cpp new file mode 100644 index 0000000..5a0a31a --- /dev/null +++ b/07_graph/shin/7562.cpp @@ -0,0 +1,68 @@ +#include +#include +#include +#include + + +using namespace std; + +struct pos { + int x, y; + int cnt; +}; + +int t, l, cX, cY, gX, gY; +bool visited[301][301]; +int dx[8] = { -1,-2,-2,-1,1,2,2,1 }; +int dy[8] = { -2,-1,1,2,-2,-1,1,2 }; + +int bfs(int cX, int cY) { + int min_move = INT_MAX; + queue q; + q.push({ cX,cY,0 }); + visited[cX][cY] = true; + + while (!q.empty()) { + pos p = q.front(); + q.pop(); + int cx = p.x; + int cy = p.y; + int cnt = p.cnt; + + for (int i = 0; i < 8; i++) { + int nx = cx + dx[i]; + int ny = cy + dy[i]; + + if (nx < 0 || ny < 0 || nx >= l || ny >= l || visited[nx][ny]) + continue; + if (nx == gX && ny == gY) { + min_move = min(min_move, cnt + 1); + } + else { + q.push({ nx,ny,cnt + 1 }); + visited[nx][ny] = true; + } + } + } + + + return min_move; +} + +int main() { + ios::sync_with_stdio(0); + cin.tie(0); cout.tie(0); + + cin >> t; + while (t--) { + cin >> l >> cX >> cY >> gX >> gY; + + // bool 배열 초기화 + fill(visited[0], visited[301], false); + + int result = bfs(cX, cY); + cout << (result == INT_MAX ? 0 : result) << "\n"; + } + + return 0; +} \ No newline at end of file From 230df943d03e2a9a9a077f0ed4453daf23a43c75 Mon Sep 17 00:00:00 2001 From: dalsu0222 Date: Thu, 8 May 2025 00:51:06 +0900 Subject: [PATCH 3/3] =?UTF-8?q?=EC=A0=81=EB=A1=9D=EC=83=89=EC=95=BD=5F1002?= =?UTF-8?q?6.cpp?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 07_graph/shin/10026.cpp | 89 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 07_graph/shin/10026.cpp diff --git a/07_graph/shin/10026.cpp b/07_graph/shin/10026.cpp new file mode 100644 index 0000000..4ccafb4 --- /dev/null +++ b/07_graph/shin/10026.cpp @@ -0,0 +1,89 @@ +#include +#include +#include + +using namespace std; +int n; + +char map[101][101]; +bool isVisited[101][101]; +bool isVisited2[101][101]; +int cnt, cnt2; // cnt : 적록색약이 아닌 사람이 봤을때의 구역의 개수 ,cnt2 : // 적록색약인 사람이 봤을때의 구역의 개수 +int dx[4] = { -1,1,0,0 }; +int dy[4] = { 0,0,-1,1 }; + +// 적록색약이 아닌 사람이 봤을때의 구역 파악하기 +void bfs(int x, int y) { + isVisited[x][y] = true; + queue> q; + q.push({ x,y }); + char color = map[x][y]; + while (!q.empty()) { + int topx = q.front().first; + int topy = q.front().second; + q.pop(); + for (int i = 0; i < 4; i++) { + int nx = topx + dx[i]; + int ny = topy + dy[i]; + if (nx < 0 || nx >= n || ny < 0 || ny >= n) + continue; + if (!isVisited[nx][ny] && map[nx][ny] == color) { + q.push({ nx,ny }); + isVisited[nx][ny] = true; + } + } + } +} +// 적록색약인 사람이 봤을때의 구역 파악하기 +void bfs2(int x, int y) { + isVisited2[x][y] = true; + queue> q; + q.push({ x,y }); + char color = map[x][y]; + while (!q.empty()) { + int topx = q.front().first; + int topy = q.front().second; + q.pop(); + for (int i = 0; i < 4; i++) { + int nx = topx + dx[i]; + int ny = topy + dy[i]; + if (nx < 0 || nx >= n || ny < 0 || ny >= n) + continue; + if (!isVisited2[nx][ny] && (map[nx][ny] == color || + map[nx][ny] == 'R' && color == 'G' || map[nx][ny] == 'G' && color == 'R')) { + q.push({ nx,ny }); + isVisited2[nx][ny] = true; + } + } + } +} + +int main() +{ + ios_base::sync_with_stdio(false); + cin.tie(NULL); + cout.tie(NULL); + + cin >> n; + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + cin >> map[i][j]; + } + } + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + if (!isVisited[i][j]) { + bfs(i, j); + cnt++; + } + if (!isVisited2[i][j]) { + bfs2(i, j); + cnt2++; + } + } + } + + cout <