티스토리 뷰
출처 : https://www.hackerrank.com/challenges/queens-attack-2/problem?h_r=internal-search
#include <bits/stdc++.h>
using namespace std;
vector<string> split_string(string);
int queensAttack(int n, int k, int r_q, int c_q, vector<vector<int>> obstacles) {
int ans = 0;
bool check[8] = {0,0,0,0,0,0,0,0};
int ck[8] = { 0,0,0,0,0,0,0,0 };
for (int i = 0; i < k; i++) {
int xr = obstacles[i][0]; int xc = obstacles[i][1];
if (xr == r_q && xc != c_q) {
if (xc > c_q) {
if (check[2]) ck[2] = min(ck[2], (xc - c_q - 1));
else { check[2] = true; ck[2] = (xc - c_q - 1); }
}
else {
if (check[6]) ck[6] = min(ck[6], (c_q - xc - 1));
else { check[6] = true; ck[6] = (c_q - xc - 1);}
}
}
else if (xc == c_q && xr != r_q) {
if (xr > r_q) {
if (check[0]) ck[0] = min(ck[0], (xr - r_q - 1));
else { check[0] = true; ck[0] = (xr - r_q - 1); }
}
else {
if (check[4]) ck[4] = min(ck[4], (r_q - xr - 1));
else { check[4] = true; ck[4] = (r_q - xr - 1); }
}
}
else if (abs(xr - r_q) == abs(xc - c_q)) {
if (xr > r_q) {
if (xc > c_q) {
if (check[1]) ck[1] = min(ck[1], (abs(xr - r_q) - 1));
else { check[1] = true; ck[1] = (abs(xr - r_q) - 1); }
}
else {
if (check[7]) ck[7] = min(ck[7], (abs(xr - r_q) - 1));
else { check[7] = true; ck[7] = (abs(xr - r_q) - 1); }
}
}
else {
if (xc > c_q) {
if (check[3]) ck[3] = min(ck[3], (abs(xr - r_q) - 1));
else { check[3] = true; ck[3] = (abs(xr - r_q) - 1); }
}
else {
if (check[5]) ck[5] = min(ck[5], (abs(xr - r_q) - 1));
else { check[5] = true; ck[5] = (abs(xr - r_q) - 1); }
}
}
}
}
for (int i = 0; i < 8; i++) if (ck[i]) ans += ck[i];
for (int d = 0; d < 8; d++) {
if (!check[d]) {
if (d == 0) ans += (n - r_q);
else if (d == 4 ) ans += (r_q - 1);
else if (d == 2 ) ans += (n - c_q);
else if (d == 6 ) ans += (c_q - 1);
else if (d == 1 ) ans += (min((n-c_q),(n-r_q)));
else if (d == 3 ) ans += (min((n - c_q), (r_q-1)));
else if (d == 5 ) ans += (min((c_q-1), (r_q-1)));
else if (d == 7 ) ans += (min((c_q-1), (n - r_q)));
}
}
return ans;
}
int main()
{
ofstream fout(getenv("OUTPUT_PATH"));
string nk_temp;
getline(cin, nk_temp);
vector<string> nk = split_string(nk_temp);
int n = stoi(nk[0]);
int k = stoi(nk[1]);
string r_qC_q_temp;
getline(cin, r_qC_q_temp);
vector<string> r_qC_q = split_string(r_qC_q_temp);
int r_q = stoi(r_qC_q[0]);
int c_q = stoi(r_qC_q[1]);
vector<vector<int>> obstacles(k);
for (int i = 0; i < k; i++) {
obstacles[i].resize(2);
for (int j = 0; j < 2; j++) {
cin >> obstacles[i][j];
}
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
int result = queensAttack(n, k, r_q, c_q, obstacles);
fout << result << "\n";
fout.close();
return 0;
}
vector<string> split_string(string input_string) {
string::iterator new_end = unique(input_string.begin(), input_string.end(), [](const char &x, const char &y) {
return x == y and x == ' ';
});
input_string.erase(new_end, input_string.end());
while (input_string[input_string.length() - 1] == ' ') {
input_string.pop_back();
}
vector<string> splits;
char delimiter = ' ';
size_t i = 0;
size_t pos = input_string.find(delimiter);
while (pos != string::npos) {
splits.push_back(input_string.substr(i, pos - i));
i = pos + 1;
pos = input_string.find(delimiter, i);
}
splits.push_back(input_string.substr(i, min(pos, input_string.length()) - i + 1));
return splits;
}
'알고리즘 > 기타' 카테고리의 다른 글
[HackerRank] Bigger is Greater (0) | 2018.10.01 |
---|---|
[HackerRank] Organizing Containers of Balls (0) | 2018.10.01 |
[HackerRank] Encryption (0) | 2018.09.30 |
[HackerRank] Non-Divisible Subset (0) | 2018.09.30 |
[HackerRank] Forming a Magic Square (0) | 2018.09.30 |
- Total
- Today
- Yesterday
- SW Expert Academy
- DP
- 백준
- 역량 테스트
- SWEA
- 17143
- DFS
- hackerrank
- 17837
- 연구소 3
- 시간 복잡도
- 17144
- scanf
- string
- 입출력
- 17779
- 2018 카카오 블라인드 채용
- 17140
- boj
- 미세먼지 안녕!
- 게리맨더링 2
- 2018 KAKAO BLIND RECRUITMENT
- 트렌드
- 알고리즘
- STL
- 이차원 배열과 연산
- 팁
- 17142
- 새로운 게임 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 |