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;
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];
int step;
};
queue<node> q;
int v[3];
int vis[maxn][maxn][maxn];
void pour(node &x, int a, int 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];
}
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)
{
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];
int step;
};
queue<node> q;
bool v[3];
bool vis[maxn][maxn][maxn];
node temp;
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()
{
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)
{
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]] = 1;
}
}
}
}
}
cout << "NO" << endl;
}
int main()
{
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]);
bfs();
}
return 0;
}