NYOJ 21 图的广度优先搜索 倒水问题

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

思路:

算法竞赛入门经典中该题有思路分析,该题是隐式图的遍历,每次三个杯子中的状态可以看成一个结点,通过几种倒水的方式,推出下一层的结点,以此例推,可得出解。

这里需要解决的问题:

1,怎样解决倒水

A:通过模拟,由于没有刻度,每次倒水只能倒满,而且倒水的容量取决于容器的最大可容体积与已有水。

2,结点的构造

A:定义结构体,生成一种状态,构造一个节点

3,广度搜索

A,利用队列

Code One

#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
struct Glass
{
 int state[3];
 int step;
 Glass() { step=0;}
};
int cupMax[3],target[3];
bool visited[105][105][105];        //为了避免同样的状态造成时间及空间的浪费,需要进行剪枝,利用标记数组
void PourWater(int k,int i,Glass &Cup)     // i到k的倒水
{
 int Yield=cupMax[k]-Cup.state[k];
 if(Cup.state[i]>=Yield)
 {
  Cup.state[k]+=Yield;
  Cup.state[i]-=Yield;
 }
 else
 {
  Cup.state[k]+=Cup.state[i];
  Cup.state[i]=0;
 }
}
bool IsTrue(Glass t)
{
 if(t.state[0]==target[0]&&t.state[1]==target[1]&&t.state[2]==target[2])
 return true;
 return false;
}
int Bfs()
{
 int i,j,k;
 queue<Glass> Q;
 Glass Initial;
 Initial.state[0]=cupMax[0];
 Initial.state[1]=Initial.state[2]=0;
 Q.push(Initial);
 memset(visited,false,sizeof(visited));
 visited[Initial.state[0]][0][0]=true;
 while(!Q.empty())
 {
  Glass node=Q.front();
  Q.pop();
  if(IsTrue(node))
  return node.step;
  for(i=0;i<3;i++)
  {
   for(j=1;j<3;j++)
   {
    k=(i+j)%3;
    if(node.state[i]!=0&&node.state[k]<cupMax[k])
    {
      Glass newnode=node;
      PourWater(k,i,newnode);
      newnode.step=node.step+1;
      if(!visited[newnode.state[0]][newnode.state[1]][newnode.state[2]])
      {
      visited[newnode.state[0]][newnode.state[1]][newnode.state[2]]=true;
      Q.push(newnode);
      }
    }
   }
  }
 }
 return -1;
}
int main()
{
 int test;
 cin>>test;
 while(test--)
 {
  cin>>cupMax[0]>>cupMax[1]>>cupMax[2];
  cin>>target[0]>>target[1]>>target[2];
  cout<<Bfs()<<endl;
 }
// system("pause");
 return 0;
}

   
 Code Two

#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
using namespace std;
int cupMax[3],target[3];
struct Glass
{
 int state[3];
 int step;
 Glass() { step=0; }
};
bool visited[105][105][105];
int min(int a,int b) { return a>b?b:a;}
bool IsTrue(Glass t)
{
 if(t.state[0]==target[0]&&t.state[1]==target[1]&&t.state[2]==target[2])
 return true;
 return false;
}
int Bfs()
{
 memset(visited,false,sizeof(visited));
 Glass initial;
 initial.state[0]=cupMax[0];
 initial.state[1]=initial.state[2]=0;
 visited[initial.state[0]][0][0]=true;
 queue<Glass> Q;
 Q.push(initial);
 while(!Q.empty())
 {
  Glass node=Q.front();
  Q.pop();
  if(IsTrue(node))
  return node.step;

 //////////////////////分别模拟几种倒水方式
  // 0-1
  if(node.state[0]>0&&node.state[1]<cupMax[1])
  {
   Glass temp=node;
   int k=min(node.state[0],cupMax[1]-node.state[1]);
   temp.state[0]-=k;
   temp.state[1]+=k;
      temp.step=node.step+1;
   if(!visited[temp.state[0]][temp.state[1]][temp.state[2]])
   {
    visited[temp.state[0]][temp.state[1]][temp.state[2]]=true;
    Q.push(temp);
   }
  }
  // 0-2
  if(node.state[0]>0&&node.state[2]<cupMax[2])
  {
   Glass temp=node;
   int k=min(node.state[0],cupMax[2]-node.state[2]);
   temp.state[0]-=k;
   temp.state[2]+=k;
   temp.step=node.step+1;
   if(!visited[temp.state[0]][temp.state[1]][temp.state[2]])
   {
    visited[temp.state[0]][temp.state[1]][temp.state[2]]=true;
    Q.push(temp);
   }
  }
  //1 -0
  if(node.state[1]>0&&node.state[0]<cupMax[0])
  {
   Glass temp=node;
   int k=min(node.state[1],cupMax[0]-node.state[0]);
   temp.state[1]-=k;
   temp.state[0]+=k;
   temp.step=node.step+1;
   if(!visited[temp.state[0]][temp.state[1]][temp.state[2]])
   {
    visited[temp.state[0]][temp.state[1]][temp.state[2]]=true;
    Q.push(temp);
   }
  }
  // 1-2
  if(node.state[1]>0&&node.state[2]<cupMax[2])
  {
   Glass temp=node;
   int k=min(node.state[1],cupMax[2]-node.state[2]);
   temp.state[1]-=k;
   temp.state[2]+=k;
   temp.step=node.step+1;
   if(!visited[temp.state[0]][temp.state[1]][temp.state[2]])
   {
    visited[temp.state[0]][temp.state[1]][temp.state[2]]=true;
    Q.push(temp);
   }
  }
  //2-0
  if(node.state[2]>0&&node.state[0]<cupMax[0])
  {
   Glass temp=node;
   int k=min(node.state[2],cupMax[0]-node.state[0]);
   temp.state[2]-=k;
   temp.state[0]+=k;
   temp.step=node.step+1;
   if(!visited[temp.state[0]][temp.state[1]][temp.state[2]])
   {
    visited[temp.state[0]][temp.state[1]][temp.state[2]]=true;
    Q.push(temp);
   }
  }
  // 2-1
  if(node.state[2]>0&&node.state[1]<cupMax[1])
  {
   Glass temp=node;
   int k=min(node.state[2],cupMax[1]-node.state[1]);
   temp.state[2]-=k;
   temp.state[1]+=k;
   temp.step=node.step+1;
   if(!visited[temp.state[0]][temp.state[1]][temp.state[2]])
   {
    visited[temp.state[0]][temp.state[1]][temp.state[2]]=true;
    Q.push(temp);
   }
  }
 }
 return -1;
}
int main()
{
 int test;
 cin>>test;
 while(test--)
 {
  cin>>cupMax[0]>>cupMax[1]>>cupMax[2];
  cin>>target[0]>>target[1]>>target[2];
  cout<<Bfs()<<endl;
 }
// system("pause");
 return 0;
}
 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值