๋์ด๋ | ์ ๋ต๋ฅ |
โ โ โ โ | -% |
ํ๋ฆฌ๋ฏธ์ ์๊ณ ๋ฆฌ์ฆ ์ํด๋ฆฌ ๋นํ์๊ณ ์์ฆ3 3์ 2์ฃผ์ฐจ
goorm
๊ตฌ๋ฆ์ ํด๋ผ์ฐ๋ ๊ธฐ์ ์ ์ด์ฉํ์ฌ ๋๊ตฌ๋ ์ฝ๋ฉ์ ๋ฐฐ์ฐ๊ณ , ์ค๋ ฅ์ ํ๊ฐํ๊ณ , ์ํํธ์จ์ด๋ฅผ ๊ฐ๋ฐํ ์ ์๋ ํด๋ผ์ฐ๋ ์ํํธ์จ์ด ์ํ๊ณ์ ๋๋ค.
www.goorm.io
๋ฌธ์
N*N ํฌ๊ธฐ์ ๊ฒฉ์ํ์ (1,1)์์ ์ถ๋ฐํ์ฌ (N,N)๊น์ง ์ด๋ํ๊ณ ์ ํ๋ค. ๋ฏธ๋ก๋ 0๋ถํฐ 9๊น์ง์ ์ ์๋ก ํํํ ์ ์์ผ๋ฉฐ ๊ฐ๊ฐ์ ์ ์๋ ๋ค์์ ๋ปํ๋ค.
- 0: ์ด๋ํ ์ ์๋ ํต๋ก
- 1: ๋ฒฝ
- 2...9: ์ํ๋ฅผ ํ ์ ์๋ ๋ฐํ, ๊ฐ์ ๋ฒํธ์ ๋ฐํ๋ผ๋ฆฌ ์ด๋ ๊ฐ๋ฅ
2๋ฒ ๋ถํฐ 9๋ฒ๊น์ง๋ ๊ฒฉ์ํ์ ํญ์ 2๊ฐ ์ด์ ์กด์ฌํ๋ฉฐ, ๊ฐ ์นธ์ ์ด๋ํ๋ ์๊ฐ๊ณผ ์ํ๋ฅผ ํ๋ ์๊ฐ์ ๋ชจ๋ 1์ด ๊ฑธ๋ฆฐ๋ค.
๋ฏธ๋ก๋ฅผ ํ์ถํ๊ธฐ ์ํด ๊ฑธ๋ฆฌ๋ ์ต์ ์๊ฐ์ ๊ตฌํด๋ณด์.
์ํ์ฐฉ์ค
DFS๋ก ์ ๊ทผํ๋ค๊ฐ 7๊ฐ์ ํ ์ผ์์ runtime error๋์ DP๋ก ๋ฐ๊ฟจ๋๋ ๋ฌด๋ ค 90์ ์ ๋ง์์๋ค.(7๋ฒ ํ ์ผ๋ง ํ๋ฆผ)
์ ๋ง ๋ชจ๋ฅด๊ฒ ์ด์ ํด์ค์ ๋ณด๋๊น BFS์๋ค. ๊ทธ๋์ ๋ค์ BFS๋ก ์ง๋ณด๋ ๋๋ ๋ 3๊ฐ๋ง ๋ง๊ณ 7๊ฐ์ ํ ์ผ์์ runtime error์๋ค. ์ฌ๊ธฐ์ ๋ด ์ฝ๋๊ฐ ๋ถ๋ช ๋ฌธ์ ๊ฐ ์์๊ตฌ๋ ์ถ์ด์ ํ๋ํ๋ ๋ค์ ์ดํด๋ดค๋๋ฐ, BFSํ ๋, ์ํ๋ฅผ ๋ง๋๋ฉด ์ํ ๋จผ์ queue์ ๋ฃ์ด์ผํ๋๋ฐ, ์ํ๊ฐ ์๋ ๊ฒ ๋ถํฐ ๋ฃ์ด์... ๊ทธ ์์๋ง ๋ฐ๊พธ๋(BFS์ฝ๋์ 46, 47๋ฒ ์ค ์์๋ง ๋ฐ๊ฟ) runtime error๋ ํด๊ฒฐ์ด ๋์๊ณ 7๋ฒ๊ณผ 10๋ฒ ํ ์คํธ์ผ์ด์ค๋ง ํ๋ฆฌ๊ฒ ๋์๋ค. ๋ ์ฉ..๐ฑ
+ 200327 ๋์ฐฉํ์ง ๋ชปํ๋๊ฒฝ์ฐ -1๋ก ์ถ๋ ฅํ๊ฒ ํ๋ 7๋ฒ ํ ์ผ ํ๋๋ง ํ๋ฆฌ๊ฒ๋จ.
๊ณ์ ์๊ฐ์ ํด๋ด๋ ์ด์ ๋ ๋ ์ด์ ์์ด๋์ด๊ฐ ๋์ค์ง ์๋๋คใ ์ผ๋จ ๋ธ๋ก๊ทธ์ ์ฌ๋ ค๋๊ณ ๋์ค์ ๋ค์ ํ์ด๋ด์ผ๊ฒ ๋ค.
์ฝ๋
DP : ์์์ ์๋๋ก๋ง ๊ฐ๋ ๋งจ ์๋์์ ๋ค์ ์ฌ๋ผ์ค๋ ์ผ์ด์ค์์๋ ์คํจ.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
#include <iostream>
#include <string.h>
#include <vector>
#include <algorithm>
using namespace std;
typedef pair<int, int> p;
int dx[] = { 0,1,0 }, dy[] = { 1,0,-1 };
int n, arr[1005][1005], dp[1005][1005];
vector<p> warp[10];
void goWarp(int x, int y, int warpNum) {
for (auto e : warp[warpNum]) {
p child = e;
int nx = child.first, ny = child.second;
if (nx == x && ny == y) continue;
dp[nx][ny] = dp[nx][ny] == -1 ? dp[x][y] + 1 : min(dp[nx][ny], dp[x][y] + 1);
}
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
scanf("%d", &arr[i][j]);
if (arr[i][j] > 1) {
warp[arr[i][j]].push_back({ i,j });
}
}
}
memset(dp, -1, sizeof(dp));
dp[0][0] = 1;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (dp[i][j] == -1) continue;
for (int k = 0; k < 3; k++) {
int nx = i + dx[k], ny = j + dy[k];
if (nx < 0 || ny < 0 || nx >= n || ny >= n || (dp[nx][ny] != -1 && dp[nx][ny] <= dp[i][j])) continue;
if (arr[nx][ny] != 1) dp[nx][ny] = dp[i][j] + 1;
if (arr[nx][ny] > 1) goWarp(nx, ny, arr[nx][ny]);
}
}
for (int j = n - 1; j >= 0; j--) {
if (dp[i][j] == -1) continue;
for (int k = 0; k < 3; k++) {
int nx = i + dx[k], ny = j + dy[k];
if (nx < 0 || ny < 0 || nx >= n || ny >= n || (dp[nx][ny] != -1 && dp[nx][ny] <= dp[i][j])) continue;
if (arr[nx][ny] != 1) dp[nx][ny] = dp[i][j] + 1;
if (arr[nx][ny] > 1) goWarp(nx, ny, arr[nx][ny]);
}
}
}
printf("%d", dp[n - 1][n - 1]);
return 0;
}
|
cs |
BFS : int dx[] = { 0,1,0,-1 } ์ด์ด์ผ ํ๋ค...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
typedef pair<int, int> p;
int dx[] = { 0,1,0,1 }, dy[] = { 1,0,-1,0 };
int n, arr[1005][1005], chk[1005][1005], warpChk[10], ans;
vector<p> warp[10];
queue<p> q;
void warpPush(int x, int y, int warpNum) {
warpChk[warpNum] = 1;
for (auto e : warp[warpNum]) {
p child = e;
int time = chk[x][y], nx = child.first, ny = child.second;
if (nx == x && ny == y) continue;
chk[nx][ny] = time + 1;
q.push({ nx, ny });
}
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
scanf("%d", &arr[i][j]);
if (arr[i][j] > 1) {
warp[arr[i][j]].push_back({ i,j });
}
}
}
chk[0][0] = 1;
q.push({ 0, 0 });
while (!q.empty()) {
p front = q.front();
q.pop();
int x = front.first, y = front.second;
int time = chk[x][y];
if (x == n - 1 && y == n - 1) {
break;
}
for (int k = 0; k < 4; k++) {
int nx = x + dx[k], ny = y + dy[k];
if (nx < 0 || ny < 0 || nx >= n || ny >= n || arr[nx][ny] == 1 || chk[nx][ny] || warpChk[arr[nx][ny]]) continue;
chk[nx][ny] = time + 1;
if (arr[nx][ny] > 1) { warpPush(nx, ny, arr[nx][ny]); }
q.push({ nx, ny });
}
}
printf("%d", chk[n - 1][n - 1] ? chk[n - 1][n - 1] : -1);
return 0;
}
|
cs |
200327 ์ถ๊ฐ
deque๋ฅผ ์ด์ฉํด์ ํด๋ณด๋๊น 80์ ๋ง์.. ๋๋ ์ข์๋๋ฐ ๊น๋นใ
204002 ์ถ๊ฐ
5
0 1 0 0 0
0 1 0 1 0
0 1 0 2 0
0 1 0 0 0
0 0 0 2 0
์ธ ๊ฒฝ์ฐ 11์ด ๋์์ผ ํ๋๋ฐ 12๊ฐ ๋์ค๊ณ ์์์.(์ํ ์ฐ์ ํ์ ๋๋ฌธ์)
๊ทธ๋์ ์๋ ์กฐ๊ฑด๋ฌธ์ (chk[nx][ny] && chk[nx][ny] <= chk[x][y]) ์ด ์กฐ๊ฑด์ ์คฌ๋๋ ๋ต์ ๋ง์ง๋ง, ์๊ฐ์ด๊ณผ๋ฌ๋ค. ๊ทธ๋์ ๋ฑ์ผ๋ก๋ ๋ชป ํธ๋ ๋ฌธ์ ์ธ ๊ฒ ๊ฐ๊ณ ๊ทธ๋ฅ BFS ํ ์ด์ฉํด์ ๋๋ฆฌ๋ ๊ฒ ํธํ ๊ฒ ๊ฐ๋ค.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
#include <iostream>
#include <vector>
#include <deque>
#include <algorithm>
using namespace std;
typedef pair<int, int> p;
int dx[] = { 0, 1, 0,-1 }, dy[] = { 1, 0, -1, 0 };
int arr[1005][1005], chk[1005][1005], warpChk[10];
int n;
vector<p> warp[10];
deque<p> dq;
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
scanf("%d", &arr[i][j]);
if (arr[i][j] > 1) {
warp[arr[i][j]].push_back({ i, j });
}
}
}
chk[0][0] = 1;
dq.push_back({ 0,0 });
while (dq.size()) {
auto front = dq.front();
dq.pop_front();
int x = front.first, y = front.second;
if (x == n - 1 && y == n - 1) {
printf("%d", chk[x][y]);
return 0;
}
for (int i = 0; i < 4; i++) {
int nx = x + dx[i], ny = y + dy[i];
if (nx < 0 || ny < 0 || nx >= n || ny >= n || chk[nx][ny] || arr[nx][ny] == 1) continue;
chk[nx][ny] = chk[x][y] + 1;
if (arr[nx][ny] && !warpChk[arr[nx][ny]]) { // ์ํ์ด๊ณ ๋ฐฉ๋ฌธํ์ง ์์ ์ํ๋ฉด
warpChk[arr[nx][ny]] = 1;
for (auto child : warp[arr[nx][ny]]) {
int cx = child.first, cy = child.second;
if (cx == nx && cy == ny) continue;
chk[cx][cy] = chk[nx][ny] + 1;
dq.push_front({ cx, cy });
}
}
else {
dq.push_back({ nx, ny });
}
}
}
printf("-1");
return 0;
}
|
cs |
์ฑ๊ณต!!
ํด๊ฒฐ
key point, int dx[] = { 0,1,0, -1 }, dy[] = { 1,0,-1, 0 }; ์์ชฝ๋ ๋ฐฉ๋ฌธํด์ผ ํ๋ค๋ ์ฌ์ค!!!
- ์ ๋ ฅ์ ๋ฐ๋ค๊ฐ ์ํ๋ฅผ ๋ง๋๋ฉด, ์ํ๋ฒํธ์ ๋ง๋ ํ์ ์ธ์๋ก ์ขํ๋ฅผ ์ ์ฅํ๋ค.
- ์ด๊ธฐ๊ฐ( chk[0][0]=1, q.push({0,0}) )์ ์ค์ ํ๋ค.
- chk๋ฐฐ์ด์ ์ฐ์์
- ๋ฐฉ๋ฌธ์ ํ๋ค๋ฉด 1์ด์์ด๊ณ
- ๋ฐฉ๋ฌธํ์ง ์์๋ค๋ฉด 0์ด๋ค.
- chk[i][j]์ ๊ฐ์ (i, j)๋ฅผ ๋ฐฉ๋ฌธํ๋ ์ต์์๊ฐ์ด๋ค.
- BFS๋ฅผ ๋๋ค.
- ํ์ฌ ์์น๋ x, y์ด๊ณ , ์ธ์ ํ ๋ค ๋ฐฉํฅ์ nx, ny์ด๋ค.
- ์ธ์ ํ ๊ณณ์ ๋ํด์ N*N ๋ฒ์๋ฅผ ๋์ด๊ฐ๊ฑฐ๋, ๋ฒฝ์ด๊ฑฐ๋, ๋ฐฉ๋ฌธํ๊ฑฐ๋, ์ด๋ฏธ ๋ฐฉ๋ฌธํ ์ํ๋ผ๋ฉด ๋ ์ด์ ํ์ธํ์ง ์๋๋ค.
- ๊ทธ ์ธ์ ๊ฒฝ์ฐ์๋ ๋ฐฉ๋ฌธ์ ํด์ผํ๋ฏ๋ก chk[nx][ny]์ ํ์ฌ์๊ฐ+1๋ก ์ ์ฅํ๋ค.
- ๊ทธ๋ฐ๋ฐ (nx, ny)์์น๊ฐ ์ํ๋ผ๋ฉด
- ํด๋น ์ํ๋ฒํธ์ ๋ํด์ ๋ฐฉ๋ฌธํ์์ ์๋ฆฌ๊ณ → warpChk
- (ํ์ฌ ์ํ์์น ์ ์ธ)๊ฐ์ ์ํ๋ฒํธ๋ฅผ ๊ฐ์ง ๋ชจ๋ ์์น์ ๋ํ์ฌ, ์๊ฐ์ ์ค์ ํ๊ณ , ์ขํ๋ฅผ q์ ๋ฃ์ด์ค๋ค.
- (nx, ny)์์น๊ฐ ์ํ๊ฐ ์๋๋ผ๋ฉด, ํ์ธํ ๊ฑฐ ์์ด q์ ๋ฃ์ด์ค๋ค.
- ์์ ์์๋ฅผ ๋ฐ๋ณตํ๋ค๊ฐ x๊ฐ n-1์ด๊ณ , y๊ฐ n-1์ด๋ผ๋ฉด ํ์ถ๊ตฌ์ด๋ฏ๋ก ๋ฐ๋ณต๋ฌธ์ ์ข ๋ฃํ๊ณ ํ์ฌ์๊ฐ์ ์ถ๋ ฅํ๋ค.
- ๋ง์ฝ ํ์ถํ ์ ์๋ค๋ฉด -1์ ์ถ๋ ฅํ๋ค.
์ฝ๋
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
typedef pair<int, int> p;
int dx[] = { 0,1,0,-1 }, dy[] = { 1,0,-1,0 };
int n, arr[1005][1005], chk[1005][1005], warpChk[10];
vector<p> warp[10];
queue<p> q;
void warpPush(int x, int y, int warpNum) {
warpChk[warpNum] = 1;
for (auto child : warp[warpNum]) {
int time = chk[x][y], cx = child.first, cy = child.second;
if (cx == x && cy == y) continue;
chk[cx][cy] = time + 1;
q.push({ cx, cy });
}
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
scanf("%d", &arr[i][j]);
if (arr[i][j] > 1) {
warp[arr[i][j]].push_back({ i,j });
}
}
}
chk[0][0] = 1;
q.push({ 0, 0 });
while (!q.empty()) {
p front = q.front();
q.pop();
int x = front.first, y = front.second;
int time = chk[x][y];
if (x == n - 1 && y == n - 1) {
break;
}
for (int k = 0; k < 4; k++) {
int nx = x + dx[k], ny = y + dy[k];
if (nx < 0 || ny < 0 || nx >= n || ny >= n || arr[nx][ny] == 1 || chk[nx][ny] || warpChk[arr[nx][ny]]) continue;
chk[nx][ny] = time + 1;
if (arr[nx][ny] > 1) { warpPush(nx, ny, arr[nx][ny]); }
else q.push({ nx, ny });
}
}
printf("%d", chk[n - 1][n - 1] ? chk[n - 1][n - 1] : -1);
return 0;
}
|
cs |
ํ๊ธฐ
์ํ์ฐฉ์ค์ BFS ์ฝ๋์์ dx๋ง์ง๋ง ์ธ์๋ฅผ -1๋ก ์์ ํด์ฃผ๋ฉด ์ฑ๊ณต์ด์๋ค.. ์ฝ!!
DP๋ก ๋ฐ๊ฟํ๋ค๊ฐ -1์ด ํ์ํ์ง ์์ ๊ฒ ๊ฐ์์ ๋บ์๋๋ฐ.... dp์๋ ํ์ํ๋๋ณด๋ค?
+ ์์ ํด๋ดค๋๋ฐ ๊ณ์ 7๋ฒ์ด ํ๋ฆผ.. ๋์ค์ ๊ณ ์ณ์ผ์ง ใฑ-
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
#include <iostream>
#include <string.h>
#include <vector>
#include <algorithm>
using namespace std;
typedef pair<int, int> p;
int dx[] = { 0,1,0,-1 }, dy[] = { 1,0,-1,0 };
int n, arr[1005][1005], dp[1005][1005], warpChk[10];
vector<p> warp[10];
void goWarp(int x, int y, int warpNum) {
for (auto child : warp[warpNum]) {
int nx = child.first, ny = child.second;
if (nx == x && ny == y) continue;
dp[nx][ny] = dp[nx][ny] == -1 ? dp[x][y] + 1 : min(dp[nx][ny], dp[x][y] + 1);
}
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
scanf("%d", &arr[i][j]);
if (arr[i][j] > 1) {
warp[arr[i][j]].push_back({ i,j });
}
}
}
memset(dp, -1, sizeof(dp));
dp[0][0] = 1;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (dp[i][j] == -1) continue;
for (int k = 0; k < 4; k++) {
int nx = i + dx[k], ny = j + dy[k];
if (nx < 0 || ny < 0 || nx >= n || ny >= n || arr[nx][ny] == 1 || (dp[nx][ny] != -1 && dp[nx][ny] <= dp[i][j]) || warpChk[arr[nx][ny]] ) continue;
dp[nx][ny] = dp[i][j] + 1;
if (arr[nx][ny] > 1) goWarp(nx, ny, arr[nx][ny]);
}
}
for (int j = n - 1; j >= 0; j--) {
if (dp[i][j] == -1) continue;
for (int k = 0; k < 4; k++) {
int nx = i + dx[k], ny = j + dy[k];
if (nx < 0 || ny < 0 || nx >= n || ny >= n || arr[nx][ny] == 1 || (dp[nx][ny] != -1 && dp[nx][ny] <= dp[i][j]) || warpChk[arr[nx][ny]]) continue;
dp[nx][ny] = dp[i][j] + 1;
if (arr[nx][ny] > 1) {
goWarp(nx, ny, arr[nx][ny]);
warpChk[arr[nx][ny]] = 1;
}
}
}
}
printf("%d", dp[n - 1][n - 1]);
return 0;
}
|
cs |
deque์ผ๋ก ํผ ๋ฐฉ๋ฒ์.. ์.... ์ฌ์น์์ ๋ฐฉ๋ฒ์ธ๊ฒ ๊ฐ๋ค!
+ 200401 ์ง๋ฌธ๋จ๊ฒจ์ ๋ต๋ณ์ ๋ฐ์๋๋ฐ
5
0 1 0 0 0
0 1 0 1 0
0 1 0 1 0
0 1 0 1 0
0 0 0 1 0
์์ ๊ฐ์ ๊ฒฝ์ฐ์ ๋ด dp์ฝ๋๋ก ํ์์ ๊ฒฝ์ฐ (4, 2)์์ ๋ ์ด์ ์๋ก ์ฌ๋ผ๊ฐ ์ ์์ผ๋ฏ๋ก -1์ ์ถ๋ ฅํ๊ฒ ๋๋ค.
๋ค์ํ ํ ์คํธ์ผ์ด์ค์ ๋ํด์ ์๊ฐํด๋ณผ์ค ์์๋ค๋ฉด ๊ฐ๋จํ ํด๊ฒฐํ์ ๊ฒ ๊ฐ๋ค.
+ 200402 ๋ฑ๋ ๋ฐ๋ก๋ฅผ ์ฐพ์์ ์ํ๋ฅผ ์์ ๋ฃ์ผ๋ฉด ์๋๋ค๋ ๊ฒ์ ๋ฐ๊ฒฌํ๋ค. ๊ทธ๋ด๊ฑฐ๋ฉด ๊ทธ๋ฅ ํธํ๊ฒ ํ๋ฅผ ์ฐ์!
'๐ฅ PS(Problem Solving) ๐ฅ > goorm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๊ตฌ๋ฆLEVEL] ๋ง๊ฐ์ง ์๋ผํ ์คํ ๋ค์ค์ ์ฒด (0) | 2020.03.27 |
---|---|
[๊ตฌ๋ฆLEVEL] ๋ฒฝ ํต๊ณผํ๊ธฐ (0) | 2020.03.25 |
[๊ตฌ๋ฆLEVEL] ๋ณ๋ค์ ์ ์ (0) | 2020.03.24 |
[๊ตฌ๋ฆLEVEL] ์ ์ต์ (0) | 2020.03.23 |
[๊ตฌ๋ฆLEVEL] ๋ฐฑ์ (0) | 2020.03.23 |
๋๊ธ