#include "osalib.h" #include <algorithm> #include <vector> #include <set> #include <cstdio> #include <utility> const int MAX = 1005; using namespace std; char b[MAX][MAX]; bool visited[MAX][MAX]; int N; int M; set<int> settlements; void NowaWyspa(int n, int m, char **board) { N = n; M = m; for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { b[i][j] = board[i][j]; if (b[i][j] == 'K') { settlements.insert(i << 15 | j); } } } } bool path(int r1, int c1, int r2, int c2) { //if (r1 == r2 && c1 == c2) return true; visited[r1][c1] = true; if (r1 > 0 && ((r1 - 1 == r2 && c1 == c2) || (b[r1 - 1][c1] != 'W' && !visited[r1 - 1][c1] && path(r1 - 1, c1, r2, c2)))) return true; if (r1 < N - 1 && ((r1 + 1 == r2 && c1 == c2) || (b[r1 + 1][c1] != 'W' && !visited[r1 + 1][c1] && path(r1 + 1, c1, r2, c2)))) return true; if (c1 > 0 && ((r1 == r2 && c1 - 1 == c2) || (b[r1][c1 - 1] != 'W' && !visited[r1][c1 - 1] && path(r1, c1 - 1, r2, c2)))) return true; if (c1 < M - 1 && ((r1 == r2 && c1 + 1) == c2 || (b[r1][c1 + 1] != 'W' && !visited[r1][c1 + 1] && path(r1, c1 + 1, r2, c2)))) return true; return false; } int NowaWarownia(int r, int c) { --r; --c; b[r][c] = 'W'; set<int>::iterator first; set<int>::iterator second; int r1, c1, r2, c2; for (first = settlements.begin(); first != settlements.end(); ++first) { second = first; second++; r1 = *first >> 15; c1 = *first & ((1 << 15) - 1); for (; second != settlements.end(); ++second) { for (int i = 0; i < N; ++i) { for (int j = 0; j < M; ++j) { visited[i][j] = false; } } r2 = *second >> 15; c2 = *second & ((1 << 15) - 1); if (!path(r1, c1, r2, c2)) { b[r][c] = '.'; return 0; } } } return 1; } void PrzeniesOsade(int r1, int c1, int r2, int c2) { --r1; --c1; --r2; --c2; b[r1][c1] = '.'; b[r2][c2] = 'K'; settlements.erase(r1 << 15 | c1); settlements.insert(r2 << 15 | c2); }
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 | #include "osalib.h" #include <algorithm> #include <vector> #include <set> #include <cstdio> #include <utility> const int MAX = 1005; using namespace std; char b[MAX][MAX]; bool visited[MAX][MAX]; int N; int M; set<int> settlements; void NowaWyspa(int n, int m, char **board) { N = n; M = m; for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { b[i][j] = board[i][j]; if (b[i][j] == 'K') { settlements.insert(i << 15 | j); } } } } bool path(int r1, int c1, int r2, int c2) { //if (r1 == r2 && c1 == c2) return true; visited[r1][c1] = true; if (r1 > 0 && ((r1 - 1 == r2 && c1 == c2) || (b[r1 - 1][c1] != 'W' && !visited[r1 - 1][c1] && path(r1 - 1, c1, r2, c2)))) return true; if (r1 < N - 1 && ((r1 + 1 == r2 && c1 == c2) || (b[r1 + 1][c1] != 'W' && !visited[r1 + 1][c1] && path(r1 + 1, c1, r2, c2)))) return true; if (c1 > 0 && ((r1 == r2 && c1 - 1 == c2) || (b[r1][c1 - 1] != 'W' && !visited[r1][c1 - 1] && path(r1, c1 - 1, r2, c2)))) return true; if (c1 < M - 1 && ((r1 == r2 && c1 + 1) == c2 || (b[r1][c1 + 1] != 'W' && !visited[r1][c1 + 1] && path(r1, c1 + 1, r2, c2)))) return true; return false; } int NowaWarownia(int r, int c) { --r; --c; b[r][c] = 'W'; set<int>::iterator first; set<int>::iterator second; int r1, c1, r2, c2; for (first = settlements.begin(); first != settlements.end(); ++first) { second = first; second++; r1 = *first >> 15; c1 = *first & ((1 << 15) - 1); for (; second != settlements.end(); ++second) { for (int i = 0; i < N; ++i) { for (int j = 0; j < M; ++j) { visited[i][j] = false; } } r2 = *second >> 15; c2 = *second & ((1 << 15) - 1); if (!path(r1, c1, r2, c2)) { b[r][c] = '.'; return 0; } } } return 1; } void PrzeniesOsade(int r1, int c1, int r2, int c2) { --r1; --c1; --r2; --c2; b[r1][c1] = '.'; b[r2][c2] = 'K'; settlements.erase(r1 << 15 | c1); settlements.insert(r2 << 15 | c2); } |