반응형

문제 출처 :


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



알고리즘 분석 :


문제 해결에 필요한 사항

1. 스택

2. 벡터


이 문제는 삼x 상반기 공채 기출 문제이다.


문제를 처음보면 난감하지만 자세히 보면 그냥 스택의 반복이다.


스택 STL을 이용하지 않고 벡터를 이용하면 문제를 쉽게 해결 할 수 있다.


벡터에는 방향을 담는다 생각하고 벡터에 담긴 역순으로 방향에 대해 설정해주면 된다.


이때 문제를 한번 생각해보면 mod 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
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
 
using namespace std;
 
int arr[102][102];
 
int main()
{
    int n;
    scanf("%d"&n);
 
    for (int i = 0; i < n; i++)
    {
        int sx, sy, dir, gen;
        scanf("%d %d %d %d"&sx, &sy, &dir, &gen);
 
        vector<int> vc;
        vc.push_back(dir);
 
        for (int j = 1; j <= gen; j++)
        {
            int len = vc.size();
            for (int k = len - 1; k >= 0; k--)
                vc.push_back((vc[k] + 1) % 4);
        }
 
        arr[sy][sx] = 1;
        for (int j = 0; j < vc.size(); j++)
        {
            int dir = vc[j];
            if (dir == 0)
                arr[sy][++sx] = 1;
            else if (dir == 1)
                arr[--sy][sx] = 1;
            else if (dir == 2)
                arr[sy][--sx] = 1;
            else if (dir == 3)
                arr[++sy][sx] = 1;
        }
    }
 
    int ans = 0;
    for (int i = 0; i <= 99; i++)
        for (int j = 0; j <= 99; j++)
            if (arr[i][j] && arr[i + 1][j] && arr[i][j + 1&& arr[i + 1][j + 1])
                ans++;
    printf("%d\n", ans);
    return 0;
}
 
//                                                       This source code Copyright belongs to Crocus
//                                                        If you want to see more? click here >>
Crocus


반응형

'Applied > 알고리즘 문제풀이' 카테고리의 다른 글

[11008번] 복붙의 달인  (0) 2018.05.01
[15686번] 치킨 배달  (0) 2018.04.30
[11663번] 선분 위의 점  (0) 2018.04.19
[2567번] 색종이1  (0) 2018.04.16
[14889번] 스타트와 링크  (0) 2018.04.14