您的位置:首页 > 其它

Nyoj 21 三个水杯

2014-04-03 21:06 507 查看
题目来源:http://acm.nyist.net/JudgeOnline/problem.php?pid=21

用搜索来模拟每一种状态,直到满足目标状态。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
#include <cmath>
#include <algorithm>

using namespace std;

typedef struct State_News State;

struct State_News
{
int a, b, c;//每种状态中每个水杯中的水量
int step;
State_News()
{
a = 0;
b = 0;
c = 0;
step = 0;
}
};

int v1, v2, v3;//表示三个水杯的容量
bool Is_Visit[110][110][110];//记录每个状态是否到达过
State TargetState;//目标状态

int BFS(State CurState)//搜索所有倒水状态中每一种状态,看是否满足目标状态100 * 100 * 100 = 1000000种情况,可以一一枚举出
{
queue <State> Que;
while(!Que.empty())
Que.pop();
Que.push(CurState);
while(!Que.empty())
{
State CurState_News = Que.front();
Que.pop();

Is_Visit[CurState_News.a][CurState_News.b][CurState_News.c] = true;

if(CurState_News.a == TargetState.a && CurState_News.b == TargetState.b && CurState_News.c == TargetState.c)
return CurState_News.step;

if(CurState_News.a > 0 && CurState_News.b < v2)//v1->v2 表示从v1中向v2中倒水
{
State temp = CurState_News;
int tmp = min(temp.a, v2 - temp.b);
temp.a -= tmp;
temp.b += tmp;
if(Is_Visit[temp.a][temp.b][temp.c] == false)
{
Is_Visit[temp.a][temp.b][temp.c] = true;
temp.step++; //这一句的等价于temp.step = CurState_News.step + 1;改变了水量step但是还是前一种状态中的step,因此自加1即可
Que.push(temp);
}
}

if(CurState_News.a > 0 && CurState_News.c < v3)//v1->v3
{
State temp = CurState_News;
int tmp = min(temp.a, v3 - temp.c);
temp.a -= tmp;
temp.c += tmp;
if(Is_Visit[temp.a][temp.b][temp.c] == false)
{
Is_Visit[temp.a][temp.b][temp.c] = true;
temp.step++;
Que.push( temp );
}
}

if(CurState_News.b > 0 && CurState_News.a < v1)//v2->v1
{
State temp = CurState_News;
int tmp = min(temp.b, v1 - temp.a);
temp.b -= tmp;
temp.a += tmp;
if(Is_Visit[temp.a][temp.b][temp.c] == false)
{
Is_Visit[temp.a][temp.b][temp.c] = true;
temp.step++;
Que.push( temp );
}
}
if(CurState_News.b > 0 && CurState_News.c < v3)//v2->v3
{
State temp = CurState_News;
int tmp = min(temp.b, v3 - temp.c);
temp.b -= tmp;
temp.c += tmp;
if(!Is_Visit[temp.a][temp.b][temp.c])
{
Is_Visit[temp.a][temp.b][temp.c] = true;
temp.step++;
Que.push( temp );
}
}

if(CurState_News.c > 0 && CurState_News.a < v1)//v3 -> v1
{
State temp = CurState_News;
int tmp = min(temp.c, v1 - temp.a);
temp.c -= tmp;
temp.a += tmp;
if(!Is_Visit[temp.a][temp.b][temp.c])
{
Is_Visit[temp.a][temp.b][temp.c] = true;
temp.step++;
Que.push( temp );
}
}
if(CurState_News.c > 0 && CurState_News.b < v2)//v3 -> v2
{
State temp = CurState_News;
int tmp = min(temp.c, v2-temp.b);
temp.b += tmp;
temp.c -= tmp;
if(!Is_Visit[temp.a][temp.b][temp.c])
{
Is_Visit[temp.a][temp.b][temp.c] = true;
temp.step++;
Que.push( temp );
}
}
}
return -1;
}

int main()
{
State InitialState;//初始状态
int T;
scanf("%d", &T);
while(T--)
{
memset(Is_Visit, 0, sizeof(Is_Visit));
scanf("%d %d %d", &v1, &v2, &v3);
InitialState.a = v1, InitialState.b = 0, InitialState.c = 0, InitialState.step = 0;
scanf("%d %d %d", &TargetState.a, &TargetState.b, &TargetState.c);
if(v1 < TargetState.a + TargetState.b + TargetState.c)
{
printf("-1\n");
continue ;
}
printf("%d\n", BFS( InitialState ));
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: