求助
查看原帖
求助
798157
emo_male_god楼主2023/2/17 21:01

代码↓

#include <iostream>
#include <string.h>
#include <queue>

using namespace std;

struct abc_node
{
	int a, b, cnt;
	string str = "";
};
int a, b, n;

void print(abc_node x)
{
	printf("%d", x.cnt);
	int lens = x.str.length();
	for (int i = 1; i <= lens; i ++ )
	{
		printf(" %c", x.str[i]);
	}
	printf("\n");
}

void bfs()
{
	bool key[1010][1010];
	queue<abc_node> q;
	q.push({0, 0, 0, ""});
	while (!q.empty())
	{
		abc_node A = q.front();
		q.pop();
		if (A.b == n)
		{
			print(A);
			break;
		}
		
		for (int i = 1; i <= 6; i ++ )
		{
			abc_node B = A;
			B.cnt ++ ;
			if (i == 1 && B.a != a)
			{
				B.a = a;
				B.str += "1";
				if (!key[B.a][B.b]) q.push(B), key[B.a][B.b] = true;
			}
			else if (i == 2 && B.b != b)
			{
				B.b = b;
				B.str += "2";
				if (!key[B.a][B.b]) q.push(B), key[B.a][B.b] = true;
			}
			else if (i == 3 && B.a != 0)
			{
				B.a = 0;
				B.str += "3";
				if (!key[B.a][B.b]) q.push(B), key[B.a][B.b] = true;
			}
			else if (i == 4 && B.b != 0)
			{
				B.b = 0;
				B.str += "4";
				if (!key[B.a][B.b]) q.push(B), key[B.a][B.b] = true;
			}
			else if (i == 5 && B.a != a && B.b != 0)
			{
				int temp = min(a - B.a, B.b);
				B.a += temp;
				B.b -= temp;
				B.str += "5";
				if (!key[B.a][B.b]) q.push(B), key[B.a][B.b] = true;
			}
			else if (i == 6 && B.b != b && B.a != 0)
			{
				int temp = min(b - B.b, B.a);
				B.b += temp;
				B.a -= temp;
				B.str += "6";
				if (!key[B.a][B.b]) q.push(B), key[B.a][B.b] = true;
			}
		}
	}
	memset(key, false, sizeof key);
}

int main()
{
	int t;
	scanf("%d", &t);
	while (t -- )
	{
		scanf("%d%d%d", &a, &b, &n);
		bfs();
	}
	return 0;
}

全WA,但是时间和内存小

2023/2/17 21:01
加载中...