-
Notifications
You must be signed in to change notification settings - Fork 0
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
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this 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;
}
There was a problem hiding this 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;
}
}
}
๐ ๋ฌธ์ ๋งํฌ
์ด๊ฒ์ด ์ฝ๋ฉํ ์คํธ๋ค 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์ ์กฐํฉ์ผ๋ก ๋ง๋ค์ด์ฃผ๊ณ , ๊ฐ ๋ฐฉํฅ์ ๋ง๊ฒ ๋ช ๋ น์ด์ ๋ณํ๊ฐ์ ๋ง์ถฐ์ฃผ์ด ์ขํ์ ๋ํด์ค๋ค์ ๋ณํ๊ฐ์ ์กฐ๊ฑด์ ์ฒดํฌํ์ฌ ํต๊ณผํ๋ฉด ํ์ ์ขํ์ ๋ฐ์ํด์ฃผ๋ ๋ฐฉ์์ ๋๋ค.
๐ ์๋กญ๊ฒ ์๊ฒ๋ ๋ด์ฉ