본문 바로가기

BFS

(29)
[BOJ] 3184번 양 (C++) https://www.acmicpc.net/problem/3184 3184번: 양 첫 줄에는 두 정수 R과 C가 주어지며(3 ≤ R, C ≤ 250), 각 수는 마당의 행과 열의 수를 의미한다. 다음 R개의 줄은 C개의 글자를 가진다. 이들은 마당의 구조(울타리, 양, 늑대의 위치)를 의미한다. www.acmicpc.net #include #include using namespace std; int r, c; char arr[250][250]; bool visited[250][250]; int dx[4] = { -1, 0, 0, 1 }; int dy[4] = { 0, -1, 1, 0 }; int cs = 0, cw = 0; void bfs(int a, int b){ int ts = 0, tw = 0; q..
[BOJ] 2589번 보물섬 (C++) https://www.acmicpc.net/problem/2589 2589번: 보물섬 보물섬 지도를 발견한 후크 선장은 보물을 찾아나섰다. 보물섬 지도는 아래 그림과 같이 직사각형 모양이며 여러 칸으로 나뉘어져 있다. 각 칸은 육지(L)나 바다(W)로 표시되어 있다. 이 지도에서 www.acmicpc.net #include #include using namespace std; int n, m; char arr[50][50]; int path[50][50] = {-1, }; int dx[4] = {0, 0, -1, 1}; int dy[4] = {-1, 1, 0, 0}; int maxlen = 0; void bfs(int x, int y){ queue q; q.push({x, y}); path[x][y] =..
[Programmers] 거리두기 확인하기 (C++) https://programmers.co.kr/learn/courses/30/lessons/81302 코딩테스트 연습 - 거리두기 확인하기 [["POOOP", "OXXOX", "OPXPX", "OOXOX", "POXXP"], ["POOPX", "OXPXP", "PXXXO", "OXXXO", "OOOPP"], ["PXOPX", "OXOXP", "OXPOX", "OXXOP", "PXPOX"], ["OOOXX", "XOOOX", "OOOXX", "OXOOX", "OOOOO"], ["PXPXP", "XPXPX", "PXPXP", "XPXPX", "PXPXP"]] [1, 0, 1, 1, 1] programmers.co.kr #include #include #include #include using namespac..
[BOJ] 1697번 숨바꼭질 (C++) https://www.acmicpc.net/problem/1697 1697번: 숨바꼭질 수빈이는 동생과 숨바꼭질을 하고 있다. 수빈이는 현재 점 N(0 ≤ N ≤ 100,000)에 있고, 동생은 점 K(0 ≤ K ≤ 100,000)에 있다. 수빈이는 걷거나 순간이동을 할 수 있다. 만약, 수빈이의 위치가 X일 www.acmicpc.net #include #include #include using namespace std; int n, v; int map[1001][1001]; int visit[1001]; void reset() { for (int i = 1; i > n1 >> n2; map[n1][n2] = map[n2][n1] = 1; } dfs(); reset(); bfs(); return 0; }..
[BOJ] 7576번 토마토 (C++) https://www.acmicpc.net/problem/7576 7576번: 토마토 첫 줄에는 상자의 크기를 나타내는 두 정수 M,N이 주어진다. M은 상자의 가로 칸의 수, N은 상자의 세로 칸의 수를 나타낸다. 단, 2 ≤ M,N ≤ 1,000 이다. 둘째 줄부터는 하나의 상자에 저장된 토마토 www.acmicpc.net #include #include using namespace std; #define MAX 1001 int N, M; int arr[MAX][MAX]; int dx[4] = { 1, -1, 0, 0 }; int dy[4] = { 0, 0, 1, -1 }; queue q; void bfs() { while (!q.empty()) { int x = q.front().first; int..
[BOJ] 2644번 촌수계산 (C++) https://www.acmicpc.net/problem/2644 2644번: 촌수계산 사람들은 1, 2, 3, …, n (1 ≤ n ≤ 100)의 연속된 번호로 각각 표시된다. 입력 파일의 첫째 줄에는 전체 사람의 수 n이 주어지고, 둘째 줄에는 촌수를 계산해야 하는 서로 다른 두 사람의 번호가 주어 www.acmicpc.net #include #include using namespace std; int n, m; int map[101][101]; int visit[101]; int depth[101]; void bfs(int start){ visit[start] = 1; queue q; q.push(start); while(!q.empty()){ int node = q.front(); q.pop();..
[BOJ] 1987번 알파벳 (C++) https://www.acmicpc.net/problem/1987 1987번: 알파벳 세로 R칸, 가로 C칸으로 된 표 모양의 보드가 있다. 보드의 각 칸에는 대문자 알파벳이 하나씩 적혀 있고, 좌측 상단 칸 (1행 1열) 에는 말이 놓여 있다. 말은 상하좌우로 인접한 네 칸 중의 한 칸으 www.acmicpc.net #include #include using namespace std; int dx[4] = { -1, 0, 0, 1 }; int dy[4] = { 0, -1, 1, 0 }; int r, c; char map[20][20]; int alphabet[26] = { 0, }; int max_path = 0; void dfs(int row, int col, int find_path) { max_p..
[BOJ] 2178번 미로 탐색 (C++) https://www.acmicpc.net/problem/2178 2178번: 미로 탐색 첫째 줄에 두 정수 N, M(2 ≤ N, M ≤ 100)이 주어진다. 다음 N개의 줄에는 M개의 정수로 미로가 주어진다. 각각의 수들은 붙어서 입력으로 주어진다. www.acmicpc.net #include #include using namespace std; char map[101][101]; int check[101][101]; bool visit[101][101]; int n, m; int dx[4] = { -1, 0, 1, 0 }; int dy[4] = { 0, -1, 0, 1 }; void bfs() { visit[0][0] = true; int cx, cy, ax, ay; queue q; q.push({ ..