Skip to content

Commit e44fe3b

Browse files
committed
Update
1 parent 79cf1e5 commit e44fe3b

File tree

8 files changed

+349
-0
lines changed

8 files changed

+349
-0
lines changed

4/1.cpp

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
// N을 입력받기
6+
int n;
7+
string plans;
8+
int x = 1, y = 1;
9+
10+
// L, R, U, D에 따른 이동 방향
11+
int dx[4] = {0, 0, -1, 1};
12+
int dy[4] = {-1, 1, 0, 0};
13+
char moveTypes[4] = {'L', 'R', 'U', 'D'};
14+
15+
int main(void) {
16+
cin >> n;
17+
cin.ignore(); // 버퍼 비우기
18+
getline(cin, plans);
19+
// 이동 계획을 하나씩 확인
20+
for (int i = 0; i < plans.size(); i++) {
21+
char plan = plans[i];
22+
// 이동 후 좌표 구하기
23+
int nx = -1, ny = -1;
24+
for (int j = 0; j < 4; j++) {
25+
if (plan == moveTypes[j]) {
26+
nx = x + dx[j];
27+
ny = y + dy[j];
28+
}
29+
}
30+
// 공간을 벗어나는 경우 무시
31+
if (nx < 1 || ny < 1 || nx > n || ny > n) continue;
32+
// 이동 수행
33+
x = nx;
34+
y = ny;
35+
}
36+
cout << x << ' ' << y << '\n';
37+
return 0;
38+
}

4/1.java

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import java.util.*;
2+
3+
public class Main {
4+
5+
public static void main(String[] args) {
6+
Scanner sc = new Scanner(System.in);
7+
8+
// N을 입력받기
9+
int n = sc.nextInt();
10+
sc.nextLine(); // 버퍼 비우기
11+
String[] plans = sc.nextLine().split(" ");
12+
int x = 1, y = 1;
13+
14+
// L, R, U, D에 따른 이동 방향
15+
int[] dx = {0, 0, -1, 1};
16+
int[] dy = {-1, 1, 0, 0};
17+
char[] moveTypes = {'L', 'R', 'U', 'D'};
18+
19+
// 이동 계획을 하나씩 확인
20+
for (int i = 0; i < plans.length; i++) {
21+
char plan = plans[i].charAt(0);
22+
// 이동 후 좌표 구하기
23+
int nx = -1, ny = -1;
24+
for (int j = 0; j < 4; j++) {
25+
if (plan == moveTypes[j]) {
26+
nx = x + dx[j];
27+
ny = y + dy[j];
28+
}
29+
}
30+
// 공간을 벗어나는 경우 무시
31+
if (nx < 1 || ny < 1 || nx > n || ny > n) continue;
32+
// 이동 수행
33+
x = nx;
34+
y = ny;
35+
}
36+
37+
System.out.println(x + " " + y);
38+
}
39+
40+
}

4/2.cpp

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
int h, cnt;
6+
7+
// 특정한 시각 안에 '3'이 포함되어 있는지의 여부
8+
bool check(int h, int m, int s) {
9+
if (h % 10 == 3 || m / 10 == 3 || m % 10 == 3 || s / 10 == 3 || s % 10 == 3)
10+
return true;
11+
return false;
12+
}
13+
14+
int main(void) {
15+
// H를 입력받기
16+
cin >> h;
17+
for (int i = 0; i <= h; i++) {
18+
for (int j = 0; j < 60; j++) {
19+
for (int k = 0; k < 60; k++) {
20+
// 매 시각 안에 '3'이 포함되어 있다면 카운트 증가
21+
if (check(i, j, k)) cnt++;
22+
}
23+
}
24+
}
25+
cout << cnt << '\n';
26+
return 0;
27+
}

