Posts 13460 Gold 2
Post
Cancel

13460 Gold 2

13460 Gold 2

코드

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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include <iostream>
#include <algorithm>
#include <cstring>
#define MAX_WIDTH 10
#define MAX_DEEP 10

using namespace std;

char map[MAX_WIDTH][MAX_WIDTH];
int width;
int height;
int R[2];
int B[2];
int Hole[2];
int minDeep;

void getProperties();
void findMinWay(int tempR[], int tempB[], int deep = 0);
void print(int tempR[], int tempB[], int deep);

int main() {
	getProperties();
	minDeep = 100;
	findMinWay(R, B);
	if (minDeep == 100)
		cout << -1;
	else
		cout << minDeep;
}

void getProperties() {
	cin >> height >> width;

	for (int h = 0; h < height; h++) {
		for (int w = 0; w < width; w++) {
			cin >> map[h][w];
			if (map[h][w] == 'R') {
				R[0] = h;
				R[1] = w;
			}
			else if (map[h][w] == 'B') {
				B[0] = h;
				B[1] = w;
			}
			else if (map[h][w] == 'O') {
				Hole[0] = h;
				Hole[1] = w;
			}
		}
	}
}


void findMinWay(int tempR[], int tempB[], int deep) {
	if (tempB[0] == Hole[0] && tempB[1] == Hole[1])
		return;
	if (tempR[0] == Hole[0] && tempR[1] == Hole[1]) {
		if (deep < minDeep) {
			minDeep = deep;
		}
		return;
	}
	if (deep >= MAX_DEEP)
		return;

	int currentR[2], currentB[2];
	memcpy(currentR, tempR, sizeof(tempR)*2);
	memcpy(currentB, tempB, sizeof(tempB)*2);

	//print(tempR, tempB, deep);

	//위
	while (map[currentR[0]-1][currentR[1]] != '#' && map[currentR[0]][currentR[1]] != 'O')
		currentR[0]--;
	while (map[currentB[0]-1][currentB[1]] != '#' && map[currentB[0]][currentB[1]] != 'O')
		currentB[0]--;
	if (currentR[0] == currentB[0] && currentR[1] == currentB[1])
		if (map[currentB[0]][currentB[1]] != 'O') {
			if (tempR[0] < tempB[0])
				currentB[0]++;
			else
				currentR[0]++;
		}
	findMinWay(currentR, currentB, deep + 1);
	memcpy(currentR, tempR, sizeof(int) * 2);
	memcpy(currentB, tempB, sizeof(int) * 2);


	//오른쪽
	while (map[currentR[0]][currentR[1]+1] != '#' && map[currentR[0]][currentR[1]] != 'O')
		currentR[1]++;
	while (map[currentB[0]][currentB[1]+1] != '#' && map[currentB[0]][currentB[1]] != 'O')
		currentB[1]++;
	if (currentR[0] == currentB[0] && currentR[1] == currentB[1])
		if (map[currentB[0]][currentB[1]] != 'O') {
			if (tempR[1] < tempB[1])
				currentR[1]--;
			else
				currentB[1]--;
		}
	findMinWay(currentR, currentB, deep + 1);
	memcpy(currentR, tempR, sizeof(int) * 2);
	memcpy(currentB, tempB, sizeof(int) * 2);

	//아래
	while (map[currentR[0]+1][currentR[1]] != '#' && map[currentR[0]][currentR[1]] != 'O')
		currentR[0]++;
	while (map[currentB[0]+1][currentB[1]] != '#' && map[currentB[0]][currentB[1]] != 'O')
		currentB[0]++;
	if (currentR[0] == currentB[0] && currentR[1] == currentB[1])
		if (map[currentB[0]][currentB[1]] != 'O') {
			if (tempR[0] < tempB[0])
				currentR[0]--;
			else
				currentB[0]--;
		}
	findMinWay(currentR, currentB, deep + 1);
	memcpy(currentR, tempR, sizeof(int) * 2);
	memcpy(currentB, tempB, sizeof(int) * 2);

	//왼쪽
	while (map[currentR[0]][currentR[1]-1] != '#' && map[currentR[0]][currentR[1]] != 'O')
		currentR[1]--;
	while (map[currentB[0]][currentB[1]-1] != '#' && map[currentB[0]][currentB[1]] != 'O')
		currentB[1]--;
	if (currentR[0] == currentB[0] && currentR[1] == currentB[1])
		if (map[currentB[0]][currentB[1]] != 'O') {
			if (tempR[1] < tempB[1])
				currentB[1]++;
			else
				currentR[1]++;
		}
	findMinWay(currentR, currentB, deep + 1);
	memcpy(currentR, tempR, sizeof(int) * 2);
	memcpy(currentB, tempB, sizeof(int) * 2);
}

void print(int tempR[], int tempB[], int deep) {
	cout << "deep : " << deep << "/ R : " << tempR[0] << ' ' << tempR[1] << "/ B : " << tempB[0] << ' ' << tempB[1] << endl;
}
This post is licensed under CC BY 4.0 by the author.

12865 평범한 배낭

14501 Samsung sw test

Comments powered by Disqus.