1일1알

백준 19238번 스타트 택시 C++ 본문

알고리즘

백준 19238번 스타트 택시 C++

영춘권의달인 2022. 9. 16. 11:58

https://www.acmicpc.net/problem/19238

 

19238번: 스타트 택시

첫 줄에 N, M, 그리고 초기 연료의 양이 주어진다. (2 ≤ N ≤ 20, 1 ≤ M ≤ N2, 1 ≤ 초기 연료 ≤ 500,000) 연료는 무한히 많이 담을 수 있기 때문에, 초기 연료의 양을 넘어서 충전될 수도 있다. 다

www.acmicpc.net

 

bfs인데 구현 난이도가 쉽지 않았다.

 

#include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <algorithm>
#include <utility>
#include <stack>
#include <queue>
#include <math.h>
#include <set>
#include <map>
#include <list>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <limits.h>

#include <bitset>

using namespace std;
using int64 = long long;

int dRow[4] = { -1,0,1,0 };
int dCol[4] = { 0,1,0,-1 };

int n, m, f;

struct Info {
    int row;
    int col;
    int useF;
};

vector<vector<int>> board;
vector<vector<bool>> found;

map<pair<int, int>, pair<int, int>> mp;

void RefReshFound() {
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            found[i][j] = false;
        }
    }
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
	cout.tie(NULL);

    cin >> n >> m >> f;

    board = vector<vector<int>>(n + 1, vector<int>(n + 1));
    found = vector<vector<bool>>(n + 1, vector<bool>(n + 1, false));

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            cin >> board[i][j];
        }
    }

    int startRow, startCol;
    cin >> startRow >> startCol;

    for (int i = 0; i < m; i++) {
        int sR, sC, eR, eC;
        cin >> sR >> sC >> eR >> eC;
        board[sR][sC] = 2;
        mp.insert({ {sR,sC},{eR,eC} });
    }

    while (true) {
        queue<Info> q;

        q.push({ startRow,startCol,0 });
        found[startRow][startCol] = true;

        Info foundPerson{ -1,-1,-1 };
        set<pair<int, int>> persons;

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

            if (board[curr.row][curr.col] == 2) {
                persons.insert({ curr.row,curr.col });
                foundPerson = curr;
                break;
            }

            for (int i = 0; i < 4; i++) {
                int nextRow = curr.row + dRow[i];
                int nextCol = curr.col + dCol[i];
                if (nextRow<1 || nextRow>n) continue;
                if (nextCol<1 || nextCol>n) continue;
                if (found[nextRow][nextCol]) continue;
                if (board[nextRow][nextCol] == 1) continue;
                found[nextRow][nextCol] = true;
                q.push({ nextRow,nextCol,curr.useF + 1 });
            }
        }

        if (foundPerson.row == -1) {
            f = -1;
            break;
        }

        if (foundPerson.useF >= f) {
            f = -1;
            break;
        }

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

            if (curr.useF > foundPerson.useF) break;

            if (board[curr.row][curr.col] == 2) {
                persons.insert({ curr.row,curr.col });
            }
        }

        while (!q.empty()) q.pop();

        startRow = persons.begin()->first;
        startCol = persons.begin()->second;

        f -= foundPerson.useF;
        auto it = mp.find({ startRow,startCol });

        pair<int, int> target = it->second;
        mp.erase(it);

        RefReshFound();
        board[startRow][startCol] = 0;
        found[startRow][startCol] = true;
        q.push({ startRow,startCol,0 });

        int useF = -1;

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

            if (curr.row == target.first && curr.col == target.second) {
                useF = curr.useF;
                break;
            }

            for (int i = 0; i < 4; i++) {
                int nextRow = curr.row + dRow[i];
                int nextCol = curr.col + dCol[i];
                if (nextRow<1 || nextRow>n) continue;
                if (nextCol<1 || nextCol>n) continue;
                if (found[nextRow][nextCol]) continue;
                if (board[nextRow][nextCol] == 1) continue;
                found[nextRow][nextCol] = true;
                q.push({ nextRow,nextCol,curr.useF + 1 });
            }
        }

        if (useF == -1) {
            f = -1;
            break;
        }

        if (useF > f) {
            f = -1;
            break;
        }

        f += useF;

        startRow = target.first;
        startCol = target.second;

        if (mp.empty()) break;
        RefReshFound();
    }
    cout << f;
}

'알고리즘' 카테고리의 다른 글

백준 17135번 캐슬 디펜스 C++  (0) 2022.09.18
백준 25307번 시루의 백화점 구경 C++  (0) 2022.09.17
백준 3273번 두 수의 합 C++  (0) 2022.09.15
백준 2437번 저울 C++  (1) 2022.09.14
백준 1939번 중량제한 C++  (0) 2022.09.13