๊ตฌํ˜„์ด๋ž€?

๋จธ๋ฆฟ์†์— ์žˆ๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์†Œ์Šค์ฝ”๋“œ๋กœ ๋ฐ”๊พธ๋Š” ๊ณผ์ •

 

์™„์ „ํƒ์ƒ‰ : ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ์ฃผ์ € ์—†์ด ๋‹ค ๊ณ„์‚ฐํ•˜๋Š” ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•

์‹œ๋ฎฌ๋ ˆ์ด์…˜ : ๋ฌธ์ œ์—์„œ ์ œ์‹œํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ํ•œ ๋‹จ๊ณ„์”ฉ ์ฐจ๋ก€๋Œ€๋กœ ์ง์ ‘ ์ˆ˜ํ–‰ํ•ด์•ผํ•˜๋Š” ๋ฌธ์ œ ์œ ํ˜•

์™„์ „ํƒ์ƒ‰, ์‹œ๋ฎฌ๋ ˆ์ด์…˜ ์œ ํ˜•์„ ๋ชจ๋‘ '๊ตฌํ˜„' ์œ ํ˜•์œผ๋กœ ๋ฌถ์–ด์„œ ๋‹ค๋ฃฌ๋‹ค.

 

 

[์˜ˆ์ œ 1] ์ƒํ•˜์ขŒ์šฐ

#include <iostream>
using namespace std;

//์™ผ ์˜ค ์œ„ ์•„๋ž˜
int dx[4] = {0, 0, -1, 1};
int dy[4] = {-1, 1, 0, 0};
char moveTypes[4] = {'L', 'R', 'U', 'D'};

int main() {
    int n, x = 1, y = 1;
    string plans;

    cin >> n;
    cin.ignore(); //๋ฒ„ํผ ์ง€์šฐ๊ธฐ getline ์ „์— ํ•ด์•ผํ•จ
    getline(cin, plans); //์—”ํ„ฐ๋ฅผ ๋ฒ„ํผ์— ๋‚จ๊น€

    for (char plan: plans) {
        if (plan == ' ') {
            continue;
        } else {
            int nx = 0, ny = 0;
            for (int j = 0; j < 4; j++) {
                if (plan == moveTypes[j]) {
                    nx = x + dx[j];
                    ny = y + dy[j];
                    break;
                }
            }
            if (nx < 1 || nx > n || ny < 1 || ny > n) {
                continue;
            } else {
                x = nx;
                y = ny;
            }
        }
    }
    cout << x << " " << y;
    return 0;
}

[์˜ˆ์ œ 2] ์‹œ๊ฐ

 

18312๋ฒˆ: ์‹œ๊ฐ

์ •์ˆ˜ N๊ณผ K๊ฐ€ ์ž…๋ ฅ๋˜์—ˆ์„ ๋•Œ 00์‹œ 00๋ถ„ 00์ดˆ๋ถ€ํ„ฐ N์‹œ 59๋ถ„ 59์ดˆ๊นŒ์ง€์˜ ๋ชจ๋“  ์‹œ๊ฐ ์ค‘์—์„œ K๊ฐ€ ํ•˜๋‚˜๋ผ๋„ ํฌํ•จ๋˜๋Š” ๋ชจ๋“  ์‹œ๊ฐ์„ ์„ธ๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค. ์‹œ๊ฐ์„ ์…€ ๋•Œ๋Š” ๋””์ง€ํ„ธ ์‹œ๊ณ„๋ฅผ ๊ธฐ์ค€์œผ๋กœ,

www.acmicpc.net

#include <iostream>
using namespace std;

int main() {
    int n, k, cnt = 0;
    cin >> n >> k;

    for (int i = 0; i <= n; i++) { //์‹œ
        for (int j = 0; j < 60; j++) { //๋ถ„
            for (int z = 0; z < 60; z++) { //์ดˆ
                if (i % 10 == k || i / 10 == k ||
                    j % 10 == k || j / 10 == k ||
                    z % 10 == k || z / 10 == k) {
                    cnt++;
                }
            }
        }
    }
    cout << cnt;
    return 0;
}

[์‹ค์ „ 1] ์™•์‹ค์˜ ๋‚˜์ดํŠธ

#include <iostream>
#include <string>
#include <vector>
using namespace std;

int dx[4] = {-1, 1, 0, 0}; //์œ„ ์•„๋ž˜ ์™ผ ์˜ค
int dy[4] = {0, 0, -1, 1};

vector<char> ch = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'};

bool isScope(int nx, int ny) {
    if (nx < 0 || nx > 7 || ny < 0 || ny > 7)
        return false;
    return true;
}

