站外题求助
  • 板块题目总版
  • 楼主osfly
  • 当前回复0
  • 已保存回复0
  • 发布时间2021/7/6 19:36
  • 上次更新2023/11/4 18:31:48
查看原帖
站外题求助
339299
osfly楼主2021/7/6 19:36

传送门

#include<cstdio>
#include<queue>
using namespace std;
int a,b,c,d;
bool vis[100][100][100];
struct Now
{
	int s,m,l,step;
}now;
queue<Now> q;
int bfs()
{
	int s,m,l,step;
	now.s=c;
	now.m=b;
	now.l=a;
	now.step=0;
	q.push(now);
	while(!q.empty())
	{
		now=q.front();
		q.pop();
		//
		if(s==d||m==d||l==d) return step;
		//s->m
		if(s>0&&m<b)
		{
			s=now.s;
			m=now.m;
			l=now.l;
			step=now.step+1;
			m+=s;
			s=0;
			if(m>b)
			{
				s+=(m-b);
				m=b;
			}
			if(!vis[s][m][l])
			{
				vis[s][m][l]=1;
				now.s=s;
				now.m=m;
				now.l=l;
				now.step=step;
				q.push(now);
			}
		}
		//m->s
		if(m>0&&s<c)
		{
			s=now.s;
			m=now.m;
			l=now.l;
			step=now.step+1;
			s+=m;
			m=0;
			if(s>c)
			{
				m+=(s-c);
				s=c;
			}
			if(!vis[s][m][l])
			{
				vis[s][m][l]=1;
				now.s=s;
				now.m=m;
				now.l=l;
				now.step=step;
				q.push(now);
			}
		}
		//s->l
		if(s>0&&l<a)
		{
			s=now.s;
			m=now.m;
			l=now.l;
			step=now.step+1;
			l+=s;
			s=0;
			if(l>a)
			{
				s+=(l-a);
				l=a;
			}
			if(!vis[s][m][l])
			{
				vis[s][m][l]=1;
				now.s=s;
				now.m=m;
				now.l=l;
				now.step=step;
				q.push(now);
			}
		}
		//l->s
		if(l>0&&s<c)
		{
			s=now.s;
			m=now.m;
			l=now.l;
			step=now.step+1;
			s+=l;
			l=0;
			if(s>c)
			{
				l+=(s-c);
				s=c;
			}
			if(!vis[s][m][l])
			{
				vis[s][m][l]=1;
				now.s=s;
				now.m=m;
				now.l=l;
				now.step=step;
				q.push(now);
			}
		}
		//m->l
		if(m>0&&l<a)
		{
			s=now.s;
			m=now.m;
			l=now.l;
			step=now.step+1;
			l+=m;
			m=0;
			if(l>a)
			{
				m+=(l-a);
				l=a;
			}
			if(!vis[s][m][l])
			{
				vis[s][m][l]=1;
				now.s=s;
				now.m=m;
				now.l=l;
				now.step=step;
				q.push(now);
			}
		}
		//l->m
		if(l>0&&m<b)
		{
			s=now.s;
			m=now.m;
			l=now.l;
			step=now.step+1;
			m+=l;
			l=0;
			if(m>b)
			{
				l+=(m-b);
				m=b;
			}
			if(!vis[s][m][l])
			{
				vis[s][m][l]=1;
				now.s=s;
				now.m=m;
				now.l=l;
				now.step=step;
				q.push(now);
			}
		}
	}
	return -1;
}
int main()
{
	scanf("%d%d%d%d",&a,&b,&c,&d);
	if(bfs()==-1) printf("No find");
	else printf("%d",bfs());
	return 0; 
}

样例都过不了,求助!

2021/7/6 19:36
加载中...