Bài viết: 28 



Link bài:
Thuật toán:
Code:
Thuật toán:
- Dùng một mảng 2 chiều để đánh dấu các ô trên bàn cờ.
- Với mỗi quân cờ, ta bắt đầu lan ra theo các hướng đi hợp lệ của nó.
Code:
Mã:
#include<bits/stdc++.h>
#define fi first
#define se second
#define ii pair <int, int>
#define rep(k, n) for (int i = k; i <= n; i++)
using namespace std;
int x, t, ans, position_x, position_y;
int bc[107][107];
stack <ii> t_stack, x_stack;
void runfor(int runfor_x, int runfor_y, int valuefor_x, int valuefor_y){
int a = runfor_x; int b = runfor_y;
while (a <= 8 && b <= 8 && a > 0 && b > 0){
if (bc[a][b] == -1) break;
if (bc[a][b] == 0){
bc[a][b] = 1;
ans++;
}
a += valuefor_x;
b += valuefor_y;
}
}
void read(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
//freopen("BACO2.INP", "r", stdin);
//freopen("BACO2.OUT", "w", stdout);
cin >> t >> x;
rep(1, t){
cin >> position_x >> position_y;
bc[position_x][position_y] = -1;
t_stack.push(ii(position_x, position_y));
}
rep(1, x){
cin >> position_x >> position_y;
bc[position_x][position_y] = -1;
x_stack.push(ii(position_x, position_y));
}
}
void run(){
while (t_stack.size()){
ans++;
runfor(t_stack.top().fi+1, t_stack.top().se+1, 1, 1);
runfor(t_stack.top().fi+1, t_stack.top().se-1, 1, -1);
runfor(t_stack.top().fi-1, t_stack.top().se+1, -1, 1);
runfor(t_stack.top().fi-1, t_stack.top().se-1, -1, -1);
t_stack.pop();
}
while (x_stack.size()){
ans++;
runfor(x_stack.top().fi+1, x_stack.top().se, 1, 0);
runfor(x_stack.top().fi-1, x_stack.top().se, -1, 0);
runfor(x_stack.top().fi, x_stack.top().se+1, 0, 1);
runfor(x_stack.top().fi, x_stack.top().se-1, 0, -1);
x_stack.pop();
}
cout << ans;
}
int main(){
read();
run();
return 0;
}