정신과 시간의 방

전체 글 273

카테고리 설명
게임 프로그래머가 되기 위한 청춘의 기록
작성일
2025. 4. 26. 18:00
작성자
risehyun
작성일
2025. 4. 24. 22:54
작성자
risehyun
작성일
2025. 4. 2. 21:50
작성자
risehyun
작성일
2024. 11. 30. 22:14
작성자
risehyun
  • 문제

 

 

  • 풀이
#include <iostream>
#include <queue>
using namespace std;

int box[1002][1002];
int dist[1002][1002];

int n, m, result;

int dy[4] = { -1, 1, 0, 0 };
int dx[4] = { 0, 0, -1, 1 };

int main()
{
	cin >> m >> n;

	for (int y = 0; y < n; y++)
	{
		for (int x = 0; x < m; x++)
		{
			cin >> box[y][x];
			dist[y][x] = -1;
		}
	}

	queue<pair<int, int>> q;

	for (int y = 0; y < n; y++)
	{
		for (int x = 0; x < m; x++)
		{
			if (box[y][x] == 1)
			{
				q.push({ y, x });
				dist[y][x] = 0;
			}
		}
	}

	while (!q.empty())
	{
		auto cur = q.front();
		q.pop();

		for (int dir = 0; dir < 4; dir++)
		{
			int ny = cur.first + dx[dir];
			int nx = cur.second + dy[dir];

			if (ny < 0 || ny >= n || nx < 0 || nx >= m)
			{
				continue;
			}

			if (box[ny][nx] == -1 || dist[ny][nx] != -1)
			{
				continue;
			}

		}
	}

	int result = 0;
	for (int y = 0; y < n; y++)
	{
		for (int x = 0; x < m; x++)
		{
			if (box[y][x] == 0 && dist[y][x] == -1)
			{
				cout << -1 << '\n';
				return 0;
			}
			result = max(result, dist[y][x]);
		}
	}

	return 0;
}
작성일
2024. 11. 20. 09:41
작성자
risehyun

'코딩테스트 > [자습] 알고리즘 스터디' 카테고리의 다른 글

알고리즘 스터디 3주차  (0) 2024.11.13
알고리즘 스터디 2주차  (0) 2024.11.06
알고리즘 스터디 1주차  (0) 2024.10.30
작성일
2024. 11. 17. 23:01
작성자
risehyun
  • 문제

 

  • 풀이
#include <iostream>
#include <queue>
#include <string>
using namespace std;

int maze[102][102];
int dist[102][102];
int n, m;

int dx[4] = { -1, 1, 0, 0 };
int dy[4] = { 0, 0, -1, 1 };

int bfs() {
    queue<pair<int, int>> q;
    q.push({ 0, 0 });
    dist[0][0] = 1;

    while (!q.empty()) {
        int x = q.front().first;
        int y = q.front().second;
        q.pop();

        for (int dir = 0; dir < 4; dir++) {
            int nx = x + dx[dir];
            int ny = y + dy[dir];

            if (nx < 0 || nx >= n || ny < 0 || ny >= m) continue;
            if (dist[nx][ny] != 0) continue;
            if (maze[nx][ny] == 0) continue;

            dist[nx][ny] = dist[x][y] + 1;
            q.push({ nx, ny });
        }
    }

    return dist[n - 1][m - 1];
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    cin >> n >> m;

    for (int i = 0; i < n; i++) {
        string line;
        cin >> line;
        for (int j = 0; j < m; j++) {
            maze[i][j] = line[j] - '0';
        }
    }

    cout << bfs() << '\n';

    return 0;
}

 

  • 메모

2178번처럼 최단 거리의 경로를 구하는 문제의 경우 일반적으로 BFS 또는 다익스트라(가중치가 있는 경우)를 사용한다. 

여기서는 다음과 같은 이유 때문에 DFS의 사용이 적절치 않다. 

 

1) 한 경로를 끝까지 추적한 뒤에야 다른 경로를 탐색하기 때문에 첫 번째 시도한 경로가 최단 경로인지 보장할 수 없다. 

2) 경로가 여러 갈래인 경우 최장 경로를 먼저 갈 가능성도 존재하기 때문에 적절하지 않다. 

3) 불필요한 재귀함수를 사용하게 된다. 

 

구조적 바인딩(structured binding) 을 활용하면 아래 코드를 간략화할수 있다. 단, C++ 17 이후로 사용 가능하다.

 

- 원래 코드

pair<int, int> cur = q.front();
int cx = cur.first;
int cy = cur.second;

 

- 구조적 바인딩으로 간략화

auto [cx, cy] = q.front();

 

- 문법 예시

pair<int, int> p = {3, 7};
auto [a, b] = p;
cout << a << ", " << b << "\n"; // 3, 7

 

 

CT2

작성일
2024. 11. 16. 10:19
작성자
risehyun
작성일
2024. 11. 15. 21:47
작성자
risehyun