int main() {
    string s;
    int x, y, cnt = 0;
    cin >> s;

    x = s[1] - '0' - 1;
    for (int i = 0; i < ch.size(); i++) {
        if (s[0] == ch[i]) {
            y = i;
        }
    }

    for (int i = 0; i < 4; i++) { //4๋ฐฉํ–ฅ
        int nx = x, ny = y;
        for (int j = 0; j < 2; j++) { //๋‘๋ฒˆ๊ฐ
            nx += dx[i];
            ny += dy[i];
        }

        if (!isScope(nx, ny))
            continue;

        if (i > 1) { //์ˆ˜ํ‰์ผ ๋•Œ
            for (int z = 0; z < 2; z++) {
                nx += dx[z];
                ny += dy[z];

                if (isScope(nx, ny))
                    cnt++;
            }
        } else { //์ˆ˜์ง์ผ ๋•Œ
            for (int z = 2; z < 4; z++) {
                nx += dx[z];
                ny += dy[z];

                if (isScope(nx, ny))
                    cnt++;
            }
        }
    }

    cout << cnt;
    return 0;
}

์ง„์งœ ํ•˜๋“œ์ฝ”๋”ฉํ–ˆ๋Š”๋ฐ ... ๋ฐฉํ–ฅ๋งŒ ๋ฏธ๋ฆฌ์ •ํ•ด์ฃผ๋ฉด ๊ฐ„๋‹จํžˆ ํ’€๋ฆฌ๋Š” ๋ฌธ์ œ์˜€๋‹ค ใ… 

#include <iostream>
#include <vector>
using namespace std;

int dx[] = {-2, -1, 1, 2, 2, 1, -1, -2};
int dy[] = {-1, -2, -2, -1, 1, 2, 2, 1};
vector<char> ch = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'};

int main() {
    string s;
    int x, y, cnt = 0;
    cin >> s;

    x = s[1] - '0' - 1;
    for (int i = 0; i < ch.size(); i++) {
        if (s[0] == ch[i]) {
            y = i;
        }
    }

    for (int i = 0; i < 8; i++) {
        int nx = x + dx[i];
        int ny = y + dy[i];

        if (nx < 0 || nx > 7 || ny < 0 || ny > 7)
            continue;
        cnt++;
    }
    cout << cnt;
    return 0;
}

 

[์‹ค์ „ 2] ๊ฒŒ์ž„ ๊ฐœ๋ฐœ

#include <iostream>
using namespace std;

int n, m, x, y, direction, cnt = 1, turn_time = 0;
bool visit[50][50];
int map[50][50];

int dx[] = {-1, 0, 1, 0}; //๋ถ ์„œ ๋‚จ ๋™
int dy[] = {0, -1, 0, 1};

int main() {
    cin >> n >> m;
    cin >> x >> y >> direction;
    visit[x][y] = true;

    // ์ „์ฒด ๋งต ์ •๋ณด๋ฅผ ์ž…๋ ฅ ๋ฐ›๊ธฐ
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            cin >> map[i][j];
        }
    }

    while (true) {
        direction = (direction + 1) % 4;

        int nx = x + dx[direction];
        int ny = y + dy[direction];

        if (map[nx][ny] == 1 || visit[nx][ny]) {  // ํšŒ์ „ํ•œ ์ดํ›„ ์ •๋ฉด์— ๊ฐ€๋ณด์ง€ ์•Š์€ ์นธ์ด ์—†๊ฑฐ๋‚˜ ๋ฐ”๋‹ค์ธ ๊ฒฝ์šฐ
            turn_time += 1;
        } else { // ํšŒ์ „ํ•œ ์ดํ›„ ์ •๋ฉด์— ๊ฐ€๋ณด์ง€ ์•Š์€ ์นธ์ด ์กด์žฌํ•˜๋Š” ๊ฒฝ์šฐ ์ด๋™
            visit[nx][ny] = true;
            x = nx;
            y = ny;
            cnt += 1;
            turn_time = 0;
            continue;
        }

        // ๋„ค ๋ฐฉํ–ฅ ๋ชจ๋‘ ๊ฐˆ ์ˆ˜ ์—†๋Š” ๊ฒฝ์šฐ
        if (turn_time == 4) {
            nx = x - dx[direction];
            ny = y - dy[direction];

            // ๋’ค๋กœ ๊ฐˆ ์ˆ˜ ์žˆ๋‹ค๋ฉด ์ด๋™ํ•˜๊ธฐ
            if (map[nx][ny] == 0) {
                x = nx;
                y = ny;
            } else  // ๋’ค๊ฐ€ ๋ฐ”๋‹ค๋กœ ๋ง‰ํ˜€์žˆ๋Š” ๊ฒฝ์šฐ
                break;
            turn_time = 0;
        }
    }

    cout << cnt;
    return 0;
}

+ Recent posts