Skip to content

Commit 0d089ff

Browse files
authored
Merge pull request #182 from 4RG0S/202302571
202302571_심민우_9주차
2 parents 05bedac + 6dcb2e6 commit 0d089ff

File tree

2 files changed

+236
-0
lines changed

2 files changed

+236
-0
lines changed

202302571/14503_심민우.cpp

+117
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
// 23.08.21
2+
// BOJ 14503
3+
// 시뮬레이션
4+
5+
#include <iostream>
6+
#include <vector>
7+
#include <algorithm>
8+
#include <string>
9+
10+
using namespace std;
11+
12+
int map[50][50] = { 0, };
13+
int n, m;
14+
15+
struct Cleaner {
16+
int y, x; //현재 위치
17+
int d; //바라보는 방향
18+
19+
int dy = 0, dx = 0; //이동을 했을 시 위치
20+
21+
int cleancount = 0; //청소하는 횟수
22+
23+
bool checkiswall() { //벽인지 확인
24+
return (dy >= 0) && (dy < n) && (dx >= 0) && (dx < m) && (map[dy][dx] != 1);
25+
}
26+
void ifforward() { //앞으로 이동할시 좌표 설정
27+
if (d == 0) {
28+
dy = y - 1;
29+
dx = x;
30+
}
31+
if (d == 1) {
32+
dy = y;
33+
dx = x + 1;
34+
}
35+
if (d == 2) {
36+
dy = y + 1;
37+
dx = x;
38+
}
39+
if (d == 3) {
40+
dy = y;
41+
dx = x - 1;
42+
}
43+
}
44+
void ifbackward() { //뒤로 이동할시 좌표 설정
45+
if (d == 0) {
46+
dy = y + 1;
47+
dx = x;
48+
}
49+
if (d == 1) {
50+
dy = y;
51+
dx = x - 1;
52+
}
53+
if (d == 2) {
54+
dy = y - 1;
55+
dx = x;
56+
}
57+
if (d == 3) {
58+
dy = y;
59+
dx = x + 1;
60+
}
61+
}
62+
void movement() { //이동
63+
y = dy; x = dx;
64+
}
65+
void cleaning() { //현재 칸이 아직 청소되지 않은 경우, 현재 칸을 청소
66+
if (map[y][x] == 0) {
67+
cleancount++;
68+
map[y][x] = 2; //청소한 상태
69+
}
70+
}
71+
void rotation() { //반시계 회전
72+
d--;
73+
if (d == -1)
74+
d = 3;
75+
}
76+
bool checksurround() { //주변 확인
77+
for (int i = 0; i < 4; i++) { //반시계로 돔
78+
rotation();
79+
ifforward();
80+
if (checkiswall() && map[dy][dx] == 0) { //앞쪽 칸이 청소되지 않을 경우
81+
movement();
82+
return true;
83+
}
84+
dy = y; dx = x; //이동하기 전으로 돌려 놓음
85+
}
86+
return false; //주변 4칸 중 청소되지 않은 빈 칸이 없는 경우
87+
}
88+
};
89+
90+
int main() {
91+
ios::sync_with_stdio(false); cin.tie(NULL);
92+
93+
cin >> n >> m;
94+
95+
Cleaner cleaner;
96+
cin >> cleaner.y >> cleaner.x >> cleaner.d;
97+
98+
for (int y = 0; y < n; y++)
99+
for (int x = 0; x < m; x++)
100+
cin >> map[y][x];
101+
102+
while (true) {
103+
cleaner.cleaning();
104+
105+
if (!cleaner.checksurround()) { //주변 4칸 중 청소되지 않은 빈 칸이 없는 경우
106+
cleaner.ifbackward(); //후진 해보고 벽인지 체크
107+
if (!cleaner.checkiswall())
108+
break;
109+
110+
cleaner.movement(); //벽이 아니면 이동
111+
}
112+
}
113+
114+
cout << cleaner.cleancount;
115+
116+
return 0;
117+
}

