Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define ll long long
- #define endl '\n'
- #define sz(x) int(x.size())
- #define all(x) x.begin(), x.end()
- int main() {
- ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
- string s;
- while (cin >> s) {
- if (s == "") break;
- int row = 0, col = 0;
- auto valid = [](int i, int j) -> bool {
- return 0 <= i && i < 8 && 0 <= j && j < 8;
- };
- vector<vector<bool>> under_attack(8, vector<bool>(8, 0));
- vector<vector<bool>> occ(8, vector<bool>(8, 0));
- for (int J = 0; J < sz(s); J++) {
- if (s[J] == '/') row++, col = 0;
- else {
- if (isdigit(s[J])) col += s[J] - '0';
- else {
- occ[row][col] = 1;
- col++;
- }
- }
- }
- row = 0, col = 0;
- for (int J = 0; J < sz(s); J++) {
- if (s[J] == '/') row++, col = 0;
- else {
- if (isdigit(s[J])) col += s[J] - '0';
- else {
- if (s[J] == 'q' || s[J] == 'Q') { // Queen
- for (int i = 1; i <= 8; i++) {
- if (valid(row + i, col + i)) {
- if (occ[row + i][col + i]) break;
- under_attack[row + i][col + i] = 1;
- }
- }
- for (int i = 1; i <= 8; i++) {
- if (valid(row - i, col - i)) {
- if (occ[row - i][col - i]) break;
- under_attack[row - i][col - i] = 1;
- }
- }
- for (int i = 1; i <= 8; i++) {
- if (valid(row + i, col)) {
- if (occ[row + i][col]) break;
- under_attack[row + i][col] = 1;
- }
- }
- for (int i = 1; i <= 8; i++) {
- if (valid(row - i, col)) {
- if (occ[row - i][col]) break;
- under_attack[row - i][col] = 1;
- }
- }
- for (int i = 1; i <= 8; i++) {
- if (valid(row, col + i)) {
- if (occ[row][col + i]) break;
- under_attack[row][col + i] = 1;
- }
- }
- for (int i = 1; i <= 8; i++) {
- if (valid(row, col - i)) {
- if (occ[row][col - i]) break;
- under_attack[row][col - i] = 1;
- }
- }
- for (int i = 1; i <= 8; i++) {
- if (valid(row - i, col + i)) {
- if (occ[row - i][col + i]) break;
- under_attack[row - i][col + i] = 1;
- }
- }
- for (int i = 1; i <= 8; i++) {
- if (valid(row + i, col - i)) {
- if (occ[row + i][col - i]) break;
- under_attack[row + i][col - i] = 1;
- }
- }
- }
- else if (s[J] == 'p' || s[J] == 'P') {
- if (valid(row + (s[J] == 'p' ? 1 : -1), col + 1))
- under_attack[row + (s[J] == 'p' ? 1 : -1)][col + 1] = 1;
- if (valid(row + (s[J] == 'p' ? 1 : -1), col - 1))
- under_attack[row + (s[J] == 'p' ? 1 : -1)][col - 1] = 1;
- }
- else if (s[J] == 'r' || s[J] == 'R') {
- for (int i = 1; i <= 8; i++) {
- if (valid(row + i, col)) {
- if (occ[row + i][col]) break;
- under_attack[row + i][col] = 1;
- }
- }
- for (int i = 1; i <= 8; i++) {
- if (valid(row - i, col)) {
- if (occ[row - i][col]) break;
- under_attack[row - i][col] = 1;
- }
- }
- for (int i = 1; i <= 8; i++) {
- if (valid(row, col + i)) {
- if (occ[row][col + i]) break;
- under_attack[row][col + i] = 1;
- }
- }
- for (int i = 1; i <= 8; i++) {
- if (valid(row, col - i)) {
- if (occ[row][col - i]) break;
- under_attack[row][col - i] = 1;
- }
- }
- }
- else if (s[J] == 'b' || s[J] == 'B') {
- for (int i = 1; i <= 8; i++) {
- if (valid(row + i, col + i)) {
- if (occ[row + i][col + i]) break;
- under_attack[row + i][col + i] = 1;
- }
- }
- for (int i = 1; i <= 8; i++) {
- if (valid(row - i, col - i)) {
- if (occ[row - i][col - i]) break;
- under_attack[row - i][col - i] = 1;
- }
- }
- for (int i = 1; i <= 8; i++) {
- if (valid(row - i, col + i)) {
- if (occ[row - i][col + i]) break;
- under_attack[row - i][col + i] = 1;
- }
- }
- for (int i = 1; i <= 8; i++) {
- if (valid(row + i, col - i)) {
- if (occ[row + i][col - i]) break;
- under_attack[row + i][col - i] = 1;
- }
- }
- }
- else if (s[J] == 'n' || s[J] == 'N') {
- vector<pair<int, int>> knight_moves = {
- {2, 1}, {2, -1}, {-2, 1}, {-2, -1},
- {1, 2}, {-1, 2}, {1, -2}, {-1, -2}
- };
- for (auto & it : knight_moves) {
- int dx = it.first + row, dy = it.second + col;
- if (valid(dx, dy)) under_attack[dx][dy] = 1;
- }
- }
- else if (s[J] == 'k' || s[J] == 'K') {
- for (int i = -1; i < 2; i++) {
- for (int j = -1; j < 2; j++) {
- int dx = row + i, dy = col + j;
- if (valid(dx, dy)) under_attack[dx][dy] = 1;
- }
- }
- }
- under_attack[row][col] = 1;
- col++;
- }
- }
- }
- int cnt = 0;
- for (int i = 0; i < 8; i++) {
- for (int j = 0; j < 8; j++) {
- cnt += (under_attack[i][j] == 0);
- }
- }
- cout << cnt << endl;
- s = "";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement