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

该问题描述了一个abKnight在特定大小的棋盘上移动的规则,并要求计算从初始位置到棋盘上其他所有位置的最少跳跃数。给定棋盘的行数和列数,以及abKnight的跳跃参数a和b,程序使用广度优先搜索(BFS)来找到每个位置的最短路径。对于无法到达的位置,输出-1。
摘要由CSDN通过智能技术生成

目录

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 <iostream>
#include <cstring>
#include <queue>
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 <node> 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<<ans[i][j];
				if (j == m) cout<<endl;
				else cout<<" ";
			}
		}
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值