202302571/16236_심민우.cpp

+119
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
// 23.08.27
2+
// BOJ 16236
3+
// 시뮬레이션
4+
5+
#include <iostream>
6+
#include <vector>
7+
#include <algorithm>
8+
#include <queue>
9+
#include <memory.h>
10+
11+
using namespace std;
12+
13+
int map[20][20] = { {0,} };
14+
int n;
15+
16+
struct Shark {
17+
int y, x;
18+
int size;
19+
int distance = 0;
20+
int sizecount = 0;
21+
22+
Shark(int Y, int X, int Size) : y(Y), x(X), size(Size) {}
23+
};
24+
25+
bool compdistance(Shark& A, Shark& B) {
26+
if (A.distance == B.distance) {
27+
if (A.y == B.y)
28+
return A.x < B.x;
29+
else
30+
return A.y < B.y;
31+
}
32+
else
33+
return A.distance < B.distance;
34+
}
35+
36+
int bfs(const Shark& babyshark, const Shark& edibleshark) {
37+
int dy[] = { 0,1,0,-1 };
38+
int dx[] = { 1,0,-1,0 };
39+
40+
bool isvisited[20][20];
41+
memset(isvisited, false, sizeof(isvisited));
42+
43+
queue<pair<Shark,int>> q;
44+
q.push({ babyshark,0 });
45+
while (!q.empty()) {
46+
pair<Shark, int> cur = q.front(); q.pop();
47+
int cury = cur.first.y;
48+
int curx = cur.first.x;
49+
50+
if (isvisited[cury][curx])
51+
continue;
52+
isvisited[cury][curx] = true;
53+
54+
if (cury == edibleshark.y && curx == edibleshark.x)
55+
return cur.second;
56+
57+
for (int i = 0; i < 4; i++) {
58+
int ny = cury + dy[i];
59+
int nx = curx + dx[i];
60+
61+
if (ny < 0 || ny >= n || nx < 0 || nx >= n)
62+
continue;
63+
if (map[ny][nx] > babyshark.size)
64+
continue;
65+
66+
q.push({ Shark(ny,nx,babyshark.size), cur.second + 1 });
67+
}
68+
}
69+
70+
return 9999;
71+
}
72+
73+
int main() {
74+
ios::sync_with_stdio(false); cin.tie(NULL);
75+
76+
cin >> n;
77+
78+
Shark babyshark = Shark(0, 0, 2);
79+
80+
for (int y = 0; y < n; y++) {
81+
for (int x = 0; x < n; x++) {
82+
cin >> map[y][x];
83+
if (map[y][x] == 9) {
84+
babyshark.y = y;
85+
babyshark.x = x;
86+
}
87+
}
88+
}
89+
int cnt = 0;
90+
while (true) {
91+
vector<Shark> edibleshark;
92+
for (int y = 0; y < n; y++) {
93+
for (int x = 0; x < n; x++) {
94+
if (map[y][x] != 0 && map[y][x] < babyshark.size && map[y][x] != 9) {
95+
edibleshark.push_back(Shark(y, x, map[y][x]));
96+
edibleshark.back().distance = bfs(babyshark, edibleshark.back());
97+
}
98+
}
99+
}
100+
if (edibleshark.empty())
101+
break;
102+
sort(edibleshark.begin(), edibleshark.end(), compdistance);
103+
if (edibleshark.front().distance == 9999)
104+
break;
105+
cnt += edibleshark.front().distance;
106+
if (babyshark.size == ++babyshark.sizecount) {
107+
babyshark.size++;
108+
babyshark.sizecount = 0;
109+
}
110+
map[babyshark.y][babyshark.x] = 0;
111+
babyshark.y = edibleshark.front().y;
112+
babyshark.x = edibleshark.front().x;
113+
map[babyshark.y][babyshark.x] = 9;
114+
}
115+
116+
cout << cnt;
117+
118+
return 0;
119+
}

0 commit comments

Comments
 (0)