본문 바로가기
BaekJoon/C++

11370 : Spawn of Ungoliant (C++)

by GrayChoi 2021. 2. 27.
반응형

 

11370번: Spawn of Ungoliant

The first line of an input case is of the form W H, where W is the width of the map and H is the height. The next H lines contain strings of length W specifying the layout of Mirkwood. An S character represents a spider-infested tree, and an T character re

www.acmicpc.net


쉬운 내용의 BFS라서 설명없이 그냥 코드만 올림

#include<iostream>
#include<queue>
#include<string>
#include<cstring>

using namespace std;

int W, H;

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

int main() {
    W = 1;
    H = 1;

    while(W != 0 && H != 0) {
        scanf("%d %d", &W, &H);
        char map[H][W];

        memset(map, '.', sizeof(map));

        queue<pair<int, int>> q;

        for(int x = 0; x < H; x++) {
            string temp;
            cin >> temp;
            for(int y = 0; y < W; y++) {
                map[x][y] = temp[y];
                if(map[x][y] == 'S') {
                    q.push({x, y});
                }
            }
        }

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

            for(int i = 0; i < 4; i++) {
                int nx = x + dx[i];
                int ny = y + dy[i];
                if(nx >= 0 && nx < H && ny >= 0 && ny < W) {
                    if(map[nx][ny] == 'T') {
                        q.push({nx, ny});
                        map[nx][ny] = 'S';
                    }
                }
            }

        }

        for(int x = 0; x < H; x++) {
            for(int y = 0; y < W; y++) {
                printf("%c", map[x][y]);
            }
            printf("\n");
        }


    }

    return 0;
}
반응형

'BaekJoon > C++' 카테고리의 다른 글

14940 : 쉬운 최단거리 (C++)  (0) 2021.03.02
11607 : Grid (C++)  (0) 2021.03.02
5378 : Hex (C++)  (0) 2021.02.27
6764 : Sounds fishy! (C++)  (0) 2021.02.26
6186 : Best Grass (C++)  (0) 2021.02.26

댓글