반응형

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

 

19591번: 독특한 계산기

숫자, '+', '*', '-', '/'로만 이루어진 길이가 106 이하인 수식이 주어진다. 계산 과정 중의 모든 수는 −263 이상 263 미만이며, 0으로 나누는 경우는 없다. 숫자 앞에 불필요한 0이 있을 수 있다. 

www.acmicpc.net

 

 

[ 문제풀이 ]

 

1. 문자열을 입력받을 str 변수를 선언하고, 입력을 받습니다.

 

2. num을 선언하고, 기호가 아닌 문자가 나오면 num = num * 10 + str[ i ] - '0'을 통해 num을 갱신해 줍니다.

 

3. 기호가 나온다면 deque<char> symbol에 기호를 push_back 해주고, 첫 번째 문자열이 '-'라면 isFirst를 true로 바꾸고, num의 값에 -1을 곱해준 후 deque<ll> number에 num을 push_back 해줍니다.

 

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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include<iostream>
#include<deque>
#include<string>
#define ll long long
 
using namespace std;
deque<ll> number;
deque<char> symbol;
ll ans;
 
ll cal(ll num1, ll num2, char sym)
{
    if (sym == '+') {
        return num1 + num2;
    }
    else if (sym == '-') {
        return num1 - num2;
    }
    else if (sym == '*') {
        return num1 * num2;
    }
    else {
        return num1 / num2;
    }
}
 
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
 
    string str;
 
    cin >> str;
 
    bool isFirst = false;
    ll num = 0;
 
    for (int i = 0; i < str.size(); i++) {
        if (i == 0 && str[i] == '-') {
            isFirst = true;
        }
        else if (str[i] >= '0') {
            num = num * 10 + str[i] - '0';
        }
        else {
            if (isFirst) {
                isFirst = false;
                num *= -1;
            }
            number.push_back(num);
            symbol.push_back(str[i]);
            num = 0;
        }
    }
    if (isFirst) {
        isFirst = false;
        num *= -1;
    }
    number.push_back(num);
 
    if (number.size() == 1) {
        cout << number.front();
        return 0;
    }
 
    while (1) {
        ll tmp[4= { 0 };
 
        if (number.size() == 3) {
            tmp[0= number.front();
            tmp[3= number.back();
            number.pop_back();
            number.pop_front();
            tmp[1= tmp[2= number.front();
        }
        else if (number.size() == 2) {
            tmp[0= number.front();
            tmp[1= number.back();
        }
        else {
            tmp[0= number.front();
            tmp[3= number.back();
            number.pop_back();
            number.pop_front();
            tmp[1= number.front();
            tmp[2= number.back();
        }
 
        if (symbol.size() == 1) {
            char sym = symbol.front();
            cout << cal(tmp[0], tmp[1], sym);
 
            return 0;
        }
        else {
            char sym1 = symbol.front();
            char sym2 = symbol.back();
 
            if (sym1 == '+' || sym1 == '-') {
                if (sym2 == '*' || sym2 == '/') {
                    symbol.pop_back();
                    number.pop_back();
                    number.push_back(cal(tmp[2], tmp[3], sym2));
                    number.push_front(tmp[0]);
                }
                else {
                    ll num1 = cal(tmp[0], tmp[1], sym1);
                    ll num2 = cal(tmp[2], tmp[3], sym2);
 
                    if (num1 >= num2) {
                        symbol.pop_front();
                        number.pop_front();
                        number.push_front(cal(tmp[0], tmp[1], sym1));
                        number.push_back(tmp[3]);
                    }
                    else {
                        symbol.pop_back();
                        number.pop_back();
                        number.push_back(cal(tmp[2], tmp[3], sym2));
                        number.push_front(tmp[0]);
                    }
                }
            }
            else if (sym1 == '*' || sym1 == '/') {
                if (sym2 == '+' || sym2 == '-') {
                    symbol.pop_front();
                    number.pop_front();
                    number.push_front(cal(tmp[0], tmp[1], sym1));
                    number.push_back(tmp[3]);
                }
                else {
                    ll num1 = cal(tmp[0], tmp[1], sym1);
                    ll num2 = cal(tmp[2], tmp[3], sym2);
 
                    if (num1 >= num2) {
                        symbol.pop_front();
                        number.pop_front();
                        number.push_front(cal(tmp[0], tmp[1], sym1));
                        number.push_back(tmp[3]);
                    }
                    else {
                        symbol.pop_back();
                        number.pop_back();
                        number.push_back(cal(tmp[2], tmp[3], sym2));
                        number.push_front(tmp[0]);
                    }
                }
            }
        }
    }
}
cs
반응형
반응형

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

 

17085번: 십자가 2개 놓기

첫째 줄에 격자판의 크기 N, M (2 ≤ N, M ≤ 15)이 주어진다. 둘째 줄부터 N개의 줄에 격자판의 상태가 주어진다. 항상 두 개의 십자가를 놓을 수 있는 경우만 입력으로 주어진다.

www.acmicpc.net

 

 

[ 문제풀이 ]

 

1. N, M을 입력받고, 격자판을 입력받습니다.

 

2. 십자가의 최대 크기가 7이므로 두 개의 십자가 크기가 7, 7부터 0, 0까지 격자판에 놓고, 두 십자가의 넓이의 곱의 최댓값을 ans에 저장합니다.

 

3. ans를 출력합니다.

 

[ 소스코드 ]

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
#include<iostream>
 
using namespace std;
 
int N, M;
char arr[15][16];
int temp[15][15];
int dy[] = { -1,1,0,0 };
int dx[] = { 0,0,-1,1 };
bool flag = false;
int visited[15][15];
int ans;
 
bool check(int y, int x, int n)
{
    for (int i = 0; i < 4; i++) {
        for (int j = 1; j <= n; j++) {
            int yy = y + dy[i] * j;
            int xx = x + dx[i] * j;
 
            if (arr[yy][xx] == '#' && visited[yy][xx] != 1) {
                visited[yy][xx] = 1;
            }
            else {
                return false;
            }
        }
    }
 
    return true;
}
 
void dfs(int a, int b, int cnt)
{
    if (flag) return;
    if (cnt == 2) {
        ans = max(ans, (1 + 4 * a) * (1 + 4 * b));
        flag = true;
        return;
    }
 
    int tmp;
 
    if (cnt == 0) tmp = a;
    else tmp = b;
 
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < M; j++) {
            temp[i][j] = visited[i][j];
        }
    }
 
    for (int i = tmp; i < N - tmp; i++) {
        for (int j = tmp; j < M - tmp; j++) {
            if (arr[i][j] == '#' && visited[i][j] != 1) {
                visited[i][j] = 1;
                if (check(i, j, tmp)) {
                    dfs(a, b, cnt + 1);
                }
                for (int i = 0; i < N; i++) {
                    for (int j = 0; j < M; j++) {
                        visited[i][j] = temp[i][j];
                    }
                }
            }
        }
    }
 
}
 
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
 
    cin >> N >> M;
 
    for (int i = 0; i < N; i++) {
        cin >> arr[i];
    }
 
    for (int i = 7; i >= 0; i--) {
        for (int j = i; j >= 0; j--) {
            flag = false;
            for (int i = 0; i < N; i++) {
                for (int j = 0; j < M; j++) {
                    visited[i][j] = 0;
                }
            }
            dfs(i, j, 0);
        }
    }
 
    cout << ans;
}
cs
반응형
반응형

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

 

20210번: 파일 탐색기

첫 줄에 문자열의 개수 N(2 ≤ N ≤ 10,000)이 주어진다. 그 다음 N줄에 정렬할 문자열이 한 줄에 하나씩 주어진다. 모든 문자열의 길이는 100 이하이며, 알파벳 대소문자와 숫자로만 이루어져 있다.

www.acmicpc.net

 

 

[ 문제풀이 ]

 

1. 문자열을 입력받고, arr 배열에 저장합니다.

 

2. cmp 함수를 만들어 문자열을 서로 비교하면서 두 문자열이 모두 숫자가 나온다면 0이 아닌 숫자가 나온 시점부터 string에 넣어줍니다.

 

3. 두 숫자가 같을 때 사이즈가 다르면 사이즈가 더 작은 수를 앞에 두고, 사이즈가 같다면 각 자리 숫자를 비교해 주면서 더 작은 숫자를 앞에 둡니다.

 

4. cmp함수를 통해 arr 배열을 정렬해주고, arr배열을 출력해 줍니다.

 

[ 소스코드 ]

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
#include<iostream>
#include<string>
#include<algorithm>
 
using namespace std;
 
int N;
string arr[10000];
 
bool cmp(string left, string right)
{
    if (left == right) return false;
 
    int l = 0, r = 0;
    while (1) {
        if (l == left.size()) {
            return true;
        }
        if (r == right.size()) {
            return false;
        }
 
        if (left[l] <= '9' && right[r] <= '9') {
            int sizeL = 0, sizeR = 0;
            string L, R;
            bool zeroL = false, zeroR = false;
            while (left[l] <= '9' && l < left.size()) {
                if (left[l] != '0' && zeroL == false) {
                    zeroL = true;
                }
                
                if (zeroL) {
                    L += left[l];
                }
 
                l++;
                sizeL++;
            }
 
            while (right[r] <= '9' && r < right.size()) {
                if (right[r] != '0' && zeroR == false) {
                    zeroR = true;
                }
 
                if (zeroR) {
                    R += right[r];
                }
                r++;
                sizeR++;
            }
 
            if (R == L) {
                if (sizeL != sizeR) {
                    return sizeL < sizeR;
                }
            }
            else {
                if (L.size() == R.size()) {
                    for (int i = 0; i < L.size(); i++) {
                        if (L[i] != R[i]) {
                            return L[i] < R[i];
                        }
                    }
                }
                return L.size() < R.size();
            }
            continue;
        }
        
        if (left[l] != right[r]) {
            if (left[l] >= 'a') {
                if (right[r] >= 'a') {
                    return left[l] < right[r];
                }
                else if (right[r] >= 'A') {
                    return left[l] - 'a' < right[r] - 'A';
                }
                else {
                    return false;
                }
            }
            else if (left[l] >= 'A') {
                if (right[r] >= 'a') {
                    return left[l] - 'A' <= right[r] - 'a';
                }
                else if (right[r] >= 'A') {
                    return left[l] < right[r];
                }
                else {
                    return false;
                }
            }
            else {
                if (right[r] >= 'A') {
                    return true;
                }
            }
        }
 
        l++;
        r++;
    }
}
 
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
 
    cin >> N;
 
    for (int i = 0; i < N; i++) {
        cin >> arr[i];
    }
 
    sort(arr, arr + N, cmp);
 
    for (int i = 0; i < N; i++) {
        cout << arr[i] << '\n';
    }
}
cs
반응형
반응형

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

 

2174번: 로봇 시뮬레이션

첫째 줄에 두 정수 A, B가 주어진다. 다음 줄에는 두 정수 N, M이 주어진다. 다음 N개의 줄에는 각 로봇의 초기 위치(x, y좌표 순) 및 방향이 주어진다. 다음 M개의 줄에는 각 명령이 명령을 내리는 순

www.acmicpc.net

 

 

[ 문제풀이 ]

 

1. 각 로봇의 위치와 방향을 입력받고, arr 배열에 저장하고, m 배열을 만들어 각 좌표에 로봇이 있다면 해당 로봇의 번호로 초기화합니다.

 

2. 방향 전환 입력을 받으면 방향을 바꾸어주고, F 입력을 받으면 한 칸씩 움직이면서 해당 좌표에 다른 로봇이 있다면 충돌했다고 출력해 주고, 지도의 범위 벗어나면 벽과 추돌했다고 출력합니다.

 

3. 만약 충돌 없이 모든 명령을 수행했다면 OK를 출력합니다.

 

[ 소스코드 ]

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
#include<iostream>
 
using namespace std;
 
struct robot {
    int y, x;
    int dir;
};
 
int A, B, N, M;
int m[101][101];
robot arr[101];
int dy[] = { 0,-1,0,1 };
int dx[] = { 1,0,-1,0 };
 
int move(int num, int re)
{
    int yy = arr[num].y;
    int xx = arr[num].x;
    int dir = arr[num].dir;
 
    m[yy][xx] = 0;
 
    for (int i = 0; i < re; i++) {
        yy += dy[dir];
        xx += dx[dir];
        if (yy > 0 && yy <= B && xx > 0 && xx <= A) {
            if (m[yy][xx] != num && m[yy][xx] != 0) {
                return m[yy][xx];
            }
        }
        else {
            return -1;
        }
    }
 
    m[yy][xx] = num;
    arr[num] = { yy,xx,dir };
 
    return 0;
}
 
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
 
    cin >> A >> B >> N >> M;
 
    for (int i = 1; i <= N; i++) {
        int x, y;
        char dir;
        cin >> x >> y >> dir;
        if (dir == 'E') {
            m[y][x] = i;
            arr[i] = { y,x,0 };
        }
        else if (dir == 'S') {
            m[y][x] = i;
            arr[i] = { y,x,1 };
        }
        else if (dir == 'W') {
            m[y][x] = i;
            arr[i] = { y,x,2 };
        }
        else {
            m[y][x] = i;
            arr[i] = { y,x,3 };
        }
    }
 
    while (M--) {
        int num, re;
        char cmd;
        cin >> num >> cmd >> re;
 
        if (cmd == 'F') {
            int n = move(num, re);
            if (n == -1) {
                cout << "Robot " << num << " crashes into the wall";
                return 0;
            }
            else if (n != 0) {
                cout << "Robot " << num << " crashes into robot " << n;
                return 0;
            }
        }
        else if (cmd == 'R') {
            arr[num].dir += re;
            arr[num].dir %= 4;
        }
        else {
            re %= 4;
            arr[num].dir -= re;
            arr[num].dir = (arr[num].dir + 4) % 4;
        }
    }
 
    cout << "OK";
}
cs
반응형
반응형

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

 

14719번: 빗물

첫 번째 줄에는 2차원 세계의 세로 길이 H과 2차원 세계의 가로 길이 W가 주어진다. (1 ≤ H, W ≤ 500) 두 번째 줄에는 블록이 쌓인 높이를 의미하는 0이상 H이하의 정수가 2차원 세계의 맨 왼쪽 위치

www.acmicpc.net

 

 

[ 문제풀이 ]

 

1. 블록의 높이를 입력받고 arr 배열에 저장합니다.

 

2. arr 배열을 for문을 통해 돌면서 왼쪽편의 블록의 최대 높이와 오른쪽 편의 블록의 최대 높이를 찾고, 두 블록의 높이 중 더 낮은 값에서 현재 위치의 블록의 높이 값을 뺀 값을 ans에 더합니다.

 

3. 2에서 현재 블록의 높이가 두 블록의 높이보다 클 경우 0을 더해줍니다.

 

4. ans를 출력합니다.  

 

[ 소스코드 ]

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
#include<iostream>
 
using namespace std;
 
int H, W;
int arr[500];
int ans;
 
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
 
    cin >> H >> W;
 
    for (int i = 0; i < W; i++) {
        cin >> arr[i];
    }
 
    for (int i = 1; i < W - 1; i++) {
        int left = 0, right = 0;
 
        for (int j = i - 1; j >= 0; j--) {
            left = max(left, arr[j]);
        }
 
        for (int j = i + 1; j < W; j++) {
            right = max(right, arr[j]);
        }
 
        ans += max(min(left, right) - arr[i], 0);
    }
 
    cout << ans;
}
cs
반응형
반응형

 

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

 

3967번: 매직 스타

매직 스타의 모양이 주어진다. 수가 채워져 있지 않은 곳은 x로, 채워져 있는 곳은 'A'부터 'L'까지 알파벳으로 채워져 있다. i번째 알파벳은 숫자 i를 의미한다. '.'는 매직 스타의 형태를 만들기

www.acmicpc.net

 

 

[ 문제풀이 ]

 

1. 매직 스타를 입력받고, 채워지지 않은 위치를 pos에 저장합니다.

 

2. 재귀함수를 이용하여 각 자리에 알파벳을 채워주고, 각 줄의 합이 282보다 크다면 return해줍니다.

 

3. 모든 자리가 다 찬다면 그대로 출력해줍니다.

 

[ 소스코드 ]

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
#include<iostream>
#include<string>
#include<vector>
 
using namespace std;
 
string str[5];
char arr[12];
int visited[12];
vector<int> pos;
bool flag = false;
 
void dfs(int level)
{
    if (flag) return;
    if (arr[1+ arr[2+ arr[3+ arr[4> 282return;
    if (arr[0+ arr[2+ arr[5+ arr[7> 282return;
    if (arr[0+ arr[3+ arr[6+ arr[10> 282return;
    if (arr[7+ arr[8+ arr[9+ arr[10> 282return;
    if (arr[1+ arr[5+ arr[8+ arr[11> 282return;
    if (arr[4+ arr[6+ arr[9+ arr[11> 282return;
 
    if (level == pos.size()) {
        int cnt = 0;
        flag = true;
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 9; j++) {
                if (str[i][j] != '.') {
                    cout << arr[cnt++];
                }
                else {
                    cout << str[i][j];
                }
            }
            cout << '\n';
        }
        return;
    }
 
    for (int i = 0; i < 12; i++) {
        if (visited[i] == 0) {
            visited[i] = 1;
            arr[pos[level]] = i + 'A';
            dfs(level + 1);
            visited[i] = 0;
            arr[pos[level]] = 0;
        }
    }
}
 
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
 
    for (int i = 0; i < 5; i++) {
        cin >> str[i];
    }
 
    int cnt = 0;
 
    for (int i = 0; i < 5; i++) {
        for (int j = 0; j < 9; j++) {
            if (str[i][j] != '.') {
                if (str[i][j] == 'x') {
                    pos.push_back(cnt++);
                }
                else {
                    visited[str[i][j] - 'A'= 1;
                    arr[cnt++= str[i][j];
                }
            }
        }
    }
 
    dfs(0);
}
cs
반응형
반응형

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

 

2116번: 주사위 쌓기

첫줄에는 주사위의 개수가 입력된다. 그 다음 줄부터는 한 줄에 하나씩 주사위의 종류가 1번 주사위부터 주사위 번호 순서대로 입력된다. 주사위의 종류는 각 면에 적혀진 숫자가 그림1에 있는

www.acmicpc.net

 

 

[ 문제풀이 ]

 

1. 모든 주사위를 입력받고, 첫 주사위의 바닥이 A일 때부터 F일 때까지 for문을 통해 돌면서 모든 경우의 수를 구합니다.

 

2. 주사위의 바닥이 정해지면 윗면이 정해지고, 두 면을 제외한 나머지 수 중 가장 큰 수를 Max에 저장하고, 쌓여 있는 모든 주사위의 옆면 중 Max 값을 ret에 더해줍니다.

 

3. ret 값중 가장 큰 값을 ans에 저장하고, 출력합니다.

 

[ 소스코드 ]

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
#include<iostream>
 
using namespace std;
 
int N;
int arr[10000][6];
int ans;
 
int getSum(int n)
{
    int temp = arr[0][n];
    int idx = n;
    int ret = 0;
    int Max = 0;
 
    for (int i = 0; i < N; i++) {
        Max = 0;
 
        for (int j = 0; j < 6; j++) {
            if (arr[i][j] == temp) {
                idx = j;
                break;
            }
        }
 
        if (idx == 0 || idx == 5) {
            for (int j = 0; j < 6; j++) {
                if (j != 0 && j != 5) {
                    Max = max(Max, arr[i][j]);
                }
            }
 
            if (idx == 0) {
                temp = arr[i][5];
            }
            else {
                temp = arr[i][0];
            }
        }
        else if (idx == 1 || idx == 3) {
            for (int j = 0; j < 6; j++) {
                if (j != 1 && j != 3) {
                    Max = max(Max, arr[i][j]);
                }
            }
 
            if (idx == 1) {
                temp = arr[i][3];
            }
            else {
                temp = arr[i][1];
            }
        }
        else {
            for (int j = 0; j < 6; j++) {
                if (j != 2 && j != 4) {
                    Max = max(Max, arr[i][j]);
                }
            }
 
            if (idx == 2) {
                temp = arr[i][4];
            }
            else {
                temp = arr[i][2];
            }
        }
 
        ret += Max;
    }
 
    return ret;
}
 
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
 
    cin >> N;
 
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < 6; j++) {
            cin >> arr[i][j];
        }
    }
 
    for (int i = 0; i < 6; i++) {
        ans = max(ans, getSum(i));
    }
 
    cout << ans;
}
cs
반응형
반응형

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

 

16509번: 장군

오랜만에 휴가를 나온 호근이는 문득 동아리방에 있는 장기가 하고 싶어졌다. 하지만 장기를 오랫동안 하지 않은 탓인지 예전에는 잘 쓰던 상을 제대로 쓰는 것이 너무 힘들었다. 호근이를 위해

www.acmicpc.net

 

 

[ 문제풀이 ]

 

1. R1, C1, R2, C2를 입력받고, R1, C1을 queue에 넣고 bfs를 실행합니다.

 

2. 만약 이동 경로 중간에 말이 있다면 움직이지 않고, 그렇지 않다면 움직입니다.

 

3. 만약 상이 왕의 위치로 가면 여태 움직인 횟수를 출력하고, 왕을 잡지 못한다면 -1을 출력합니다.

 

[ 소스코드 ]

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
#include<iostream>
#include<queue>
 
using namespace std;
 
int R1, C1, R2, C2;
int visited[10][9];
int dy[8][3= {
    -1,-1,-1,
    0,-1,-1,
    0,1,1,
    1,1,1,
    1,1,1,
    0,1,1,
    0,-1,-1,
    -1,-1,-1
};
int dx[8][3= {
    0,1,1,
    1,1,1,
    1,1,1,
    0,1,1,
    0,-1,-1,
    -1,-1,-1,
    -1,-1,-1,
    0,-1,-1
};
 
struct node {
    int y;
    int x;
    int cnt;
};
 
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
 
    cin >> R1 >> C1 >> R2 >> C2;
 
    queue<node> q;
 
    q.push({ R1,C1,0 });
    visited[R1][C1] = 1;
 
    while (!q.empty()) {
        const int y = q.front().y;
        const int x = q.front().x;
        const int cnt = q.front().cnt;
        q.pop();
 
        if (y == R2 && x == C2) {
            cout << cnt;
            return 0;
        }
 
        for (int i = 0; i < 8; i++) {
            int yy = y;
            int xx = x;
            bool flag = true;
            for (int j = 0; j < 3; j++) {
                yy += dy[i][j];
                xx += dx[i][j];
                if (yy == R2 && xx == C2 && j != 2) {
                    flag = false;
                }
            }
 
            if (yy >= 0 && yy < 10 && xx >= 0 && xx < 9 && flag) {
                if (visited[yy][xx] != 1) {
                    visited[yy][xx] = 1;
                    q.push({ yy,xx,cnt + 1 });
                }
            }
        }
    }
 
    cout << -1;
}
cs
반응형
반응형

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

 

10866번: 덱

첫째 줄에 주어지는 명령의 수 N (1 ≤ N ≤ 10,000)이 주어진다. 둘째 줄부터 N개의 줄에는 명령이 하나씩 주어진다. 주어지는 정수는 1보다 크거나 같고, 100,000보다 작거나 같다. 문제에 나와있지

www.acmicpc.net

 

 

 

[ 문제풀이 ]

 

1. deque 자료구조를 이용하여 입력받는 수와 동일한 메서드를 이용하여 조건에 맞는 동작을 수행합니다.

 

[ 소스코드 ]

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
#include<iostream>
#include<deque>
#include<string>
 
using namespace std;
 
int N;
deque<int> deq;
 
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
 
    cin >> N;
 
    for (int i = 0; i < N; i++) {
        string str;
        int num;
 
        cin >> str;
 
        if (str == "push_front") {
            cin >> num;
            deq.push_front(num);
        }
        else if (str == "push_back") {
            cin >> num;
            deq.push_back(num);
        }
        else if (str == "pop_front") {
            if (!deq.empty()) {
                cout << deq.front() << '\n';
                deq.pop_front();
            }
            else {
                cout << -1 << '\n';
            }
        }
        else if (str == "pop_back") {
            if (!deq.empty()) {
                cout << deq.back() << '\n';
                deq.pop_back();
            }
            else {
                cout << -1 << '\n';
            }
        }
        else if (str == "size") {
            cout << deq.size() << '\n';
        }
        else if (str == "empty") {
            if (!deq.empty()) {
                cout << 0 << '\n';
            }
            else {
                cout << 1 << '\n';
            }
        }
        else if (str == "front") {
            if (!deq.empty()) {
                cout << deq.front() << '\n';
            }
            else {
                cout << -1 << '\n';
            }
        }
        else if (str == "back") {
            if (!deq.empty()) {
                cout << deq.back() << '\n';
            }
            else {
                cout << -1 << '\n';
            }
        }
    }
}
cs
반응형
반응형

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

 

5430번: AC

각 테스트 케이스에 대해서, 입력으로 주어진 정수 배열에 함수를 수행한 결과를 출력한다. 만약, 에러가 발생한 경우에는 error를 출력한다.

www.acmicpc.net

 

 

[ 문제풀이 ]

 

1. 값들을 입력받고, 전처리를 통해서 배열의 값들을 deque에 넣어줍니다.

 

2. R이 입력되면 rev변수를 갱신해 줍니다.

 

3. rev변수의 값에 따라 pop_back, 혹은 pop_front를 해줍니다.

 

4. deq에 남아있는 수를 rev변수에 따라 출력해 줍니다.

 

[ 소스코드 ]

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
#include<iostream>
#include<deque>
#include<string>
 
using namespace std;
 
int T;
deque<int> deq;
 
void preproc(string x)
{
    int num = 0;
    for (auto next : x) {
        if (next >= '0' && next <= '9') {
            num *= 10;
            num += next - '0';
        }
        else if (next == ']' || next == ',') {
            deq.push_back(num);
            num = 0;
        }
    }
}
 
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
 
    cin >> T;
 
    while (T--) {
        bool flag = true;
        bool rev = false;
        deq.clear();
        string p, x;
        int n;
        cin >> p >> n >> x;
 
        if (n) preproc(x);
 
        for (auto com : p) {
            if (com == 'R') {
                rev = !rev;
            }
            else {
                if (!deq.empty()) {
                    if (rev) {
                        deq.pop_back();
                    }
                    else {
                        deq.pop_front();
                    }
                }
                else {
                    cout << "error\n";
                    flag = false;
                    break;
                }
            }
            
        }
 
        if (flag) {
            cout << '[';
            while (!deq.empty()) {
                if (rev) {
                    if (deq.size() == 1) {
                        cout << deq.back();
                    }
                    else {
                        cout << deq.back() << ',';
                    }
                    deq.pop_back();
                }
                else {
                    if (deq.size() == 1) {
                        cout << deq.front();
                    }
                    else {
                        cout << deq.front() << ',';
                    }
                    deq.pop_front();
                }
            }
            cout << "]\n";
        }
    }
}
cs
반응형

+ Recent posts