본문 바로가기
SWEA

[SWEA] 5650 핀볼 게임(모의 SW 역량테스트)

by 콩순이냉장고 2020. 9. 30.

문제 URL : swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AWXRF8s6ezEDFAUo

 

SW Expert Academy

SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!

swexpertacademy.com

문제 접근법 : 시뮬레이션 문제입니다. 

접근 1: 조건에 나와있는 그대로 움직여야하는 조건대로 움직이세요

접근 2: (1,1) ~ (N*N) * 4(모든방향)에 대해 전체 다 핀볼을 돌려보셔야합니다.

접근 3: MAP 사이즈 범위 바깥으로 나가더라도 움직이세요 움직인다음 현재위치가 바깥인지 확인하면 방향만 바꾸시면됩니다. (그래서 초기시작점 1,1로 잡음) 

접근 4: 만약 무한 루프가 돈다면 당신이 코드를 작성한겁니다. 무한루프 도는 테스트케이스를 주지않기때문에

 

소스코드 : 

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
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
 
using namespace std;
int n;
int map[102][102];
int dy[4= { -1010 };
int dx[4= { 010-1 };
vector<pair<intint>> hv[11];
 
bool isrange(int y, int x){
    if (1 <= y&&<= n && 1 <= x&&<= n)
        return true;
    return false;
}
struct pinball{
    int y, x, dir;
    pinball(int y, int x, int dir) :y(y), x(x), dir(dir){}
    pinball(){}
};
void nextmove(int y, int x, int dir,queue<pinball> &q,int &cnt){
    int ny = y + dy[dir];
    int nx = x + dx[dir];
    if (1 <= map[ny][nx] && map[ny][nx] <= 5){
        switch (map[ny][nx]){
        case 1:
            if (dir == 2)q.push({ ny, nx, dir - 1 });
            else if (dir == 3)q.push({ ny, nx, (dir + 1) % 4 });
            else q.push({ ny, nx, (dir + 2) % 4 });
            break;
        case 2:
            if (dir == 0) q.push({ ny, nx, (dir + 1) % 4 });
            else if (dir == 3) q.push({ ny, nx, dir - 1 });
            else q.push({ ny, nx, (dir + 2) % 4 });
            break;
        case 3:
            if (dir == 0) q.push({ ny, nx, 3 });
            else if (dir == 1) q.push({ ny, nx, (dir + 1) % 4 });
            else q.push({ ny, nx, (dir + 2) % 4 });
            break;
        case 4:
            if (dir == 1) q.push({ ny, nx, dir - 1 });
            else if (dir == 2) q.push({ ny, nx, (dir + 1) % 4 });
            else q.push({ ny, nx, (dir + 2) % 4 });
            break;
        case 5:
            q.push({ ny, nx, (dir + 2) % 4 });
            break;
        }
        cnt++;
    }
    else if (map[ny][nx] >= 6)
    {
        int idx = map[ny][nx];
        if (hv[idx][0].first == ny&&hv[idx][0].second == nx)
            q.push({ hv[idx][1].first, hv[idx][1].second, dir });
        else
            q.push({ hv[idx][0].first, hv[idx][0].second, dir });
    }
    else
    {
        q.push({ ny, nx, dir });
    }
}
int Move(int sy,int sx,int dir){
    queue<pinball> q;
    q.push({ sy, sx, dir });
    bool start = true;//한번도움직이지 않았는지 확인
    int cnt = 0;
    while (!q.empty()){
        int y = q.front().y;
        int x = q.front().x;
        int dir = q.front().dir;
        q.pop();
        if ((y == sy&&== sx&&start==false|| map[y][x] == -1)
            return cnt;
        start = false;//한번이라도 움직임
        if (!isrange(y, x))//맵 바깥이라면 벽을부딛치고 방향을 바꿔줌
        {
            nextmove(y,x,(dir+2)%4,q,cnt);
            cnt++;
            continue;
        }
        nextmove(y, x, dir, q, cnt);
    }
    return cnt;
}
void init(){
    for (int i = 6; i <= 10; i++)
        hv[i].clear();
}
void input(){
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= n; j++){
            cin >> map[i][j];
            if (6 <= map[i][j] && map[i][j] <= 10)
                hv[map[i][j]].push_back({ i, j });
        }
    }
}
int main(void)
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int te;
    cin >> te;
    for (int test = 1; test <= te; test++){
        memset(map, 0sizeof(map));
        init();
        input();
        int res = 0;
        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= n; j++){
                for (int k = 0; k < 4; k++){
                    if (map[i][j] == 0){
                    
                        int cnt = Move(i, j, k);
                        res = max(res, cnt);
                    }
                }
            }
        }
        cout << "#" << test << " " << res << "\n";
    }
}
 
 
cs

 

좀 노가다를 요구했던 문제였네요 아직 실력이 부족해서 벽돌을 만날때마다 

if else 가아닌 인덱스 방식으로 처리하고싶었는데 실력이 부족해서 노가다로 풀었네요 ㅠ.ㅠ

 

궁금한점 혹은 모르는점 혹은 논리적인 오류가 있다면 언제든 댓글을 이용해주시길 바랍니다.