[백준] 3055번 탈출

2025. 6. 2. 11:59·백준 문제/BFS

문제: 3055번: 탈출

 

//#include <bits/stdc++.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <climits>
#include <string>
#include <queue>
#include <set>
#include <iomanip>
#include <string.h>
#include <sstream>
#include <tuple>
#include <map>
#include <stack>
#include <queue>

using namespace std;

vector<vector<char>> world;
vector<vector<int>> visits;
queue<pair<int, int>> waterNexts;
queue<pair<int, int>> hedgehogNexts;

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

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

    int r, c;
    cin >> r >> c;
    world = vector<vector<char>>(r, vector<char>(c));
    visits = vector<vector<int>>(r, vector<int>(c));
    cin.ignore();
    for (int i = 0; i < r; i++) {
        string line;
        getline(cin, line);

        for (int j = 0; j < c; j++) {
            world[i][j] = line[j];

            if (world[i][j] == 'S') {
                hedgehogNexts.push({ i, j }); // 고슴도치 시작 위치
                visits[i][j] = 1; // 방문 처리
            }
            else if (world[i][j] == '*') {
                waterNexts.push({ i, j });
                visits[i][j] = 1; // 방문 처리
            }
        }
    }
    int time = 0;
    // 고슴도치는 물이 찰 예정인 곳으로 갈 수 없으니까 물 먼저 확장시키자
    while (!hedgehogNexts.empty()) {
        // 물 먼저 확장
        queue<pair<int, int>> newWaterNexts;
        while (!waterNexts.empty()) {
            pair<int, int> waterPos = waterNexts.front(); waterNexts.pop();

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

                if (nx < 0 || nx > r - 1 || ny < 0 || ny > c - 1) continue;
                if (world[nx][ny] != '.') continue;   // 빈 칸이 아니면 스킵
                world[nx][ny] = '*';
                newWaterNexts.push({ nx, ny });
            }
        }
        waterNexts = newWaterNexts;

        // 고슴도치 이동
        queue<pair<int, int>> newHedgeNexts;
        while (!hedgehogNexts.empty()) {
            pair<int, int> hedgePos = hedgehogNexts.front(); hedgehogNexts.pop();
            int x = hedgePos.first;
            int y = hedgePos.second;
            if (world[x][y] == 'D') {
                cout << time;
                return 0;
            }

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

                if (nx < 0 || nx > r - 1 || ny < 0 || ny > c - 1) continue;
                if (world[nx][ny] == '*' || world[nx][ny] == 'X' || visits[nx][ny] == 1) continue;

                if (world[nx][ny] != 'D') {
                    world[nx][ny] = 'S';
                }
                visits[nx][ny] = 1;
                newHedgeNexts.push({ nx, ny });
            }
        }
        hedgehogNexts = newHedgeNexts;
        time++;
    }
    cout << "KAKTUS"; // 여기까지 도달하면 고슴도치가 갈 곳이 없는거..

    return 0;
}

 

그냥 물 먼저 확장시키고 다음에 고슴도치 이동하도록 했다.

'백준 문제/BFS' 카테고리의 다른 글
  • [백준] 16948번 데스 나이트
  • [백준] 6593번 상범 빌딩
  • [백준] 29634번 Hotel
  • [백준] 21736번 헌내기는 친구가 필요해
dubu0721
dubu0721
dubu0721 님의 블로그 입니다.
  • dubu0721
    dubu0721 님의 블로그
    dubu0721
  • 전체
    오늘
    어제
    • 분류 전체보기 (343) N
      • 프로그래밍언어론 정리 (5)
      • 컴퓨터네트워크 정리 (5)
      • 알고리즘&자료구조 공부 (64)
        • it 취업을 위한 알고리즘 문제풀이 입문 강의 (60)
        • 학교 알고리즘 수업 (3)
        • 실전프로젝트I (0)
      • 백준 문제 (196) N
        • 이분탐색 (7)
        • 투포인트 (10)
        • 그래프 (7)
        • 그리디 (24)
        • DP (25)
        • BFS (18) N
        • MST (7)
        • KMP (4)
        • Dijkstra (3)
        • Disjoints Set (4)
        • Bellman-Ford (2)
        • 시뮬레이션 (3)
        • 백트래킹 (15)
        • 위상정렬 (5)
        • 자료구조 (25)
        • 기하학 (1)
        • 정렬 (11)
        • 구현 (8)
        • 재귀 (8)
        • 수학 (8)
      • 유니티 공부 (11)
        • 레트로의 유니티 게임 프로그래밍 에센스 (11)
        • 유니티 스터디 자료 (0)
        • C# 공부 (0)
      • 유니티 프로젝트 (48)
        • 케이크게임 (13)
        • 점토게임 (35)
      • 언리얼 공부 (10)
        • 이득우의 언리얼 프로그래밍 (10)
      • 진로 (1)
      • 논문 읽기 (1)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    큐
    BFS
    스택
    바킹독
    유니티 프로젝트
    골드메탈
    dp
    레트로의 유니티 프로그래밍
    수학
    정렬
    티스토리챌린지
    유니티 공부 정리
    이벤트 트리거
    C#
    재귀
    백준
    그리디
    언리얼
    시뮬레이션
    우선순위큐
    이분탐색
    해시
    오블완
    맵
    이득우
    자료구조
    유니티
    그래프
    백트래킹
    투포인터
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
dubu0721
[백준] 3055번 탈출
상단으로

티스토리툴바