Skip to content

Instantly share code, notes, and snippets.

@gusanthiago
Created October 19, 2019 18:56
Show Gist options
  • Save gusanthiago/1c46cc72972444c19ef19edea01d616a to your computer and use it in GitHub Desktop.
Save gusanthiago/1c46cc72972444c19ef19edea01d616a to your computer and use it in GitHub Desktop.
#include <bits/stdc++.h>
using namespace std;
vector<string> split_string(string);
map<string, bool> queenMap;
int getSquaresInDirection(int n, int iQueen, int jQueen, pair<int, int> direction) {
int count = 0;
string keyMap;
while (true) {
iQueen += direction.first;
jQueen += direction.second;
if (! (iQueen > 0 && iQueen <= n && jQueen > 0 && jQueen <= n)) {
break;
}
keyMap = to_string(iQueen) + "-" + to_string(jQueen);
if (queenMap[keyMap] != NULL) {
break;
}
count++;
}
return count;
}
// Complete the queensAttack function below.
int queensAttack(int n, int k, int r_q, int c_q, vector<vector<int>> obstacles) {
int totalCount = 0;
string keyMap = "";
for (int i=0 ; i < k; i++) {
keyMap = to_string(obstacles[i][0]) + "-" + to_string(obstacles[i][1]);
cout << keyMap << " " << (obstacles[i][0]) << " " << (obstacles[i][1]) << endl;
queenMap[keyMap] = true;
}
totalCount += getSquaresInDirection(n, r_q, c_q, make_pair(0, 1));
totalCount += getSquaresInDirection(n, r_q, c_q, make_pair(0, -1));
totalCount += getSquaresInDirection(n, r_q, c_q, make_pair(1, 0));
totalCount += getSquaresInDirection(n, r_q, c_q, make_pair(-1, 0));
totalCount += getSquaresInDirection(n, r_q, c_q, make_pair(1, 1));
totalCount += getSquaresInDirection(n, r_q, c_q, make_pair(-1, -1));
totalCount += getSquaresInDirection(n, r_q, c_q, make_pair(-1, 1));
totalCount += getSquaresInDirection(n, r_q, c_q, make_pair(1, -1));
return totalCount;
}
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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment