hdu 1495非常可乐 BFS

AC代码

#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
#include <algorithm>
using namespace std;
const int maxn=105;
bool v[4];
bool vis[maxn][maxn][maxn];
struct Node
{
    int v[4];
    int step;
} temp;
 
//倒水函数,把a杯子中的可乐倒到b杯子中
void pour(int a,int b)
{
    int sum=temp.v[a]+temp.v[b];
    if(sum>=v[b])
        temp.v[b]=v[b];
    else
        temp.v[b]=sum;
    temp.v[a]=sum-temp.v[b];
}
void BFS()
{
    queue<Node>q;
    Node now;
    now.v[0]=v[0];
    now.v[1]=0;
    now.v[2]=0;
    now.step=0;
    q.push(now);
    memset(vis,false,sizeof(vis));
    vis[v[0]][0][0]=true;
    while(!q.empty())
    {
        now=q.front();
        q.pop();
        if(now.v[0]==now.v[2]&&now.v[1]==0)
        {
            cout<<now.step<<endl;
            return;
        }
        for(int i=0; i<3; i++)
        {
            for(int j=0; j<3; j++)
            {
                if(i!=j)//自己不倒水给自己
                {
                    temp=now;
                    //每个水位情况都要把所有操作枚举一遍,
                    //所以都要赋值为原始水位情况
                    pour(i,j);
                    if(!vis[temp.v[0]][temp.v[1]][temp.v[2]])
                    {
                        temp.step++;
                        q.push(temp);
                        vis[temp.v[0]][temp.v[1]][temp.v[2]]=true;
                    }
                }
            }
        }
    }
    cout<<"NO"<<endl;
}
int main()
{
    while(scanf("%d%d%d",&v[0],&v[1],&v[2])!=EOF)
    {
        if(v[0]+v[1]+v[2]==0)
            break;
        if(v[1]>v[2])
            swap(v[1],v[2]);
        BFS();
    }
    return 0;
}

TLE ???为啥啊?不知道哪里不对,导致超时

#pragma warning(disable:4996)
#include<iostream>
#include<string>
#include<cmath>
#include<ctype.h>
#include<memory.h>
#include<string.h>
#include<algorithm>
#include<map>
#include<iomanip>
#include<set>
#include<list>
#include<vector>
#include<stack>
#include<queue>
#define ll long long int
using namespace std;
const int maxn = 110;
struct node
{
	int v[3];//0,1,2号, 三个杯子里的情况
	int step;
};
queue<node> q;
int v[3];//v[0]=s;v[1]=n;v[2]=m;
int vis[maxn][maxn][maxn];

void pour(node &x, int a, int b)//a号杯子里的水倒入b号杯子中
{
	int sum = x.v[a] + x.v[b];
	if (sum >= v[b])
		x.v[b] = v[b];
	else
		x.v[b] = sum;
	x.v[a] = sum - x.v[b];//总数减去变化后的v[b],即为v[a];
}
void bfs()
{
	while (!q.empty())q.pop();
	memset(vis, 0, sizeof(vis));
	node t;

	t.v[0] = v[0]; t.v[1] = 0; t.v[2] = 0; t.step = 0;

	q.push(t);
	vis[v[0]][0][0] = 1;

	while (!q.empty())
	{
		node now = q.front(); q.pop();
		if (now.v[0] == now.v[1] && now.v[2] == 0)//s=n+m. n!=m. 所以要想等分,,必须s杯子里的水和n,m二者里的一个容量较大的相等
		{
			cout << now.step << endl;
			return;
		}

		for (int i = 0; i < 3; i++)
		{
			for (int j = 0; j < 3; j++)
			{
				if (i != j)
				{
					node next = now;
					pour(next, i, j);

					if (!vis[next.v[0]][next.v[1]][next.v[2]])
					{
						next.step++;
						q.push(next);
						vis[next.v[0]][next.v[1]][next.v[2]]=1;
					}
				}
			}
		}
	}
	cout << "NO" << endl;
}

int main()
{
	while (cin >> v[0] >> v[1] >> v[2])
	{
		if (v[0] == 0 && v[1] == 0 && v[2] == 0)
			break;
		if (v[1] < v[2])
			swap(v[1], v[2]);

		bfs();
	}
	return 0;
}

对比发现,超时和输出使用cin等 无关,貌似是pour函数采用传参的方法就会超时,需要使用全局变量才不会超时 = = 。。。

#pragma warning(disable:4996)
#include<iostream>
#include<string>
#include<cmath>
#include<ctype.h>
#include<memory.h>
#include<string.h>
#include<algorithm>
#include<map>
#include<iomanip>
#include<set>
#include<list>
#include<vector>
#include<stack>
#include<queue>
#define ll long long int
using namespace std;
const int maxn = 110;
struct node
{
	int v[3];//0,1,2号, 三个杯子里的情况
	int step;
};
queue<node> q;
bool v[3];//v[0]=s;v[1]=n;v[2]=m;
bool vis[maxn][maxn][maxn];
node temp;
void pour(int a, int b)//a号杯子里的水倒入b号杯子中
{
	int sum = temp.v[a] + temp.v[b];
	if (sum >= v[b])
		temp.v[b] = v[b];
	else
		temp.v[b] = sum;
	temp.v[a] = sum - temp.v[b];//总数减去变化后的v[b],即为v[a];
}
//void pour(node x, int a, int b)//a号杯子里的水倒入b号杯子中
//{
//	int sum = x.v[a] + x.v[b];
//	if (sum >= v[b])
//		x.v[b] = v[b];
//	else
//		x.v[b] = sum;
//	x.v[a] = sum - x.v[b];//总数减去变化后的v[b],即为v[a];
//}
void bfs()
{
	while (!q.empty())q.pop();
	memset(vis, 0, sizeof(vis));
	node t;

	t.v[0] = v[0]; t.v[1] = 0; t.v[2] = 0; t.step = 0;

	q.push(t);
	vis[v[0]][0][0] = 1;

	while (!q.empty())
	{
		node now = q.front(); q.pop();
		if (now.v[0] == now.v[1] && now.v[2] == 0)//s=n+m. n!=m. 所以要想等分,,必须s杯子里的水和n,m二者里的一个容量较大的相等
		//if (now.v[0] == now.v[2] && now.v[1] == 0)
		{
			cout << now.step << endl;
			return;
		}

		for (int i = 0; i < 3; i++)
		{
			for (int j = 0; j < 3; j++)
			{
				if (i != j)
				{
					/*node next = now;
					pour(next, i, j);*/
					temp = now;
					pour(i, j);

					/*if (!vis[next.v[0]][next.v[1]][next.v[2]])
					{
						next.step++;
						q.push(next);
						vis[next.v[0]][next.v[1]][next.v[2]] = 1;
					}*/
					if (!vis[temp.v[0]][temp.v[1]][temp.v[2]])
					{
						temp.step++;
						q.push(temp);
						vis[temp.v[0]][temp.v[1]][temp.v[2]] = 1;
					}
				}
			}
		}
	}
	cout << "NO" << endl;
}

int main()
{
	//while (cin >> v[0] >> v[1] >> v[2])
	while (scanf("%d%d%d", &v[0], &v[1], &v[2]) != EOF)
	{
		if (v[0] == 0 && v[1] == 0 && v[2] == 0)
			break;
		if (v[1] < v[2])
			swap(v[1], v[2]);
		/*if (v[1] > v[2])
			swap(v[1], v[2]);*/

		bfs();
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值