目录
问题 A: ab Knight
问题 D: King
问题 I: Queen Coverage
题目描述
In chess, a knight is the only piece that can “jump” over pieces. Namely, a typical knight can move 1 square up, down, left or right, followed by 2 squares in a perpendicular direction. Thus, if a knight is at square (x, y), after its jump it can end up at one of these eight positions:
(x ± 1, y ± 2), (x ± 2, y ± 1), provided that they are in bounds and there isn’t a piece on the destination square.
Now, consider a modified knight called an ab knight, where a and b are both positive integers, not necessarily distinct. From square (x, y), the eight squares the ab knight can reach on a single jump are (x ± a, y ± b), (x ± b, y ± a), provided that they are in bounds.
For the purposes of this problem, we’ll assume that there are no pieces on the board except for the original ab knight. Our goal will be to calculate the fewest number of jumps necessary for the knight to reach each of the other squares on the board.
The Problem
Given the size of a chess board, the values of a and b for the ab knight, and the initial position of
the ab knight, determine the fewest number of moves necessary for the ab knight to reach each of
the squares on the board, or determine that some squares aren’t reachable.
输入
The first line of input will contain a single positive integer, n (n ≤ 20), representing the number of input cases to process. The input cases follow, each taking up three lines. The first line of each input case contains two space separated positive integers, r (3 ≤ r ≤ 100) and c (3 ≤ c ≤ 100), representing the number of rows and columns on the chessboard for the input case. The second line of each input case contains two space separated positive integers, a (a ≤ min(r, c)) and b (b ≤ min(r, c)), representing the values of a and b for the ab knight for the input case. The third line of each input case contains two space separated positive integers, x (x ≤ r) and y (y ≤ c), representing the row and column of the initial location of the ab knight. Assume that the top left hand corner of the board is square is row 1, column 1, the bottom left hand corner of the board is square row r, column 1, the top right hand corner of the board is square row 1, column c, and the bottom right corner of the board is square row r, column c.
输出
For each case, output r lines of c space separated integers, where the jth value on the ith line represent the fewest number of jumps necessary for the ab knight to travel from its initial square to row i column j. Remember not to output a space after the last integer on each row. If a square is unreachable by the ab knight, print out -1 instead.
样例输入 Copy
2
3 3
1 2
1 1
2 5
1 1
1 2
样例输出 Copy
0 3 2
3 -1 1
2 1 4
-1 0 -1 2 -1
1 -1 1 -1 3
#include
#include
#include
using namespace std;
int ans[105][105];
bool v[105][105];
int q[8][2];
int f;
int r,c;
int init(int a,int b) {
if(a==b) {
q[0][0]=a,q[0][1]=b;
q[1][0]=-a,q[1][1]=b;
q[2][0]=-a,q[2][1]=-b;
q[3][0]=a,q[3][1]=-b;
return 4;
} else {
q[0][0]=a,q[0][1]=b;
q[1][0]=-a,q[1][1]=b;
q[2][0]=-a,q[2][1]=-b;
q[3][0]=a,q[3][1]=-b;
q[4][0]=b,q[4][1]=a;
q[5][0]=-b,q[5][1]=a;
q[6][0]=-b,q[6][1]=-a;
q[7][0]=b,q[7][1]=-a;
return 8;
}
}
int d[100005][3];
void bfs(int x,int y) {
memset(d,0,sizeof(d));
int head=0,tail=1;
d[head][1]=x;
d[head][2]=y;
while(head=1&&dx<=r&&dy>=1&&dy<=c&&!v[dx][dy]) {
v[dx][dy]=true;
ans[dx][dy]=ans[d[head][1]][d[head][2]]+1;
d[tail][1]=dx;
d[tail][2]=dy;
tail++;
}
}
head++;
}
}
int main() {
int n;
cin>>n;
while(n--) {
int a,b,x,y;
memset(ans,-1,sizeof(ans));
memset(q,0,sizeof(q));
memset(v,false,sizeof(v));
cin>>r>>c>>a>>b>>x>>y;
ans[x][y]=0;
v[x][y]=true;
f=init(a,b);
bfs(x,y);
for(int i=1; i<=r; i++) {
for(int j=1; j<=c; j++) {
cout<
题目描述
In chess, a king can move 1 space in any of the eight directions: up, down, left, right, up and left, up and right, down and left, and down and right.
We define the “space” of a king to include the square it's on as well as all of the squares it can move to in one move.
Consider trying to place kings on the board such that each square on the board is contained in the space of at least one king. What is the fewest number of kings necessary to achieve the task?
The Problem
Given the size of a chess board, determine the fewest number of kings which would have to be placed on the board so that each square is contained in the space of at least one king.
输入
The first line of input will contain a single positive integer, n (n ≤ 20), representing the number of input cases to process. The input cases follow, each on one line. Each input case contains two space separated positive integers, r (3 ≤ r ≤ 100) and c (3 ≤ c ≤ 100), representing the number of rows and columns on the chessboard for the input case.
输出
For each case, output a single line with a single integer representing the answer to the input case.
样例输入
2
3 3
6 7
样例输出
1
6
#include
#include
#include
using namespace std;
int main() {
int t;
double n,m;
cin>>t;
while(t--) {
cin>>n>>m;
cout<<(ceil(n/3)*ceil(m/3))<
题目描述
In chess, a queen can move as many spaces as she wants in any of the eight directions: up, down, left, right, up and left, up and right, down and left, and down and right, so long as there are no other pieces in her way.
The Problem
Given a large chess board with several queens placed on it, how many squares on the board are either occupied by a queen or can be attacked directly by a queen? (Another way of thinking of the question is: how many squares on the board are already occupied by queens or could have a queen after one move by a queen?)
输入
The first line of input will contain a single positive integer, n (n ≤ 20), representing the number of input cases to process. The input cases follow.
The first line of each input case contains two space separated positive integers, s (3 ≤ s ≤ 2000) and q (0 ≤ q ≤ 2000), representing the size of the chess board (both the number of rows and columns) and the number of queens placed on the board, respectively. Then, there will be q lines, each with two space separated integers, xi and yi (1 ≤ xi , yi ≤ s) representing the row and column of the ith queen.
输出
For each case, output a single line with a single integer representing the answer to the input case.
样例输入
2
3 1
1 3
4 2
1 3
2 2
样例输出
7
14
#include
#include
#include
using namespace std;
int team;
int size,n,x,y;
int work(int size,bool line[],bool hen[],bool zhu[],bool ci[]) {
int start=1,ans=0;
for(int i=1; i<=size; i++) {
if(line[i]) {
ans+=size;
continue;
}
for(int j=start; j<=size; j++) {
if(zhu[i-j+size]||ci[i+j-1]||hen[j]) {
ans++;
}
}
}
return ans;
}
int main() {
cin>>team;
while(team--) {
bool line[2010]= {false},hen[2010]= {false},zhu[4000]= {false},ci[4000]= {false};
cin>>size>>n;
int a=0,b=0,c=0,d=0;
for(int i=0; i>x>>y;
if(!line[x]) {
a++;
line[x]=true;
}
if(!hen[y]) {
b++;
hen[y]=true;
}
if(!zhu[x-y+size]) {
c++;
zhu[x-y+size]=true;
}
if(!ci[x+y-1]) {
ci[x+y-1]=true;
d++;
}
}
cout<