π 곡λΆνλ μ§μ§μνμΉ΄λ μ²μμ΄μ§?
[μ΄κ²μ΄ μ½λ© ν μ€νΈλ€ with Python]_14_ꡬν λ³Έλ¬Έ
[μ΄κ²μ΄ μ½λ© ν μ€νΈλ€ with Python]_14_ꡬν
μ§μ§μνμΉ΄ 2022. 1. 31. 22:13220131 μμ±
<λ³Έ λΈλ‘κ·Έλ γμ΄κ²μ΄ μ·¨μ μ μν μ½λ© ν μ€νΈλ€γ μ 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;
}