A - Jugs ZOJ - 1005 (模拟)

题目链接:https://cn.vjudge.net/contest/281037#problem/A

题目大意:给你a,b,n。a代表第一个杯子的容量,b代表第二个杯子的容量,然后一共有6种操作。让你用尽可能少的步骤将第二个杯子的当前的水的体积转换成n。

具体思路:就是队列模拟啊,,,,打比赛的时候脑子瓦特了,没读完题目就开始读了,,,这个毛病确实得改了,,

AC代码:

  1 #include<iostream>
  2 #include<stack>
  3 #include<iomanip>
  4 #include<cmath>
  5 #include<stdio.h>
  6 #include<cstring>
  7 #include<algorithm>
  8 #include<queue>
  9 #include<vector>
 10 using namespace std;
 11 # define ll long long
 12 const int maxn = 2e6+100;
 13 int head[maxn];
 14 struct node
 15 {
 16     int op;
 17     int a;
 18     int b;
 19 } q[maxn];
 20 int ans[maxn],flag=0;
 21 int vis[1000+100][1000+100];
 22 void print(int t)
 23 {
 24     if(t>1)
 25     {
 26         print(head[t]);
 27         ans[++flag]=q[t].op;
 28     }
 29 }
 30 void cal(int t1,int t2,int g)
 31 {
 32     head[0]=-1;
 33     int l=0,r=1,tt;
 34     int num=0;
 35     q[1].a=0,q[1].b=0,q[1].op=0;
 36     vis[0][0]=1;
 37     while(l<=r)
 38     {
 39         int tmp=l;
 40         vis[q[tmp].a][q[tmp].b]=1;
 41         l++;
 42         if(vis[t1][q[tmp].b]==0){
 43         head[++r]=tmp;
 44         q[r].a=t1;
 45         q[r].b=q[tmp].b;
 46         q[r].op=1;
 47         if(q[r].b==g)
 48         {
 49             print(r);
 50             return ;
 51         }
 52         }
 53         if(vis[q[tmp].a][t2]==0)
 54     {
 55         head[++r]
 56                 =tmp;
 57             q[r].a=q[tmp].a;
 58             q[r].b=t2;
 59             q[r].op=2;
 60             if(q[r].b==g)
 61             {
 62                 print(r);
 63                 return ;
 64             }
 65         }
 66         if(vis[0][q[tmp].b]==0)
 67     {
 68         head[++r]
 69                 =tmp;
 70             q[r].a=0;
 71             q[r].b=q[tmp].b;
 72             q[r].op=3;
 73             if(q[r].b==g)
 74             {
 75                 print(r);
 76                 return ;
 77             }
 78         }
 79         if(vis[q[tmp].a][0]==0)
 80     {
 81         head[++r]
 82                 =tmp;
 83             q[r].a=q[tmp].a;
 84             q[r].b=0;
 85             q[r].op=4;
 86             if(q[r].b==g)
 87             {
 88                 print(r);
 89                 return ;
 90             }
 91         }
 92         int s1=q[tmp].a-min(q[tmp].a,t2-q[tmp].b);
 93                int s2=q[tmp].b+q[tmp].a-s1;
 94                if(vis[s1][s2]==0)
 95     {
 96         head[++r]
 97                 =tmp;
 98             q[r].a=s1;
 99             q[r].b=s2;
100             q[r].op=5;
101             if(q[r].b==g)
102             {
103                 print(r);
104                 return ;
105             }
106         }
107         s1=q[tmp].b-min(q[tmp].b,t1-q[tmp].a);
108            s2=q[tmp].a+q[tmp].b-s1;
109            if(vis[s1][s2]==0)
110     {
111         head[++r]
112                 =tmp;
113             tt=q[tmp].b;
114             q[r].b=s1;
115             q[r].a=s2;
116             q[r].op=6;
117             if(q[r].b==g)
118             {
119                 print(r);
120                 return ;
121             }
122         }
123     }
124 }
125 int main()
126 {
127     int t1,t2,n;
128     while(~scanf("%d %d %d",&t1,&t2,&n))
129     {
130         memset(vis,0,sizeof(vis));
131         flag=0;
132         cal(t1,t2,n);
133         for(int i=1; i<=flag; i++)
134         {
135             if(ans[i]==1)
136             {
137                 printf("fill A\n");
138             }
139             else if(ans[i]==2)
140             {
141                 printf("fill B\n");
142             }
143             else if(ans[i]==3)
144             {
145                 printf("empty A\n");
146             }
147             else if(ans[i]==4)
148             {
149                 printf("empty B\n");
150             }
151             else if(ans[i]==5)
152             {
153                 printf("pour A B\n");
154             }
155             else if(ans[i]==6)
156             {
157                 printf("pour B A\n");
158             }
159         }
160         printf("success\n");
161     }
162     // }
163     return 0;
164 }

原文地址:https://www.cnblogs.com/letlifestop/p/10320343.html

时间: 2024-11-05 12:14:50

A - Jugs ZOJ - 1005 (模拟)的相关文章

ZOJ 1005 Jugs

Jugs Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge In the movie "Die Hard 3", Bruce Willis and Samuel L. Jackson were confronted with the following puzzle. They were given a 3-gallon jug and a 5-gallon jug and were asked t

ZOJ 3674 模拟

[题意]:若干组数据 每组给一个整数n代表n个名词(单词),接下来1-n给出一个单词和一行注释(一行注释由多个字符串组成),然后给出一个整数m,接下来1-m 每行若干个单词来自(1-n),要求出这若干个单词共有的注释字符串并按字典序排列输出,若不存在则输出NO. Sample Input 4 fish agile animal horse swift animal eagle fierce animal Kyuubee alien incubator 2 fish horse eagle fis

zoj 3627#模拟#枚举

Treasure Hunt II Time Limit: 2 Seconds                                     Memory Limit: 65536 KB There are n cities(1, 2, ... ,n) forming a line on the wonderland. city i and city i+1 are adjacent and their distance is 1. Each city has many gold coi

ZOJ 3829 模拟贪心

2014牡丹江现场赛水题 给出波兰式,判断其是否合法,如果不合法有两种操作: 1:任意位置加一个数字或者操作符 2:任意两个位置的元素对调 贪心模拟即可 先判断数字数是否大于操作符数,若不大于 ans+=sum2-sum1+1:新加入的数字全部放到左端. 然后从左到右遍历一遍,存储到当前位置为止,数字数和sum1,和操作数和sum2 若sum2>=1sum1,优先与队尾的数字对调,若没有则sum1++,表示在最左端加一个数字 #include "stdio.h" #include

组队赛#1 解题总结 ZOJ 3803 YY&#39;s Minions (DFS搜索+模拟)

YY's Minions Time Limit: 2 Seconds      Memory Limit: 65536 KB Despite YY's so much homework, she would like to take some time to play with her minions first. YY lines her minions up to an N*M matrix. Every minion has two statuses: awake or asleep. W

poj 1005:I Think I Need a Houseboat(水题,模拟)

I Think I Need a Houseboat Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 85149   Accepted: 36857 Description Fred Mapper is considering purchasing some land in Louisiana to build his house on. In the process of investigating the land,

HDU 1986 &amp; ZOJ 2989 Encoding(模拟)

题目链接: HDU: http://acm.hdu.edu.cn/showproblem.php?pid=1986 ZOJ: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1988 HDU 1987 & ZOJ 2990 和这题刚好相反,也是比较容易模拟: Chip and Dale have devised an encryption method to hide their (written) text messages

[ACM] ZOJ 3844 Easy Task (模拟+哈希)

Easy Task Time Limit: 2 Seconds      Memory Limit: 65536 KB You are given n integers. Your task is very easy. You should find the maximum integer a and the minimum integer b among these n integers. And then you should replace both a and bwith a-b. Yo

ZOJ 3804 YY&#39;s Minions (简单模拟)

1 /* 2 题意:一个矩阵中有 n*m个宠物,每一个宠物都有一个状态, 1醒着的,0睡着的 3 X离开的!如果这个宠物(醒着的)的周围醒着的个数>3 || <2它就会睡着, 4 如果这个宠物(睡着的)的周围醒着的个数==3就会醒来! 5 每一分钟都会有变换一个状态! 6 其中会有些宠物会在给定的时间内离开! 7 */ 8 #include<iostream> 9 #include<cstring> 10 #include<cstdio> 11 #inclu