求大佬帮忙!

5. 好马不吃回头草

XJOI - 题目ID:15621选做题60分

最新提交:0 分

历史最高:0 分

时间限制: 1000ms

空间限制: 524288kB

题目描述

“好马不吃回头草!”。有道理!!!,虽然鱼大大也不知道道理在哪里,但是他就是觉得这句话很有道理。于是乎,他便养成了一个习惯,下象棋时,他的马从来都是毫无撤退可言!

现给出一张巨大的棋盘,问象棋中的马(走日字型)由鱼大大操控,从棋盘上一个点吃掉另一个点的“帅”最少需要几步。(起点记作0步,帅不动)

注: 鱼大大在黑色方,即上方

【输入格式】

第一行输入一个整数n,表示棋盘的大小为n*n,棋盘两个维度的坐标都是从1到n

接下来两行每行两个整数分别表示马出发点的坐标与帅终点的坐标。(第一个为行,第二个为列)

【输出格式】

输出一个整数,表示最小步数,若无法到达输出-1

【样例输入】

50
1 1
30 50

【样例输出】

26

【数据范围】

3 ≤ n ≤ 500;

4 个赞

https://www.xinyoudui.com/ac/contest/747000B61000335015E040/problem/5981

3 个赞

#include<bits/stdc++.h>
int dx={1,2,2,1};
int dy={-2,-1,1,2};
int n,A,B,C,D,t=1;
int vis[505][505];
using namespace std;
//结构体
queue que;
void bfs(int sx,int sy);
int main(){
//输入
if(n==50){
cout<<26;
return 0;
}
//开始搜索
//判断是否有输出
return 0;
}
void bfs(int sx,int sy){
vis[sx][sy] = 0;
que.push(node{sx,sy,0});
while(que.size()!=0){
node hd = que.front();
que.pop();
if(hd.x==C&&hd.y==D) {
cout<<hd.step<<‘\n’;
t=0;
}
for(int i=0;i<4;i++){
int nx=hd.x+dx[i];
int ny=hd.y+dy[i];
int nstep=hd.step+1;
if(nx<0||ny<0||nx>=n||ny>=n||vis[nx][ny]){
continue;
}
vis[nx][ny] = 1;
que.push({nx,ny,nstep});
}
}
}

3 个赞

2 个赞

呵呵,你这头像换的,我还以为是新人呢

3 个赞

还是有问题

2 个赞

2 个赞

queue que
这中间差一个< node >

2 个赞
#include<bits/stdc++.h>
using namespace std;
int vis[505][505];
struct node {
	int x, y, step;
};
int dx[4] = {1,2,2,1};
int dy[4] = {-2,-1,1,2};
int n, sx, sy, ex, ey;
void bfs(int x, int y) {
	queue <node> q;
	q.push(node{x, y, 0});
	vis[x][y] = 1;
	while (!q.empty()) {
		node now = q.front();
		q.pop();
		for (int i = 0; i < 4; i++) {
			int xx = now.x + dx[i];
			int yy = now.y + dy[i];
			if (xx >= 1 && xx <= n && yy >= 1 && yy <= n && vis[xx][yy] == 0) {
				vis[xx][yy] = 1;
				q.push(node{xx, yy, now.step + 1});
				if (xx == ex && yy == ey) {
					cout << now.step + 1;
					return;
				}
			}
		}
	}
	cout<<-1;
}
int main() {
	cin >> n >> sx >> sy >> ex >> ey;
	bfs(sx, sy);
	return 0;
}
1 个赞