4/2.java

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import java.util.*;
2+
3+
public class Main {
4+
5+
// 특정한 시각 안에 '3'이 포함되어 있는지의 여부
6+
public static boolean check(int h, int m, int s) {
7+
if (h % 10 == 3 || m / 10 == 3 || m % 10 == 3 || s / 10 == 3 || s % 10 == 3)
8+
return true;
9+
return false;
10+
}
11+
12+
public static void main(String[] args) {
13+
Scanner sc = new Scanner(System.in);
14+
15+
// H를 입력받기
16+
int h = sc.nextInt();
17+
int cnt = 0;
18+
19+
for (int i = 0; i <= h; i++) {
20+
for (int j = 0; j < 60; j++) {
21+
for (int k = 0; k < 60; k++) {
22+
// 매 시각 안에 '3'이 포함되어 있다면 카운트 증가
23+
if (check(i, j, k)) cnt++;
24+
}
25+
}
26+
}
27+
28+
System.out.println(cnt);
29+
}
30+
31+
}

4/3.cpp

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
string inputData;
6+
7+
// 나이트가 이동할 수 있는 8가지 방향 정의
8+
int dx[] = {-2, -1, 1, 2, 2, 1, -1, -2};
9+
int dy[] = {-1, -2, -2, -1, 1, 2, 2, 1};
10+
11+
int main(void) {
12+
// 현재 나이트의 위치 입력받기
13+
cin >> inputData;
14+
int row = inputData[1] - '0';
15+
int column = inputData[0] - 'a';
16+
17+
// 8가지 방향에 대하여 각 위치로 이동이 가능한지 확인
18+
int result = 0;
19+
for (int i = 0; i < 8; i++) {
20+
// 이동하고자 하는 위치 확인
21+
int nextRow = row + dx[i];
22+
int nextColumn = column + dy[i];
23+
// 해당 위치로 이동이 가능하다면 카운트 증가
24+
if (nextRow >= 1 && nextRow <= 8 && nextColumn >= 1 && nextColumn <= 8) {
25+
result += 1;
26+
}
27+
}
28+
29+
cout << result << '\n';
30+
return 0;
31+
}

4/3.java

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import java.util.*;
2+
3+
public class Main {
4+
5+
public static void main(String[] args) {
6+
Scanner sc = new Scanner(System.in);
7+
8+
// 현재 나이트의 위치 입력받기
9+
String inputData = sc.nextLine();
10+
int row = inputData.charAt(1) - '0';
11+
int column = inputData.charAt(0) - 'a';
12+
13+
// 나이트가 이동할 수 있는 8가지 방향 정의
14+
int[] dx = {-2, -1, 1, 2, 2, 1, -1, -2};
15+
int[] dy = {-1, -2, -2, -1, 1, 2, 2, 1};
16+
17+
// 8가지 방향에 대하여 각 위치로 이동이 가능한지 확인
18+
int result = 0;
19+
for (int i = 0; i < 8; i++) {
20+
// 이동하고자 하는 위치 확인
21+
int nextRow = row + dx[i];
22+
int nextColumn = column + dy[i];
23+
// 해당 위치로 이동이 가능하다면 카운트 증가
24+
if (nextRow >= 1 && nextRow <= 8 && nextColumn >= 1 && nextColumn <= 8) {
25+
result += 1;
26+
}
27+
}
28+
29+
System.out.println(result);
30+
}
31+
32+
}

