SWExpert Academy의 핀볼게임(5650) 문제이다.


[ 문제풀이 ]

1) 도형을 만났을 때의 방향전환과 웜홀을 만났을 때의 위치변환을 잘해줘야 하는 문제이다.

    먼저 웜홀을 어떻게 관리할지 부터 알아보도록 하자.

    먼저 본인은 WarmHole배열을 만들어주었다. 자료형은 pair<pair<int,int>, pair<int,int>> 로 만들어 주었다.

    WarmHole은 2개로 이루어져있기 때문에 그 2개의 좌표들을 앞에쌍과 뒤에쌍에 각각 저장해주었다.

    (본문 : pair<pair<int,int>, pair<int,int>> WarmHole[5])

    웜홀을 만나게 되면 간단하다. 먼저 하나의 웜홀이 가지고 있는 2개의 좌표 중에 현재 내가 만난 웜홀이 무슨 좌표인지만

    확인을 해주면 된다.

    이 후, 그 나머지 좌표로 나를 옮겨주기만 하면 끝 !

   

    별다른 설명보다는 소스코드를 참고하는 것이 더 도움이 될 듯하다.


[ 소스코드 ]

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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include<iostream>
#include<vector>
 
#define endl "\n"
#define MAX 100
using namespace std;
 
int Answer;
int N;
int MAP[MAX][MAX];
bool Already_Input[5];
pair<pair<intint>pair<intint>> WarmHole[5];
 
vector<pair<intint>> V;
 
int dx[] = { 001-1 };
int dy[] = { 1-100 };
int Start_X, Start_Y;
 
int Bigger(int A, int B) { if (A > B) return A; return B; }
 
void Initialize()
{
    Answer = 0;
    for (int i = 0; i < 5; i++)
    {
        WarmHole[i].first.first = -1;
        WarmHole[i].first.second = -1;
        WarmHole[i].second.first = -1;
        WarmHole[i].second.second = -1;
        Already_Input[i] = false;
    }
    V.clear();
}
 
void Input()
{
    cin >> N;
    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < N; j++)
        {
            cin >> MAP[i][j];
            if (MAP[i][j] == 0)
            {
                V.push_back(make_pair(i, j));
            }
            else if (6 <= MAP[i][j] && MAP[i][j] <= 10)
            {
                if (Already_Input[MAP[i][j] - 6== false)
                {
                    WarmHole[MAP[i][j] - 6].first.first = i;
                    WarmHole[MAP[i][j] - 6].first.second = j;
                    Already_Input[MAP[i][j] - 6= true;
                }
                else
                {
                    WarmHole[MAP[i][j] - 6].second.first = i;
                    WarmHole[MAP[i][j] - 6].second.second = j;
                }
            }
        }
    }
}
 
int Change_Direction(int Cur_d, int Shape)
{
    int nd;
    if (Cur_d == 0)
    {
        if (Shape == 1 || Shape == 2 || Shape == 5) nd = 1;
        else if (Shape == 3) nd = 2;
        else if (Shape == 4) nd = 3;        
    }
    else if (Cur_d == 1)
    {
        if (Shape == 3 || Shape == 4 || Shape == 5) nd = 0;
        else if (Shape == 1) nd = 3;
        else if (Shape == 2) nd = 2;
    }
    else if (Cur_d == 2)
    {
        if (Shape == 2 || Shape == 3 || Shape == 5) nd = 3;
        else if (Shape == 1) nd = 0;
        else if (Shape == 4) nd = 1;
    }
    else if (Cur_d == 3)
    {
        if (Shape == 1 || Shape == 4 || Shape == 5) nd = nd = 2;
        else if (Shape == 2) nd = 0;
        else if (Shape == 3) nd = 1;
    }
    return nd;
}
 
int Move(int x, int y, int d)
{
    int Score = 0;
    while (1)
    {
        int nx = x + dx[d];
        int ny = y + dy[d];
        int nd = d;
 
        if (nx >= 0 && ny >= 0 && nx < N && ny < N)
        {
            if (MAP[nx][ny] == -1 || (nx == Start_X && ny == Start_Y)) break;
            else if (1 <= MAP[nx][ny] && MAP[nx][ny] <= 5)
            {
                nd = Change_Direction(d, MAP[nx][ny]);
                Score++;
            }
            else if (6 <= MAP[nx][ny] && MAP[nx][ny] <= 10)
            {
                if (nx == WarmHole[MAP[nx][ny] - 6].first.first && ny == WarmHole[MAP[nx][ny] - 6].first.second)
                {
                    int tx = WarmHole[MAP[nx][ny] - 6].second.first;
                    int ty = WarmHole[MAP[nx][ny] - 6].second.second;
                    nx = tx;
                    ny = ty;
                }
                else
                {
                    int tx = WarmHole[MAP[nx][ny] - 6].first.first;
                    int ty = WarmHole[MAP[nx][ny] - 6].first.second;
                    nx = tx;
                    ny = ty;
                }
            }
        }
        else
        {
            if (d == 0) nd = 1;
            else if (d == 1) nd = 0;
            else if (d == 2) nd = 3;
            else if (d == 3) nd = 2;
            Score++;
        }
        x = nx;
        y = ny;
        d = nd;
    }
    return Score;
}
 
void Solution()
{
    for (int i = 0; i < V.size(); i++)
    {
        int x = V[i].first;
        int y = V[i].second;
        Start_X = x;
        Start_Y = y;
        
        for (int j = 0; j < 4; j++)
        {
            int Temp_Answer = Move(x, y, j);
            Answer = Bigger(Answer, Temp_Answer);
        }
    }
}
 
void Solve()
{
    int Tc; cin >> Tc;
    for (int T = 1; T <= Tc; T++)
    {
        Initialize();
        Input();
        Solution();
 
        cout << "#" << T << " " << Answer << endl;
    }
}
 
int main(void)
{
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
 
    //freopen("Input.txt", "r", stdin);
    Solve();
 
    return 0;
}
cs


+ Recent posts