Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

15-wnsmir #65

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

15-wnsmir #65

wants to merge 1 commit into from

Conversation

wnsmir
Copy link
Collaborator

@wnsmir wnsmir commented Jan 26, 2025

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

์ด๊ฒƒ์ด ์ฝ”๋”ฉํ…Œ์ŠคํŠธ๋‹ค p.110 ๊ตฌํ˜„ 1๋ฒˆ ์ƒํ•˜์ขŒ์šฐ ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.

๋ฌธ์ œ
1,1์—์„œ ์‹œ์ž‘ํ•˜์—ฌ R, L, U, D๋ผ๋Š” ๋“ค์–ด์˜ค๋Š” ๋ช…๋ น์–ด์— ๋”ฐ๋ผ์„œ ๋ง์„ ์ด๋™์‹œ์ผœ์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.
R์€ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ํ•œ์นธ, L์™ผ์ชฝ์œผ๋กœ ๋“ฑ๋“ฑ ์ž…๋‹ˆ๋‹ค. ๋งต์‚ฌ์ด์ฆˆ๋Š” N*N์˜ ์ •์‚ฌ๊ฐํ˜• ๋ชจ์–‘์ด๋ฉฐ ์ด ๋งต์„ ๋„˜์–ด๊ฐ€๋ฉด ๋ช…๋ น์„ ์Šคํ‚ตํ•ฉ๋‹ˆ๋‹ค.
์ตœ์ข…์œ„์น˜๋ฅผ ์ขŒํ‘œ๋กœ ๋ฐ˜ํ™˜ํ•˜์‹œ์˜ค.

์ž…๋ ฅ์˜ˆ์‹œ
5 #๋งต์˜ ํฌ๊ธฐ N
R R R U D D

์ถœ๋ ฅ์˜ˆ์‹œ
4 3

โœ”๏ธ ์†Œ์š”๋œ ์‹œ๊ฐ„

20min

โœจ ์ˆ˜๋„ ์ฝ”๋“œ

PCCP๋Œ€๋น„ํ•˜๋ฉด์„œ ๊ตฌํ˜„์ชฝ์€ ์ฒ˜์Œ ํŽผ์ณ๋ดค๋Š”๋ฐ ์ฒ˜์Œ๋ถ€ํ„ฐ ๊ตฌํ˜„ํŒŒํŠธ๋Š” ๋‹ค ํ’€์–ด๋ณด๊ณ  ๋„˜์–ด๊ฐˆ๊ป„ ์‹ถ์—ˆ์Šต๋‹ˆ๋‹ค.
์‚ฌ์‹ค์ƒ ์ฝ”๋”ฉํ…Œ์ŠคํŠธ 4~5๋ฌธ์ œ์ค‘ 1, 2๋ฒˆ์€ ๋ฌด์กฐ๊ฑด ํ’€์–ด์•ผํ•˜๋Š” ๊ทธ๋ฆฌ๋”” ํ˜น์€ ๊ตฌํ˜„๋ฌธ์ œ์ธ๋ฐ ์ด๋ถ€๋ถ„์„ ๋„ˆ๋ฌด ํ—ˆ์ˆ ํ•˜๊ฒŒ ์งš๊ณ  ๋„˜์–ด๊ฐ”๋˜๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ํ”ํžˆ ๋งํ•˜๋Š” "ํ”ผ์ง€์ปฌ"์€ ์ด ๋‹จ์›์ธ๋ฐ ๋ง์ž…๋‹ˆ๋‹ค,,

ํ”ผ์ง€์ปฌ์„ ์ข€ ๋Š˜๋ ค๋ณด๊ณ ์ž ์•ž์œผ๋กœ pr 3๊ฐœ์ •๋„๋Š” ๊ตฌํ˜„๋ฌธ์ œ๋กœ ๋‹ค๋ค„๋ณด๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค.
์‰ฌ์šด๋ฌธ์žฌ๋“ค์ด๋‹ˆ ๊ณต๋ถ€ํ•˜๊ธฐ์ „ ์›Œ๋ฐ์—… ๋Š๋‚Œ์œผ๋กœ ๋”ฐ๋ผ์™€์ฃผ์„ธ์š”!

์ด๋ฌธ์ œ ํ˜น์€ ๊ทธ๋ž˜ํ”„๋ฌธ์ œ๋“ค์—์„œ ์ €๋Š” ๋ฐฉํ–ฅ์„ ์žก์„๋•Œ ๊ณ ์ •์ ์œผ๋กœ ์•„๋ž˜์™€๊ฐ™์ด dx, dy๋ฅผ ๊ฒน์น˜์ง€ ์•Š๊ฒŒ 0๊ณผ 1, -1์˜ ์กฐํ•ฉ์œผ๋กœ ๋งŒ๋“ค์–ด์ฃผ๊ณ , ๊ฐ ๋ฐฉํ–ฅ์— ๋งž๊ฒŒ ๋ช…๋ น์–ด์™€ ๋ณ€ํ™”๊ฐ’์„ ๋งž์ถฐ์ฃผ์–ด ์ขŒํ‘œ์— ๋”ํ•ด์ค€๋‹ค์Œ ๋ณ€ํ™”๊ฐ’์„ ์กฐ๊ฑด์— ์ฒดํฌํ•˜์—ฌ ํ†ต๊ณผํ•˜๋ฉด ํ˜„์ œ ์ขŒํ‘œ์— ๋ฐ˜์˜ํ•ด์ฃผ๋Š” ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค.

