NYOJ-21 三个水杯【隐式图搜索】

题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=21

解题思路:

隐式图搜索。我们需要对广搜到基本框架了解。

因为范围为100,最多有100×100×100=1000000种情况,所以可以用广搜暴力来枚举每一种状态。

这个图是一个有向图,和SPFA不同,如果一个状态已经出现过,就不需要再考虑了。但是SPFA中,一个状态可能通过不同的路径到达,而不同的路径的距离也不同,这样,别的路径改变后可能会改变当前的状态,所以对每个节点来说,可能需要多次进入队列中。而水杯状态不受影响,故不考虑重复情况。


代码如下:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<queue>
#include<algorithm>
#include<cstdlib>
using namespace std;

int total;
int v1, v2, v3;
bool visit[105][105][105]; //状态是否出现

struct state
{
	int a, b, c;
	int ceng; //最小步数
	state(){
		a = b = c = ceng = 0;
	}
}b, e;

int BFS(state begin)
{
	queue<state> q;
	while(!q.empty())
		q.pop();
	q.push(b);
	while(!q.empty())
	{
		state cur = q.front(); 
		q.pop();
		visit[cur.a][cur.b][cur.c] = true;

		if(cur.a == e.a && cur.b == e.b && cur.c == e.c) //找到
			return cur.ceng;

		if(cur.a > 0 && cur.b < v2)					//v1->v2
		{
			state temp = cur;
			int tt = min(temp.a, v2 - temp.b);
			temp.a -= tt;
			temp.b += tt;
			if(visit[temp.a][temp.b][temp.c] == false)
			{
				visit[temp.a][temp.b][temp.c] = true;
				temp.ceng++;
				q.push(temp);
			}
		}

		if(cur.a > 0 && cur.c < v3)					//v1->v3
		{
			state temp = cur;
			int tt = min(temp.a, v3 - temp.c);
			temp.a -= tt;
			temp.c += tt;
			if(visit[temp.a][temp.b][temp.c] == false)
			{
				visit[temp.a][temp.b][temp.c] = true;
				temp.ceng++;
				q.push(temp);
			}
		}

		if(cur.b > 0 && cur.a < v1)					//v2->v1
		{
			state temp = cur;
			int tt = min(temp.b, v1 - temp.a);
			temp.a += tt;
			temp.b -= tt;
			if(visit[temp.a][temp.b][temp.c] == false)
			{
				visit[temp.a][temp.b][temp.c] = true;
				temp.ceng++;
				q.push(temp);
			}
		}

		if(cur.b > 0 && cur.c < v3)					//v2->v3
		{
			state temp = cur;
			int tt = min(temp.b, v3 - temp.c);
			temp.b -= tt;
			temp.c += tt;
			if(visit[temp.a][temp.b][temp.c] == false)
			{
				visit[temp.a][temp.b][temp.c] = true;
				temp.ceng++;
				q.push(temp);
			}
		}

		if(cur.c > 0 && cur.a < v1)					//v3->v1
		{
			state temp = cur;
			int tt = min(temp.c, v1 - temp.a);
			temp.c -= tt;
			temp.a += tt;
			if(visit[temp.a][temp.b][temp.c] == false)
			{
				visit[temp.a][temp.b][temp.c] = true;
				temp.ceng++;
				q.push(temp);
			}
		}

		if(cur.c > 0 && cur.b < v2)					//v3->v2
		{
			state temp = cur;
			int tt = min(temp.c, v2 - temp.b);
			temp.c -= tt;
			temp.b += tt;
			if(visit[temp.a][temp.b][temp.c] == false)
			{
				visit[temp.a][temp.b][temp.c] = true;
				temp.ceng++;
				q.push(temp);
			}
		}
	}
	return -1; //没有终状态
}

int main()
{
	int ncase;
	scanf("%d", &ncase);
	while(ncase--)
	{
		total = 0;
		memset(visit, false, sizeof(visit));
		scanf("%d %d %d", &v1, &v2, &v3);
		b.a = v1, b.b = 0, b.c = 0, b.ceng = 0;
		scanf("%d %d %d", &e.a, &e.b, &e.c);
		if(v1 < e.a + e.b + e.c)
		{
			printf("-1\n");
			continue;
		}
		else
			printf("%d\n", BFS(b));
	}
	return 0;
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值