Skip to content

Instantly share code, notes, and snippets.

@rep-movsd
Last active May 17, 2016 21:40
Show Gist options
  • Save rep-movsd/f2fec946601114474835cee98e18761c to your computer and use it in GitHub Desktop.
Save rep-movsd/f2fec946601114474835cee98e18761c to your computer and use it in GitHub Desktop.
Topcoder Inv 2001 Semi A+B 1000 pointer
#include <bits/stdc++.h>
#define mp make_pair
#define vstr vector<string>
#define pb push_back
#define all(x) begin(x), end(x)
#define sz(x) int(x.size())
#define P(X) cout << X << "\t"
#define NL cout << endl
#define FOR(I,N) for(int I = 0; I < N; ++I)
using namespace std;
typedef pair<int, int> Coord;
typedef pair<int, int> Incr;
static constexpr Incr UP = mp(-1, 0), DN = mp(1, 0), LT = mp(0, -1), RT = mp(0, 1);
static constexpr Incr NE = mp(-1, 1), NW = mp(-1, -1), SE = mp(1, 1), SW = mp(1, -1);
template<typename T> struct Iter2d
{
// ... snip (See iter2d.cpp) ...
};
struct ChessCover
{
typedef Iter2d<char> Iter;
void attack(vector<vector<char>> &board, const Coord &c, const std::initializer_list<Incr> dirs)
{
for(const auto &dir:dirs)
for(Iter it = Iter(board, c, dir).next(), ite = it.end(); it != ite && (*it == 'U' || *it == '*'); ++it)
*it = '*';
}
void attackOne(vector<vector<char>> &board, const Coord &c, std::initializer_list<Incr> dirs)
{
for(const auto &dir:dirs)
{
Iter it(board, c, dir);
if(++it && *it == 'U') *it = '*';
}
}
int getSafe(vector<string> param0)
{
vector<vector<char>> board;
FOR(i, param0.size()) board.push_back(vector<char>(all(param0[i])));
FOR(i, sz(board))
FOR(j, sz(board[0]))
{
Coord c = mp(i, j);
switch(board[i][j])
{
case 'Q': attack(board, c, {UP, DN, LT, RT, NE, NW, SE, SW}); break;
case 'R': attack(board, c, {UP, DN, LT, RT}); break;
case 'B': attack(board, c, {NE, NW, SE, SW}); break;
case 'P': attackOne(board, c, {NE, NW, SE, SW}); break;
case 'K':
attackOne(board, c, {mp(-2, -1), mp(-1, -2), mp(-2, 1), mp(-1, 2),
mp(2, -1), mp(1, -2), mp(2, 1), mp(1, 2)});
break;
}
}
int cnt = 0;
FOR(i, sz(board))
{
cnt += count(all(board[i]), 'U');
}
return cnt;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment