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;
}
 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
孪生素数是指两个素数之间的差值为2的素数对。通过筛选法可以找出给定素数范围内的所有孪生素数的组数。 在引用的代码中,使用了递归筛选法来解决孪生素数问题。该程序首先使用循环将素数的倍数标记为非素数,然后再遍历素数数组,找出相邻素数之间差值为2的素数对,并统计总数。 具体实现过程如下: 1. 定义一个数组a[N,用来标记数字是否为素数,其中N为素数范围的上限。 2. 初始化数组a,将0和1标记为非素数。 3. 输入要查询的孪生素数的个数n。 4. 循环n次,每次读入一个要查询的素数范围num。 5. 使用两层循环,外层循环从2遍历到num/2,内层循环从i的平方开始,将素数的倍数标记为非素数。 6. 再次循环遍历素数数组,找出相邻素数之间差值为2的素数对,并统计总数。 7. 输出总数。 至此,我们可以使用这个筛选法的程序来解决孪生素数问题。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [python用递归筛选法求N以内的孪生质数(孪生素数)](https://blog.csdn.net/weixin_39734646/article/details/110990629)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [NYOJ-26 孪生素数问题](https://blog.csdn.net/memoryofyck/article/details/52059096)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值