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
| #include <iostream> #include <algorithm> #include <queue>
using namespace std;
typedef pair<int, int> PII; typedef pair<PII, int> PIII;
const int N = 120;
queue<PIII> q; bool st[N][N]; int n, k; int x, y; int ans;
int px[] = {1, 2, 2, 1, -1, -2, -2, -1}; int py[] = {2, 1, -1, -2, -2, -1, 1, 2};
int main() { cin >> n >> k; cin >> x >> y; st[x][y] = true; q.push({{x, y},k}); ans = 1; while(q.size()) { auto item = q.front(); q.pop(); int nx = item.first.first, ny = item.first.second; int nk = item.second; for (int i = 0; i < 8; i ++) { int tx = nx + px[i]; int ty = ny + py[i]; int tk = nk - 1; if (tx < 1 || ty < 1 || tx > n || ty > n) { continue; } if (st[tx][ty] == true) { continue; }
st[tx][ty] = true; ans ++; if (tk != 0) { q.push({{tx, ty}, tk}); } } } cout << ans << endl; return 0; }
|