4/4.cpp

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
int n, m, x, y, direction;
6+
// 방문한 위치를 저장하기 위한 맵을 생성하여 0으로 초기화
7+
int d[50][50];
8+
// 전체 맵 정보
9+
int arr[50][50];
10+
11+
// 북, 동, 남, 서 방향 정의
12+
int dx[] = {-1, 0, 1, 0};
13+
int dy[] = {0, 1, 0, -1};
14+
15+
// 왼쪽으로 회전
16+
void turn_left() {
17+
direction -= 1;
18+
if (direction == -1) direction = 3;
19+
}
20+
21+
int main(void) {
22+
// N, M을 공백을 기준으로 구분하여 입력받기
23+
cin >> n >> m;
24+
// 현재 캐릭터의 X 좌표, Y 좌표, 방향을 입력받기
25+
cin >> x >> y >> direction;
26+
d[x][y] = 1; // 현재 좌표 방문 처리
27+
28+
// 전체 맵 정보를 입력 받기
29+
for (int i = 0; i < n; i++) {
30+
for (int j = 0; j < m; j++) {
31+
int x;
32+
cin >> x;
33+
arr[i][j] = x;
34+
}
35+
}
36+
37+
// 시뮬레이션 시작
38+
int cnt = 1;
39+
int turn_time = 0;
40+
while (true) {
41+
// 왼쪽으로 회전
42+
turn_left();
43+
int nx = x + dx[direction];
44+
int ny = y + dy[direction];
45+
// 회전한 이후 정면에 가보지 않은 칸이 존재하는 경우 이동
46+
if (d[nx][ny] == 0 && arr[nx][ny] == 0) {
47+
d[nx][ny] = 1;
48+
x = nx;
49+
y = ny;
50+
cnt += 1;
51+
turn_time = 0;
52+
continue;
53+
}
54+
// 회전한 이후 정면에 가보지 않은 칸이 없거나 바다인 경우
55+
else turn_time += 1;
56+
// 네 방향 모두 갈 수 없는 경우
57+
if (turn_time == 4) {
58+
nx = x - dx[direction];
59+
ny = y - dy[direction];
60+
// 뒤로 갈 수 있다면 이동하기
61+
if (arr[nx][ny] == 0) {
62+
x = nx;
63+
y = ny;
64+
}
65+
// 뒤가 바다로 막혀있는 경우
66+
else break;
67+
turn_time = 0;
68+
}
69+
}
70+
71+
cout << cnt << '\n';
72+
}

4/4.java

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import java.util.*;
2+
3+
public class Main {
4+
5+
public static int n, m, x, y, direction;
6+
// 방문한 위치를 저장하기 위한 맵을 생성하여 0으로 초기화
7+
public static int[][] d = new int[50][50];
8+
// 전체 맵 정보
9+
public static int[][] arr = new int [50][50];
10+
11+
// 북, 동, 남, 서 방향 정의
12+
public static int dx[] = {-1, 0, 1, 0};
13+
public static int dy[] = {0, 1, 0, -1};
14+
15+
// 왼쪽으로 회전
16+
public static void turn_left() {
17+
direction -= 1;
18+
if (direction == -1) direction = 3;
19+
}
20+
21+
public static void main(String[] args) {
22+
Scanner sc = new Scanner(System.in);
23+
24+
// N, M을 공백을 기준으로 구분하여 입력받기
25+
n = sc.nextInt();
26+
m = sc.nextInt();
27+
28+
// 현재 캐릭터의 X 좌표, Y 좌표, 방향을 입력받기
29+
x = sc.nextInt();
30+
y = sc.nextInt();
31+
direction = sc.nextInt();
32+
d[x][y] = 1; // 현재 좌표 방문 처리
33+
34+
// 전체 맵 정보를 입력 받기
35+
for (int i = 0; i < n; i++) {
36+
for (int j = 0; j < m; j++) {
37+
arr[i][j] = sc.nextInt();
38+
}
39+
}
40+
41+
// 시뮬레이션 시작
42+
int cnt = 1;
43+
int turn_time = 0;
44+
while (true) {
45+
// 왼쪽으로 회전
46+
turn_left();
47+
int nx = x + dx[direction];
48+
int ny = y + dy[direction];
49+
// 회전한 이후 정면에 가보지 않은 칸이 존재하는 경우 이동
50+
if (d[nx][ny] == 0 && arr[nx][ny] == 0) {
51+
d[nx][ny] = 1;
52+
x = nx;
53+
y = ny;
54+
cnt += 1;
55+
turn_time = 0;
56+
continue;
57+
}
58+
// 회전한 이후 정면에 가보지 않은 칸이 없거나 바다인 경우
59+
else turn_time += 1;
60+
// 네 방향 모두 갈 수 없는 경우
61+
if (turn_time == 4) {
62+
nx = x - dx[direction];
63+
ny = y - dy[direction];
64+
// 뒤로 갈 수 있다면 이동하기
65+
if (arr[nx][ny] == 0) {
66+
x = nx;
67+
y = ny;
68+
}
69+
// 뒤가 바다로 막혀있는 경우
70+
else break;
71+
turn_time = 0;
72+
}
73+
}
74+
75+
System.out.println(cnt);
76+
}
77+
78+
}

0 commit comments

Comments
 (0)