BOJ : https://www.acmicpc.net/problem/1063

 

시뮬레이션 문제입니다

 

문자 각각 따로따로 구현하면 너무 코드가 길어지고 보기 안좋으니

간단하게 스트링 받아서 비교한 후 바로 사용할 수 있도록 하시면 편하게 해결할 수 있습니다.

 

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
#include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <algorithm>
#include <set>
#include <cmath>
#include <limits>
#include <cstring>
#include <string>
using namespace std;
 
// R L B T RT LT RB LB
int ml[8= { 1-1001-11-1 };
int mr[8= { 00-11 ,1 ,1-1-1 };
string arr[8= { "R","L","B","T","RT","LT","RB","LB" };
 
int main() {
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
 
    int tc;
    string str;
    pair<intint> k, d;
 
    getline(cin, str);
    
    k.first = str[0- 'A', k.second = str[1]-'0', d.first = str[3- 'A', d.second = str[4]-'0';
 
    if (str.size() == 7) tc = str[6]-'0';
    else tc = (str[6]-'0'* 10 + (str[7]-'0');
 
    while (tc--) {
        getline(cin, str);
        int check;
        int l = k.first, r = k.second;
        for (int i = 0; i < 8; i++) { if (str.compare(arr[i]) == 0) { check = i, l += ml[i], r += mr[i]; break; } }
 
        if (l >= 0 && l < 8 && r >= 1 && r <= 8) {
            if (!(l == d.first && r == d.second)) {
                k.first = l, k.second = r;
            }
            else {
                int df = d.first + ml[check];
                int ds = d.second + mr[check];
                if (df >= 0 && df < 8 && ds >= 1 && ds <= 8)
                    d.first = df, d.second = ds, k.first = l, k.second = r;
            }
        }
    }
 
    char lastk = k.first + 'A';
    char lastd = d.first + 'A';
    cout << lastk << k.second << endl;
    cout << lastd << d.second << endl;
 
    return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter

'algorithm > BOJ' 카테고리의 다른 글

BOJ 2699번 격자점 컨벡스헐  (0) 2020.04.07
BOJ 1708번 볼록 껍질  (0) 2020.04.07
BOJ 14499번 주사위 굴리기  (0) 2020.03.14
BOJ 14503번 로봇 청소기  (0) 2020.03.13
BOJ 3709번 레이저빔은 어디로  (0) 2020.03.13

BOJ : https://www.acmicpc.net/problem/14499

 

BOJ 14499번 주사위 굴리기 문제입니다.

 

처음에 인풋이 세로N, 가로M, 주사위가 놓인 위치 x y, 명령갯수 K가 주어지게 되는데,

N, M, x, y, K 순서 그대로 주어지는 건줄 알고 풀었다가 틀렸네요

N, M, y, x, K 순서로 보셔도 되고

N, M, x, y, K 순서로 보는 대신, N은 x축, M은 y축 으로 보셔도 되는 문제입니다.

 

게시판에 분명 이의제기가 있을 것 같아서 찾아 읽어보니,

백준님께서 x축, y축에 대한 언급이 없다는 설명이 맞는 말씀 같습니다.

헷갈릴수는 있지만, 문제없는 인풋입니다.

 

주사위는 문제에 각 위치를 전개도로 주었기 때문에, 이를 이용해서 동서남북에 대한 위치변경을 코드해주시면 되고

문제에서 요구하는대로 코드해주시면 문제없이 통과됩니다

 

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
#include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <algorithm>
#include <set>
#include <cmath>
#include <limits>
#include <cstring>
using namespace std;
 
struct direct {
    int y, x;
}direct[5];
 
int cube[7];
int arr[21][21];
int N, M, K;
queue<int> q;
 
void dfs(int y, int x, int cnt) {
    if (cnt == K) return;
 
    int yy, xx;
    int go;
    
    while (cnt<K) {
        cin >> go;
        cnt++;
        yy = y + direct[go].y;
        xx = x + direct[go].x;
        if (yy >= 0 && yy < N && xx >= 0 && xx < M) break;
        if (cnt == K) return;
    }
 
    if (go == 1) {
        int temp = cube[3];
        cube[3= cube[1];
        cube[1= cube[4];
        cube[4= cube[6];
        cube[6= temp;
    }
    if (go == 2) {
        int temp = cube[6];
        cube[6= cube[4];
        cube[4= cube[1];
        cube[1= cube[3];
        cube[3= temp;
    }
    if (go == 3) {
        int temp = cube[2];
        cube[2= cube[1];
        cube[1= cube[5];
        cube[5= cube[6];
        cube[6= temp;
    }
    if (go == 4) {
        int temp = cube[6];
        cube[6= cube[5];
        cube[5= cube[1];
        cube[1= cube[2];
        cube[2= temp;
    }
    if (arr[yy][xx] == 0) arr[yy][xx] = cube[6];
    else cube[6= arr[yy][xx], arr[yy][xx] = 0;
 
    cout << cube[1<< "\n";
    dfs(yy, xx, cnt);
}
 
int main() {
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
 
    direct[1= { 0,1 };
    direct[2= { 0,-1 };
    direct[3= { -1,0 };
    direct[4= { 1,0 };
 
    int y, x;
    cin >> N >> M >> y >> x >> K;
 
    for (int i = 0; i < N; i++)
        for (int j = 0; j < M; j++)
            cin >> arr[i][j];
 
    dfs(y, x, 0);
 
    return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter

'algorithm > BOJ' 카테고리의 다른 글

BOJ 1708번 볼록 껍질  (0) 2020.04.07
BOJ 1063번 킹  (0) 2020.04.07
BOJ 14503번 로봇 청소기  (0) 2020.03.13
BOJ 3709번 레이저빔은 어디로  (0) 2020.03.13
BOJ 1043번 거짓말  (0) 2020.03.13

BOJ : https://www.acmicpc.net/problem/14503

 

BOJ 14503번 로봇 청소기 문제입니다.

 

처음에는 이동가능한 곳 중에서 청소할 수 있는 모든 곳을 해야된다고 생각했습니다.

그래서 방문했던 곳(청소 한곳) 이여도 이 곳을 다시 방문하면 다른 방향에 청소를 할 수 있는 부분이 있을거라고

생각하고 문제를 풀었습니다.

 

문제를 잘못이해해서 예제출력부터 다르길래 다시 읽어봤습니다.

청소를 한 곳은 후진을 하지 않는 이상 이동을 하지 않는다는 조건이 있습니다...

 

딱히 어렵지 않으니 문제에서 요구하는대로 푸시면 될 것 같습니다.

 

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
#include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <algorithm>
#include <set>
#include <cmath>
#include <limits>
#include <cstring>
using namespace std;
 
struct direct {
    int y, x;
}direct[4];
 
int N, M;
int r, c, d;
bool arr[52][52];
int visit[52][52];
 
void dfs(int y, int x, int dt, int cnt) {
    visit[y][x] = 1;
    int go;
    bool check = false;
    for (int i = 1; i <= 4; i++) {
        go = (dt+i)%4;
        int yy = y + direct[go].y;
        int xx = x + direct[go].x;
        if (yy>=0 && yy<&& xx>=0 && xx<&& !arr[yy][xx] && !visit[yy][xx]) {
            check = true;
            dfs(yy, xx, go, cnt + 1);
            return;
        }
    }
 
    if (!check) {
        int yy = y;
        int xx = x;
        if (dt == 0) xx -= 1;
        if (dt == 1) yy++;
        if (dt == 2) xx++;
        if (dt == 3) yy--;
 
        if (yy >= 0 && yy < N && xx >= 0 && xx < M && !arr[yy][xx])
            dfs(yy, xx, dt, cnt);
        else {
            cout << cnt;
        }
    }
}
 
int main() {
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
 
    direct[0= { 0,1 };
    direct[1= { -1,0 };
    direct[2= { 0,-1 };
    direct[3= { 1,0 };
 
    cin >> N >> M;
    cin >> r >> c >> d;
 
    for (int i = 0; i < N; i++)
        for (int j = 0; j < M; j++)
            cin >> arr[i][j];
 
    int dt;
    if (d == 0) dt = 1;
    if (d == 1) dt = 0;
    if (d == 2) dt = 3;
    if (d == 3) dt = 2;
    dfs(r, c, dt, 1);
 
    return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter

 

'algorithm > BOJ' 카테고리의 다른 글

BOJ 1063번 킹  (0) 2020.04.07
BOJ 14499번 주사위 굴리기  (0) 2020.03.14
BOJ 3709번 레이저빔은 어디로  (0) 2020.03.13
BOJ 1043번 거짓말  (0) 2020.03.13
BOJ 1074번 Z  (0) 2020.03.05

BOJ : https://www.acmicpc.net/problem/3709

 

시뮬레이션 기본문제 입니다.

난이도는 높은편이 아니라서 문제에서 요구하는 대로 구현해주시면 됩니다.

설명이 필요할 것 같은 부분?은 주석처리했습니다.

 

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
#include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <algorithm>
#include <set>
#include <cmath>
#include <limits>
#include <cstring>
using namespace std;
 
struct direct {
    int y, x;
}direct[4];
 
bool arr[52][52];
int n, r;
 
void dfs(int y, int x, int dt) {
    if (!|| !|| y == n + 1 || x == n + 1) {
        cout << y << " " << x << '\n';
        return;
    }
 
    // 꺽이면
    if (arr[y][x]) {
        int go = (dt + 1) % 4;
        dfs(y + direct[go].y, x + direct[go].x, go);
    }
    else {
        dfs(y + direct[dt].y, x + direct[dt].x, dt);
    }
}
 
int main() {
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
 
    // 방향조절 (동->남->서->북)
    direct[0= { 0,1 };
    direct[1= { 1,0 };
    direct[2= { 0,-1 };
    direct[3= { -1,0 };
 
    int T; cin >> T;
    while (T--) {
        memset(arr, 0sizeof(arr));
        cin >> n >> r;
        for (int i = 0; i < r; i++) {
            int x, y;
            cin >> y >> x;
            arr[y][x] = true;
        }
 
        int sy, sx;
        cin >> sy >> sx;
        // 위 -> 아래
        if (sy == 0) dfs(sy + 1, sx, 1);
        // 아래 -> 위
        if (sy == n + 1) dfs(sy - 1, sx, 3);
        // 왼쪽 -> 오른쪽
        if (sx == 0) dfs(sy, sx + 10);
        // 오른쪽 -> 왼쪽
        if (sx == n + 1) dfs(sy, sx - 12);
    }
 
    return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
 

'algorithm > BOJ' 카테고리의 다른 글

BOJ 14499번 주사위 굴리기  (0) 2020.03.14
BOJ 14503번 로봇 청소기  (0) 2020.03.13
BOJ 1043번 거짓말  (0) 2020.03.13
BOJ 1074번 Z  (0) 2020.03.05
BOJ 1026번 보물  (0) 2020.02.28

+ Recent posts