求助大佬,帮忙看一下啊!
查看原帖
求助大佬,帮忙看一下啊!
473710
hmr26108楼主2022/2/27 16:05
#include<bits/stdc++.h>
using namespace std;
const int MAXN=2000;
struct node{
	int x,y;
	string op;
};
int a,b,c,rear,front,ans;
int vis[MAXN][MAXN];
int d[MAXN];
void print(string op)
{
	cout<<op.length()-1<<" ";
	for(int i=1;i<=op.length();i++)
	{
		switch (op[i])
		{
			case '1':
				cout<<1<<" ";
				break;
			case '2':
				cout<<2<<" ";
				break;
			case '3':
				cout<<3<<" ";
				break;
			case '4':
				cout<<4<<" ";
				break;
			case '5':
				cout<<6<<" ";
				break;
			case '6':
				cout<<5<<" ";
				break;				
		}
	}
	cout<<endl;
}
void bfs()
{
    queue<node>q;
	q.push({0,0," "});
	while(!q.empty()){
		node u=q.front();
		int x=u.x,y=u.y;
		string op=u.op;
		q.pop();
		if(x<a&&!vis[a][y]){
			vis[a][y]=1;
			q.push({a,y,op+"1"});
			}
		if(y<b&&!vis[x][b]){
			if(y==c) {
				op+="2";
				print(op);
				return ;
			}
			vis[x][b]=1;
			q.push({x,b,op+"2"});
		}
		if(x>0&&!vis[0][y]){
			vis[0][y]=1;
			q.push({0,y,op+"3"});
			}
		if(y>0&&!vis[x][0]){
			vis[x][0]=1;
			q.push({x,0,op+"4"});
			}
		if(x>0 && y<b)
		{
			if(b-y>=x&&!vis[0][x+y])
			{
				if(x+y==c)
				{
					op+="5";
					print(op);
					return ;
				}
					vis[0][x+y]=1;
					q.push({0,x+y,op+"5"});
				} 
			if(b-y<=x&&!vis[x-b+y][b])
				{
				vis[x-b+y][b]=1;
				q.push({x-b+y,b,op+"5"});
					
				}
		}
		if(y>0 && x<a)
		{
			if(a-x>=y&&!vis[x+y][0])
			{
				vis[x+y][0]=1;
				q.push({x+y,0,op+"6"});
				
				} 
			if(a-x<=y&&!vis[a][y-a+x])
				{
					if(y-a+x==c)
					{
						op+="6";
						print(op);
						return ;
					}
					vis[a][y-a+x]=1;
					q.push({a,y-a+x,op+"6"});
					
				}
			}
		}
	}

int main()
{
	int t;
	scanf("%d",&t);
	for(int i=1;i<=t;i++){
		scanf("%d%d%d",&a,&b,&c);
		memset(vis,0,sizeof(vis));
		bfs();
	}
	return 0;
	
}

第一个点wa,调了很久都没看出来 提交记录

2022/2/27 16:05
加载中...