백준 4577 소코반
본문 바로가기
알고리즘 모음집/알고리즘 (Algorithm)

백준 4577 소코반

by KyeongMin 2019. 9. 30.
728x90
반응형

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include<stdio.h>
#include<string>
#include<iostream>
#include<vector>
#include<string.h>
#define NSIZE 16
#define MSIZE 16
#define CSIZE 50
using namespace std;
int N, M;
char map[NSIZE][MSIZE];
struct Data {
    int y, x;
};
vector<Data>v;
string C;
int dy[] = { -1,1,0,0 };//위 아래 왼 오
int dx[] = { 0,0,-1,1 };
int y, x,dir;
int flag;
void init() {
    v.clear();
    memset(map, 0sizeof(map));
    C.clear();
    scanf("%d %d"&N, &M);
    if (N == 0 && M == 0return;
    for (int i = 0; i < N; i++) {
        scanf(" %s",map[i]);
    }
    cin >> C;
}
bool finishChk() {
    int cnt = 0;
    for (int i = 0; i < v.size(); i++) {
        if (map[v[i].y][v[i].x] == 'B')cnt++;
    }
    if (cnt == v.size()) {
        flag = 1;
        return true;
    }
    else return false;
}
void player() {
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < M; j++) {
            if (map[i][j] == 'w' || map[i][j] == 'W') {
                y = i; x = j; 
            }
            if (map[i][j] == 'W' || map[i][j] == 'B' || map[i][j] == '+') {
                v.push_back({ i,j });
            }
        }
    }
}
void move() {
 
}
void playGame() {
    player();
 
    for (int i = 0; i < C.size(); i++) {
        if (C[i] == 'U') {
            dir = 0;
        }
        //////////////////////////////////
        else if (C[i] == 'D') {
            dir = 1;
        }
        //////////////////////////////////
        else if (C[i] == 'L') {
            dir = 2;
        }
        ///////////////////////////////////
        else if (C[i] == 'R') {
            dir = 3;
        }
        int ny = y + dy[dir]; int nx = x + dx[dir];
        if (map[ny][nx] == 'b' || map[ny][nx] == 'B') {//box 확인
            if (map[ny][nx] == 'b') {
                int nny = ny + dy[dir]; int nnx = nx + dx[dir];
                if (map[nny][nnx] == '.') {
                    if (map[y][x] == 'w') map[y][x] = '.';
                    if (map[y][x] == 'W')map[y][x] = '+';
                    map[ny][nx] = 'w';
                    map[nny][nnx] = 'b';
                    y = ny; x = nx;
                }
                else if (map[nny][nnx] == '+') {
                    if (map[y][x] == 'w') map[y][x] = '.';
                    if (map[y][x] == 'W')map[y][x] = '+';
                    map[ny][nx] = 'w';
                    map[nny][nnx] = 'B';
                    y = ny; x = nx;
                }
            }
            if (map[ny][nx] == 'B') {
                int nny = ny + dy[dir]; int nnx = nx + dx[dir];
                if (map[nny][nnx] == '.') {
                    if (map[y][x] == 'w') map[y][x] = '.';
                    if (map[y][x] == 'W')map[y][x] = '+';
                    map[ny][nx] = 'W';
                    map[nny][nnx] = 'b';
                    y = ny; x = nx;
                }
                else if (map[nny][nnx] == '+') {
                    if (map[y][x] == 'w') map[y][x] = '.';
                    if (map[y][x] == 'W')map[y][x] = '+';
                    map[ny][nx] = 'W';
                    map[nny][nnx] = 'B';
                    y = ny; x = nx;
                }
            }
        }
        if (map[ny][nx] != '#') {
            if (map[ny][nx] == '.') {
                if (map[y][x] == 'w') map[y][x] = '.';
                if (map[y][x] == 'W')map[y][x] = '+';
                map[ny][nx] = 'w';
                y = ny; x = nx;
            }
            if (map[ny][nx] == '+') {
                if (map[y][x] == 'w') map[y][x] = '.';
                if (map[y][x] == 'W')map[y][x] = '+';
                map[ny][nx] = 'W';
                y = ny; x = nx;
            }
        }
        ///////////////////////////////////
        if (finishChk())break;
    }
}
void print() {
    for (int i = 0; i < N; i++) {
        printf("%s\n", map[i]);
    }
}
int main(void) {
    int cnt = 0;
    while (1) {
        ++cnt;
        flag = 0;
        init();
        if (N == 0 && M == 0)break;
 
        playGame();
        printf("Game %d: ", cnt);
        if (flag == 0)cout << "incomplete"<<endl;
        else cout << "complete" << endl;
        print();
    }
    return 0;
}
 
 

이문제는 정말 단순한 시뮬레이션문제인데 예전 스마트폰 나오기전에 이런 게임 많이 하지 않았나요?

저는 좀 많이 해본것 같은데  어찌됬든 정해진 위치로 벽돌을 움직이면 되는것으로 대신 2칸 앞까지 생각을 해야한다가

포인트 같습니다. 물론 대문자 소문자일때 그위치를 떠나면 + 가 남나 . 이 남는지 생각도 해야하지만 이런것은

단순히 조건만 잘주면 되니 따로 설명은 진행하지 않겠습니다. 이동 방향만 다를뿐 하는 행동은 같기때문에 

 

어렵지는 않았어도 구현할때 조금 힘들수 있는 문제 였습니다. 

오늘은  그림 설명없이 소스로 대체 하겠습니다. 여러분 모든 오늘 하루도 즐거운 코딩 !!!

 

728x90
반응형

'알고리즘 모음집 > 알고리즘 (Algorithm)' 카테고리의 다른 글

백준 2931 가스관  (0) 2019.10.02
백준 16137 견우와 직녀  (0) 2019.09.30
백준 1260 DFS와 BFS  (0) 2019.09.29
백준 15662 톱니바퀴(2)  (0) 2019.09.29
백준 1194 달이 차오른다, 가자.  (0) 2019.09.25

댓글