1일1알

백준 14271번 그리드 게임 C++ 본문

알고리즘

백준 14271번 그리드 게임 C++

영춘권의달인 2022. 10. 14. 11:08

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

 

14271번: 그리드 게임

첫째 줄에 처음 그리드의 행의 개수 N과 열의 개수 M이 주어진다. (1 ≤ N, M ≤ 50) 둘째 줄부터 N개의 줄에는 처음 그리드의 상태가 주어진다. 살아있는 칸은 'o'로, 죽어있는 칸은 '.'으로 주어진다

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>

using namespace std;
using int64 = long long;

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

const int ROW_OFFSET = 1500;
const int COL_OFFSET = 1500;
vector<vector<bool>> found(3100, vector<bool>(3100, false));

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

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

    int n, m, k;
    cin >> n >> m;
    int64 cnt = 0;
    queue<Info> q;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            char ch;
            cin >> ch;
            if (ch == 'o') {
                found[i + ROW_OFFSET][j + COL_OFFSET] = true;
                q.push({ i + ROW_OFFSET,j + COL_OFFSET,0 });
            }
        }
    }
    cin >> k;
    while (!q.empty()) {
        auto curr = q.front();
        q.pop();
        cnt++;
        if (curr.moveCnt >= k) continue;

        for (int i = 0; i < 4; i++) {
            int nextRow = curr.row + dRow[i];
            int nextCol = curr.col + dCol[i];
            if (found[nextRow][nextCol]) continue;
            found[nextRow][nextCol] = true;
            q.push({ nextRow,nextCol,curr.moveCnt + 1 });
        }
    }
    cout << cnt;
}