大佬能帮忙看看嘛,没有输出结果是咋回事呢
查看原帖
大佬能帮忙看看嘛,没有输出结果是咋回事呢
474401
qwe1471900575楼主2021/4/12 20:20
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
int arr[50][50];
int ddd[1050][1050];
struct node
{
	int x, y;
	int times;
}d1, d2;
queue<node>q;
int j[12][2] = { {2,1}, {2,-1} ,{1,2}, {1,-2}, {-2,1} ,{-2,-1} ,{-1,2} ,{-1,-2} ,{2,2}, {2,-2} ,{-2,2}, {-2,-2} };
int bfs(int x, int y)
{
	while (!q.empty())
	{
		node d2;
		d2 = q.front();
		q.pop();
		for (int i = 0; i < 12; i++)
		{
			node d3;
			d3.x = d2.x + j[i][0];
			d3.y = d2.y + j[i][1];
			if (d3.x > 0 && d3.x <= 20 && d3.y > 0 && d3.y <= 20 && ddd[d3.x][d3.y] == 0)
			{
				d3.times = d2.times + 1;
				if (d3.x == 1 && d3.y == 1 && ddd[d3.x][d3.y] == 0)
				{
					return d3.times;
				}
				else
				{
					ddd[d3.x][d3.y] == 1;
					q.push(d3);
				}
			}
		}
	}
}
int main()
{
	int a, b, c, d;
	cin >> a >> b >> c >> d;
	ddd[a][b] = 1;
	d1.x = a;
	d1.y = b;
	d1.times = 0;
	q.push(d1);
	cout << bfs(a, b) << endl;
	memset(ddd, 0, sizeof(ddd));
	ddd[c][d] = 1;
	while (!q.empty()) q.pop();
	cout << bfs(c, d);
	return 0;
}

2021/4/12 20:20
加载中...