问题 A: ab Knight(C++)(BFS)

目录

1.题目描述

2.AC


1.题目描述

问题 A: ab Knight
时间限制: 1.000 Sec  内存限制: 128 MB
 
题目描述
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. 
 

样例输入 
2 
3 3
1 2
1 1
2 5
1 1
1 2

样例输出
0 3 2 
3 -1 1 
2 1 4 
-1 0 -1 2 -1
1 -1 1 -1 3

2.AC

#include 
#include 
#include 
using namespace std;
int m, n, a, b, sx, sy;
int ans[105][105], v[105][105];
int dx[8], dy[8];
struct node {
	int x, y;
}t, p;
queue  q;
int bfs () { 
	while (!q.empty()) {
		p = q.front();
		q.pop();
		for (int i = 0; i < 8; i++) {
			if (p.x+dx[i]<1||p.x+dx[i]>n||p.y+dy[i]<1||p.y+dy[i]>m||v[p.x+dx[i]][p.y+dy[i]]) continue;
			t.x = p.x+dx[i];
			t.y = p.y+dy[i];
			v[t.x][t.y] = 1;
			ans[t.x][t.y] = ans[p.x][p.y] + 1;
			q.push(t);
		}
	}
}
int main () {
	int T;
	cin>>T;
	while (T--) {
		memset (ans, -1, sizeof (ans));
		memset (v, 0, sizeof (v));
		cin>>n>>m>>a>>b>>sx>>sy;
		dx[0] = a, dx[1] = a, dx[2] = -a, dx[3] = -a, dx[4] = b, dx[5] = b, dx[6] = -b, dx[7] = -b;
		dy[0] = b, dy[1] = -b, dy[2] = b, dy[3] = -b, dy[4] = a, dy[5] = -a, dy[6] = a, dy[7] = -a;
		t.x = sx, t.y = sy;
		v[t.x][t.y] = 1;
		ans[t.x][t.y] = 0;
		q.push(t);
		bfs();
		for (int i = 1; i <= n; i++) {
			for (int j = 1; j <= m; j++) {
				cout<

你可能感兴趣的:(算法学习,算法)