N = int(input())
orders = input().split()
dx = [0, 0, -1, 1]
dy = [-1, 1, 0, 0]
directions = ["U", "D", "L", "R"]

x, y = 1, 1

for order in orders:
    for i in range(4):
        if order == directions[i]:
            nx = x + dx[i]
            ny = y + dy[i]
    
    if nx < 1 or ny < 1 or nx > N or ny > N:
        continue
    else:
        x = nx
        y = ny

print(x, y)

๐Ÿ“š ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ๋œ ๋‚ด์šฉ

Copy link
Collaborator

@kangrae-jo kangrae-jo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๊ตฌํ˜„์€ ์‰ฌ์šฐ๋ฉด์„œ ์–ด๋ ค์šด ๊ฒƒ ๊ฐ™๋„ค์š”...
๊ทธ๋ฆฌ๊ณ  pccp๋ฌธ์ œ์ฒ˜๋Ÿผ ๋ณต์žกํ•œ ์กฐ๊ฑด์ด ์žˆ์œผ๋ฉด ๋ฉ˜ํƒˆ์ด ํ”๋“ค๋ฆฌ๋”๋ผ๊ตฌ์š”...

์ด๋ฒˆ๋ฌธ์ œ๋„ ์ž˜ ํ’€์—ˆ์Šต๋‹ˆ๋‹ค.
bfs์—์„œ ์ฃผ๋กœ ํ•˜๋˜๊ฑฐ๋ผ ์‰ฝ๊ฒŒ ๊ตฌํ˜„์„ ํ•  ์ˆ˜ ์žˆ์—ˆ์–ด์š”!
๋” ์–ด๋ ค์šด ๋ฌธ์ œ๋„ ๊ธฐ๋Œ€ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ท.

#include <iostream>
#include <sstream>
#include <vector>

using namespace std;

bool isMoveable(int x, int y, int N) {
    return 1 <= x && x <= N && 1 <= y && y <= N;
}

int main() {
    int N;
    cin >> N;

    string command;
    cin >> command;

    stringstream ss(command);
    char cmd;

    int x = 1, y = 1;
    while (ss >> cmd) {
        int x_ = 0, y_ = 0;
        if      (cmd == 'R') x_++;
        else if (cmd == 'L') x_--;
        else if (cmd == 'U') y_--;
        else if (cmd == 'D') y_++;

        if (isMoveable(x + x_, y + y_, N)) {
            x += x_;
            y += y_;
        }
    }
    cout << x << " " << y;

    return 0;
}

Copy link
Collaborator

@g0rnn g0rnn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ €๋Š” pccp ์ดํ›„์— ์ž๋ฐ”๋กœ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ํ‘ธ๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์•„์ด๋””์–ด์™€ ๋กœ์ง๋งŒ ์ค‘์š”ํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ๊ฐ€ ์•„๋‹ˆ๋ผ ์–ธ์–ด์— ๋งž๋Š” ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ์ ์ ˆํžˆ ํ™œ์šฉํ•˜์—ฌ ๋ฌธ์ œ๋ฅผ ํ‘ธ๋Š”๊ฒŒ ๋” ์ค‘์š”ํ•˜๋‹ค๊ณ  ์ƒ๊ฐ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

๊ทผ๋ฐ ์นœ์ˆ™ํ•˜๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ๋˜ ์ž๋ฐ”๋„ ๋ง‰์ƒ ํ•ด๋ณด๋‹ˆ ์ข€ ์–ด๋ ต๋„ค์š”. ๊ฐ„๋‹จํ•œ ๋ฌธ์ œ ๋•๋ถ„์— ๊ธฐ์ดˆ๋ถ€ํ„ฐ ์ž˜ ์Œ“๋Š” ๋Š๋‚Œ์ž…๋‹ˆ๋‹ค.

public class Solution15 {

    private static final int[] dx = {0, 1, 0, -1}; // ์ƒ, ์šฐ, ํ•˜, ์ขŒ
    private static final int[] dy = {-1, 0, 1, 0};
    private static int n;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        sc.nextLine();
        String line = sc.nextLine();
        String[] cmds = line.split(" ");
        Pair pos = new Pair(1, 1);

        for (String cmd : cmds) {
            if (cmd.equals("U")) {
                pos.move(dx[0], dy[0]);
            } else if (cmd.equals("R")) {
                pos.move(dx[1], dy[1]);
            } else if (cmd.equals("D")) {
                pos.move(dx[2], dy[2]);
            } else {
                pos.move(dx[3], dy[3]);
            }
        }
        System.out.println(pos.x + " " + pos.y);
        sc.close();
    }

    private static class Pair {
        public int x;
        public int y;

        public Pair(int x, int y) {
            this.x = x;
            this.y = y;
        }

        public void move(int dx, int dy) {
            if (1 > x + dx || x + dx > n || 1 > y + dy || y + dy > n) return;
            x += dx;
            y += dy;
        }
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants