😎 κ³΅λΆ€ν•˜λŠ” μ§•μ§•μ•ŒνŒŒμΉ΄λŠ” μ²˜μŒμ΄μ§€?

[이것이 μ½”λ”© ν…ŒμŠ€νŠΈλ‹€ with Python]_14_κ΅¬ν˜„ λ³Έλ¬Έ

πŸ¦₯ μ½”ν…Œ/이것이 μ½”λ”© ν…ŒμŠ€νŠΈλ‹€ with python

[이것이 μ½”λ”© ν…ŒμŠ€νŠΈλ‹€ with Python]_14_κ΅¬ν˜„

μ§•μ§•μ•ŒνŒŒμΉ΄ 2022. 1. 31. 22:13
728x90
λ°˜μ‘ν˜•

220131 μž‘μ„±

<λ³Έ λΈ”λ‘œκ·ΈλŠ” γ€Žμ΄κ²ƒμ΄ 취업을 μœ„ν•œ μ½”λ”© ν…ŒμŠ€νŠΈλ‹€γ€ μ˜ youtubeλ₯Ό μ°Έκ³ ν•΄μ„œ κ³΅λΆ€ν•˜λ©° μž‘μ„±ν•˜μ˜€μŠ΅λ‹ˆλ‹€>

https://www.youtube.com/watch?v=puH2p1CQEg4&list=PLVsNizTWUw7H9_of5YCB0FmsSc-K44y81&index=14 

 

 

 

 

 

 

 

 

1. κ΅¬ν˜„ (Implementation)

: 머리속에 μžˆλŠ” μ•Œκ³ λ¦¬μ¦˜μ„ μ†ŒμŠ€μ½”λ“œλ‘œ λ°”κΎΈλŠ” κ³Όμ •

: problem -> thinking -> solution

: μ•Œκ³ λ¦¬μ¦˜ λ¬Έμ œμ—μ„œ 2차원 곡간은 ν–‰λ ¬ (matrix) 둜 μ‚¬μš©λ¨

: μ‹œλ¬Όλ ˆμ΄μ…˜ 및 μ™„μ „ 탐색 λ¬Έμ œμ—μ„œλŠ” 2차원 κ³΅κ°„μ—μ„œμ˜ λ°©ν–₯ 벑터 자주 ν™œμš©

 

 

 

< 문제1 > μƒν•˜μ’Œμš°

: N X M 크기의 μ •μ‚¬κ°ν˜•

: 1 X 1 크기의 μ •μ‚¬κ°ν˜•μœΌλ‘œ λ‚˜λˆ„μ–΄μ Έ 있음

: κ°€μž₯ μ™Όμͺ½ μœ„ μ’Œν‘œλŠ” (1, 1), κ°€μž₯ 였λ₯Έμͺ½ μ•„λž˜ μ’Œν‘œλŠ” (N, N)

: μ—¬ν–‰κ°€ AλŠ” 상, ν•˜, 쒌, 둜 이동 (μ‹œμž‘μ€ 1,1)

: μ—¬ν–‰κ°€κ°€ μ΅œμ •μ μœΌλ‘œ 도착할 μ§€μ μ˜ (X, Y)

: μ‹œλ¬Όλ ˆμ΄μ…˜ μœ ν˜•μ˜ 문제

- L : μ™Όμͺ½ ν•œμΉΈ

- R : 였λ₯Έμͺ½ ν•œμΉΈ

- U : μœ„λ‘œ ν•œμΉΈ

- D : μ•„λž˜λ‘œ ν•œμΉΈ

 

 

  • python
n = int(input())
x, y = 1, 1
plans = input().split()

dx = [0, 0, -1, -1]
dy = [-1, 1, 0, 0 ]
move_types = ['L', 'R', 'U', 'D']

for plan in plans :
    for i in range(len(move_types)) :
        if plan == move_types[i]:
            nx = x + dx[i]
            ny = y + dy[i]
        
    if nx < 1 or ny < 1 or nx > n or ny > n :
        continue
    x, y = nx, ny
print(x, y)
  • c++
#include <bits/stdc++.h>
using namespace std;

int n;
string plans;
int x = 1;
int y = 1;

int dx[4] = {0, 0, -1, 1};
int dy[4] = {-1, 1, 0, 0};
char moveTypes[4] = {'L', 'R', 'U', 'D'};

int main(void) {
	cin >> n;
    cin.ignore();
    getline(cin, plans);
    
    for (int i = 0; i<plans.size(); i ++) {
    	char plan = plans[i]
        int nx = -1;
        int ny = -1;
        
        for (int j = 0; j < 4; j ++ ) {
        	if (plan == moveTypes[j]) {
            	nx = x + dx[i];
                ny = y + dy[i];
            }
        }
        if (nx < 1 || ny < 1 || nx > n || ny > n)
        	continue;
        x = nx;
        y = ny;
    }
    cout << x << " " << y << " " << '\n'
    return 0;
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

728x90
λ°˜μ‘ν˜•
Comments