HDU1495-非常可乐
继续刷邝斌飞搜索专题
HDOJ1495 1s 32768K
ACGO A8044 1s 128M
用脚丫子想都知道肯定不用非得是整数,比如俩杯子刻度都是1.5,S是3肯定也可以
但这题能不能对着瓶子喝啊
比如:瓶子S总共如果20,N容积为10,M容积为6
step1:S给N满上
step2:S给M满上,现在S剩4,N装着10,M装着6
step3:N一口闷10,M一口闷6,S再剩的4全部给M
或者
step1:S给M满上10,S剩10
step2:N对着瓶吹
再看样例发现可以对着瓶子吹
S是4,杯子为1和3,4给3满上,3再给1满上,此时两步操作后,瓶子S剩1,容积为3的里有2,容积为1的杯子装满了,样例的答案是3步,那就应该是,再来一个操作容积为1的杯子都给S
一开始没头绪,想的是仨杯,S先给N(M同理),然后S再给M,或者N给M后S再给N,去想总共有几种可能性,怎么都想不下去了
往BFS靠,走一步能到哪,都压入队列,走两步能到哪......
就类似大和尚想做事,分成n小问题给n个小和尚,n个小和尚又分解问题给下面的小小和尚,但把自己当成大和尚小和尚绕就很麻烦,写程序让它自己去跑就很简单。
总共仨东西,就是互相给,抛除没意义操作,比如第n步是从S到N,那第n+1步就把所有互相给的方案里抛除N到S(相当于剪枝),其他都是有用的,还有就是不用记录vis是否访问,因为就样例来说,就是来回瞎JB给,简称XJBG算法,再复杂点人脑也想不出来,嘎嘎简单不多逼逼,先按照int写,样例过了再改浮点,写代码去了
1 瞎鸡巴搞,之前打CF有群友网不好让我给他发题目我还天真的问人家啥是XJBG算法 2 洛谷那些人的代码abcx看的真烦,数一巨巨也说代码要写的便于维护 3 搜索比数论简单太多了,之前单单一个素数筛就给我眼睛看瞎了,脑子看小脑萎缩了 4 题刷上瘾了,爱的复仇也不追了,管子也不撸了 5 没必要读别人代码,根本看不懂。因为自己写也是有大概思路东西边写边在上面加声明边定义新东西的,根本不是顺序往下写的,所以顺序读代码怎么可能读懂 6 生物班主任:zbl回家到底学不学习啊 7 8 v8;不要怕想法离谱,有思路就写 9 现在锻炼的感觉代码能力提高了,敢写了,心态也平静了,之前写题贼慌张,一深度思考就慌张,古树旋律也有关系 10 11 小时候妈妈找严老师 12 大学之前他们管老师不管松叫好老师 13 如今自己主见,这hdoj数据严格不水,就是好oj
代码不难也不复杂,就是有点绕脑子,写了好几个小时md
1 #include<stdio.h> 2 #include<iostream> 3 using namespace std; 4 int S; 5 int N; 6 int M; 7 #include<queue> 8 struct Situation{ 9 int now_volume_of_N;//此时装了装多少可乐 10 int now_volume_of_M; 11 int now_volume_of_S; 12 int step; 13 int who_to_who; 14 // -1:还没美开始走 15 // 0:S→N 16 // 1:S→M 17 // 2:N→S 18 // 3:N→M 19 // 4:M→N 20 // 5:M→S 21 }; 22 Situation situation; 23 queue<Situation>q; 24 int flag=0; 25 int main() 26 { 27 while(cin>>S>>N>>M&&(N+M+S)){ 28 flag=0; 29 situation.now_volume_of_N=0; 30 situation.now_volume_of_M=0; 31 situation.now_volume_of_S=S; 32 situation.step=0; 33 situation.who_to_who=-1; 34 q.push(situation); 35 while(!q.empty()){ 36 Situation this_situation; 37 this_situation=q.front(); 38 39 if((this_situation.step!=0)&&(this_situation.now_volume_of_M==this_situation.now_volume_of_N 40 || this_situation.now_volume_of_M==this_situation.now_volume_of_S 41 || this_situation.now_volume_of_S==this_situation.now_volume_of_N))//主要是为了过滤掉第一步,因为step==0的时候。俩杯子相等但都是0,没意义 42 { 43 cout<<"#"<<this_situation.step<<endl; 44 flag=1; 45 break; 46 } 47 48 Situation next_situation; 49 if(this_situation.who_to_who==2) 50 ;//我想S→N,但上一步如果是N→S就别走了,白折腾,剪枝 51 else{//S→N,首先S得有,有!要么对方剩余空间比我现在装的可乐体积大则全倒,要么我目前装的可乐体积大,以对方剩余空间为准给对方满上,相等情况,咋都行 52 if(this_situation.now_volume_of_S==0) 53 ; 54 else if(this_situation.now_volume_of_S >= N-this_situation.now_volume_of_N){ 55 next_situation.now_volume_of_S = this_situation.now_volume_of_S - (N-this_situation.now_volume_of_N); 56 // 这句话没必要看,因为思路很简单就是S如果现有的体积更大,就以对方为准去倒,但代码的加加减减看着很乱 57 next_situation.now_volume_of_N = N; 58 next_situation.now_volume_of_M = this_situation.now_volume_of_M; 59 next_situation.step=this_situation.step+1; 60 next_situation.who_to_who=0; 61 q.push(next_situation); 62 } 63 else if(this_situation.now_volume_of_S < N-this_situation.now_volume_of_N){//S现在有的可乐比对方还能装的空间小,那直接全给对方 64 next_situation.now_volume_of_N=this_situation.now_volume_of_N+this_situation.now_volume_of_S; 65 next_situation.now_volume_of_S=0; 66 next_situation.now_volume_of_M=this_situation.now_volume_of_M; 67 next_situation.step=this_situation.step+1; 68 next_situation.who_to_who=0; 69 q.push(next_situation); 70 } 71 } 72 73 if(this_situation.who_to_who==5) 74 ;//S→M,但上一步是M→S就别倒了 75 else{ 76 if(this_situation.now_volume_of_S==0) 77 ; 78 else if(this_situation.now_volume_of_S >= M-this_situation.now_volume_of_M){ 79 next_situation.now_volume_of_S = this_situation.now_volume_of_S - (M-this_situation.now_volume_of_M); 80 //这句话没必要看,因为思路很简单就是S如果现有的体积更大,就以对方为准去倒,但代码的加加减减看着很乱, 81 next_situation.now_volume_of_M = M; 82 next_situation.now_volume_of_N = this_situation.now_volume_of_N; 83 next_situation.step=this_situation.step+1; 84 next_situation.who_to_who=1; 85 q.push(next_situation); 86 } 87 else if(this_situation.now_volume_of_S < M-this_situation.now_volume_of_M){ 88 next_situation.now_volume_of_M=this_situation.now_volume_of_M + this_situation.now_volume_of_S; 89 next_situation.now_volume_of_S=0; 90 next_situation.now_volume_of_M=this_situation.now_volume_of_M; 91 next_situation.step=this_situation.step+1; 92 next_situation.who_to_who=1; 93 q.push(next_situation); 94 } 95 96 } 97 98 if(this_situation.who_to_who==0) 99 ;//N→S 100 else{ 101 if(this_situation.now_volume_of_N==0) 102 ; 103 else if(this_situation.now_volume_of_N >= S-this_situation.now_volume_of_S){ 104 next_situation.now_volume_of_N = this_situation.now_volume_of_N - (S-this_situation.now_volume_of_S); 105 next_situation.now_volume_of_S = S; 106 next_situation.now_volume_of_M = this_situation.now_volume_of_M; 107 next_situation.step=this_situation.step+1; 108 next_situation.who_to_who=2; 109 q.push(next_situation); 110 } 111 else if(this_situation.now_volume_of_S < N-this_situation.now_volume_of_N){ 112 next_situation.now_volume_of_N=this_situation.now_volume_of_N + this_situation.now_volume_of_S; 113 next_situation.now_volume_of_S=0; 114 next_situation.now_volume_of_M=this_situation.now_volume_of_M; 115 next_situation.step=this_situation.step+1; 116 next_situation.who_to_who=2; 117 q.push(next_situation); 118 } 119 } 120 121 122 if(this_situation.who_to_who==4) 123 ;//N→M 124 else{ 125 if(this_situation.now_volume_of_N==0) 126 ; 127 else if(this_situation.now_volume_of_N >= M-this_situation.now_volume_of_M){ 128 next_situation.now_volume_of_N = this_situation.now_volume_of_N - (M-this_situation.now_volume_of_M); 129 next_situation.now_volume_of_M = M; 130 next_situation.now_volume_of_S = this_situation.now_volume_of_S; 131 next_situation.step=this_situation.step+1; 132 next_situation.who_to_who=3; 133 q.push(next_situation); 134 } 135 else if(this_situation.now_volume_of_N < M-this_situation.now_volume_of_M){ 136 next_situation.now_volume_of_M=this_situation.now_volume_of_M + this_situation.now_volume_of_N; 137 next_situation.now_volume_of_N=0; 138 next_situation.now_volume_of_S=this_situation.now_volume_of_S; 139 next_situation.step=this_situation.step+1; 140 next_situation.who_to_who=3; 141 q.push(next_situation); 142 } 143 } 144 145 if(this_situation.who_to_who==3) 146 ;//M→N 147 else{ 148 if(this_situation.now_volume_of_M==0) 149 ; 150 else if(this_situation.now_volume_of_M >= N-this_situation.now_volume_of_N){ 151 next_situation.now_volume_of_M=this_situation.now_volume_of_M-(N-this_situation.now_volume_of_N); 152 next_situation.now_volume_of_N=N; 153 next_situation.now_volume_of_S=this_situation.now_volume_of_S; 154 next_situation.step=this_situation.step+1; 155 next_situation.who_to_who=4; 156 q.push(next_situation); 157 } 158 else if(this_situation.now_volume_of_M<N-this_situation.now_volume_of_N){ 159 next_situation.now_volume_of_N=this_situation.now_volume_of_N + this_situation.now_volume_of_M; 160 next_situation.now_volume_of_M=0; 161 next_situation.now_volume_of_S=this_situation.now_volume_of_S; 162 next_situation.step=this_situation.step+1; 163 next_situation.who_to_who=4; 164 q.push(next_situation); 165 } 166 } 167 168 if(this_situation.who_to_who==1) 169 ;//M→S 我想M倒给S,但如果上一步是S倒给M的就别折腾了 170 else{ 171 if(this_situation.now_volume_of_M==0)//M想倒你得有,没有还倒啥 172 ; 173 else if(this_situation.now_volume_of_M>=S-this_situation.now_volume_of_S){//确定有后,如果M此时装的可乐比S还能装的空间大 174 next_situation.now_volume_of_M=this_situation.now_volume_of_M-(S-this_situation.now_volume_of_S);//则M倒出S能承受的 175 next_situation.now_volume_of_S=S;//S肯定满上了 176 next_situation.now_volume_of_N=this_situation.now_volume_of_N;//跟N没关系,N还用上一个的可乐体积状态 177 next_situation.step=this_situation.step+1; 178 next_situation.who_to_who=5;//还用解释? 179 q.push(next_situation); 180 } 181 else if(this_situation.now_volume_of_M<S-this_situation.now_volume_of_S){//确定有后,如果M此时装的可乐比S还能装的空间小 182 next_situation.now_volume_of_S=this_situation.now_volume_of_S+this_situation.now_volume_of_M;//那不多逼逼,直接M全射给S 183 next_situation.now_volume_of_M=0;//M射空了 184 next_situation.now_volume_of_N=this_situation.now_volume_of_N; 185 next_situation.step=this_situation.step+1; 186 next_situation.who_to_who=5; 187 q.push(next_situation); 188 } 189 } 190 } 191 if(flag==0) 192 cout<<"NO"<<endl; 193 } 194 }
但运行后.exe文件一直没反应,浏览器啥也全都卡住了,任务管理器显示exe文件内存99%,直接给我电脑干蓝屏了,死循环也不至于这样啊
HDOJ也tm崩了卧槽这都什么鸡巴OJ啊,没一个稳定的。真不愧那句话,大学acm被中学生屠榜,反而找到的中学生OJ平台都很棒棒的。
发现百度唯二的用处:1、报错信息智能回答好,2、找题的其他平台好(题目描述copy一搜百度就有,bing搜不到)
但这题百度没找到,又用bing搜关键词“算法题 非常可乐”找到了个其他可用平台2,同样是自己的数据,以后都管这个ACGO叫可用平台2,这个ACGO挺喜欢的但有点像充值能看东西的垃圾leetcode和pat他们内味呢,不过用骨头给提示思路确实很棒,但不打算用,回忆洛谷变成蓝名才有咕值,且说明里可以下载第一个数据点,但也没用过
回忆,金鏼神
WC
ACfun群
可用平台2上提示内存超限制
内存超时这么严重吗,低端hp envy13电脑都会干蓝屏,但不应该啊相等就break了,为啥4 1 3 这组数据还是崩溃
继续调试,把任务管理打开,内存89%就关掉exe
简单调试下发现代码写错了,
1、SNM字母小问题
2、没q.pop()
3、对于4 1 3样例,如果中途俩杯子都是1,这样也会输出,但应该是2的时候再输出。还有起始S\N\M中,N/M都是0,所以要来一句 this_situation.step != 0 防止第一次开板就tm给老子输出
4、等号问题
妈的我甚至以为 if(1==1==1==1==1) 不能连等判断,但(用了个错误的代码)简单测试后发现可以就没管他去找其他bug了,结果其他找半天一句一句cout试验发现没问题,艹最后定位问题就是这连等,“除去所有的假象,剩下的一种解释,无论怎么不可能,都是真相”,进一步测试惊了
代码
1 #include<stdio.h> 2 #include<iostream> 3 using namespace std; 4 int main() 5 { 6 if(1==1==2/2) 7 cout<<"!"<<endl;//输出 8 9 if(2==2==2) 10 cout<<"$"<<endl;//无 11 12 if(1==1==1==1==1==1) 13 cout<<"*"<<endl;//输出 14 15 int a=4; 16 if(2==a/2) 17 cout<<"@"<<endl;//输出 18 19 if(2==2==a/2) 20 cout<<"#"<<endl;//无 21 }
不说大话,我虽然是个垃圾,但刷题找bug调试这块自以为很强,感觉找bug这块都可以直播写题直播找bug过程了
不知道是不能连等还是不能把变量加进去比较,看了个家伙写的博客都没总结对,找到个很不错的博客总结的很到位
有点明白为啥BUPT研究生考试总有写奇奇怪怪的小C语言问题,我现在遇到的就是
好了,改正后4 1 3这组数据对了,再弄成浮点
提交,我草泥马猜对了果然是小孩玩意,居然显示这么多组数据
顿时不香了。跟那个可用平台根本没法比
而且那个狗狗的看板娘有问题,点提交总是点开他,除非缩小网页,但偏偏我还不想Adblock Plus把他关了(最后发现可以移动位置)
就在此时,hdoj好了,哈哈哈果断关掉ACGO
我实在不会算那个时间复杂度(先搁置),没想到就三个东西来回加入队列也会运行这么久吗,step到13的时候就已经等好久都不输出了,我赶紧关了,待会电脑又给我干蓝屏了,我直接写了句 if(this_situation.step==10) break; 只能过10步以内的样例。
我的代码:
1 #include<stdio.h> 2 #include<iostream> 3 using namespace std; 4 double S; 5 double N; 6 double M; 7 #include<queue> 8 struct Situation{ 9 double now_volume_of_N;//此时装了装多少可乐 10 double now_volume_of_M; 11 double now_volume_of_S; 12 int step; 13 int who_to_who; 14 // -1:还没美开始走 15 // 0:S→N 16 // 1:S→M 17 // 2:N→S 18 // 3:N→M 19 // 4:M→N 20 // 5:M→S 21 }; 22 Situation situation; 23 queue<Situation>q; 24 int flag; 25 int main() 26 { 27 while(cin>>S>>N>>M&&(N+M+S)){ 28 while(!q.empty())//不在break那清空是怕根本找不到能平分的, 29 q.pop(); 30 31 flag=0; 32 situation.now_volume_of_N=1; 33 situation.now_volume_of_M=0; 34 situation.now_volume_of_S=S; 35 situation.step=0; 36 situation.who_to_who=-1; 37 q.push(situation); 38 39 while(!q.empty()){ 40 Situation this_situation; 41 this_situation=q.front(); 42 q.pop(); 43 44 //这题没vis,如果找不到也就会无限循环跑下去,随便设置个step吧 45 if(this_situation.step==10) 46 break; 47 48 if( 49 (this_situation.step!=0)&& 50 ((this_situation.now_volume_of_M==this_situation.now_volume_of_N&&this_situation.now_volume_of_N==S/2) 51 || (this_situation.now_volume_of_M==this_situation.now_volume_of_S&&this_situation.now_volume_of_S==S/2) 52 || (this_situation.now_volume_of_S==this_situation.now_volume_of_N&&this_situation.now_volume_of_N==S/2)) 53 )//主要是为了过滤掉第一步,因为step==0的时候。俩杯子相等但都是0,没意义 54 { 55 cout<<this_situation.step<<endl; 56 flag=1; 57 break; 58 } 59 60 Situation next_situation; 61 if(this_situation.who_to_who==2) 62 ;//我想S→N,但上一步如果是N→S就别走了,白折腾,剪枝 63 else{//S→N,首先S得有,有!要么对方剩余空间比我现在装的可乐体积大则全倒,要么我目前装的可乐体积大,以对方剩余空间为准给对方满上,相等情况,咋都行 64 if(this_situation.now_volume_of_S==0) 65 ; 66 else if(this_situation.now_volume_of_S >= N-this_situation.now_volume_of_N){ 67 next_situation.now_volume_of_S = this_situation.now_volume_of_S - (N-this_situation.now_volume_of_N); 68 // 这句话没必要看,因为思路很简单就是S如果现有的体积更大,就以对方为准去倒,但代码的加加减减看着很乱 69 next_situation.now_volume_of_N = N; 70 next_situation.now_volume_of_M = this_situation.now_volume_of_M; 71 next_situation.step=this_situation.step+1; 72 next_situation.who_to_who=0; 73 q.push(next_situation); 74 } 75 else if(this_situation.now_volume_of_S < N-this_situation.now_volume_of_N){//S现在有的可乐比对方还能装的空间小,那直接全给对方 76 next_situation.now_volume_of_N=this_situation.now_volume_of_N+this_situation.now_volume_of_S; 77 next_situation.now_volume_of_S=0; 78 next_situation.now_volume_of_M=this_situation.now_volume_of_M; 79 next_situation.step=this_situation.step+1; 80 next_situation.who_to_who=0; 81 q.push(next_situation); 82 } 83 } 84 85 if(this_situation.who_to_who==5) 86 ;//S→M,但上一步是M→S就别倒了 87 else{ 88 if(this_situation.now_volume_of_S==0) 89 ; 90 else if(this_situation.now_volume_of_S >= M-this_situation.now_volume_of_M){ 91 next_situation.now_volume_of_S = this_situation.now_volume_of_S - (M-this_situation.now_volume_of_M); 92 //这句话没必要看,因为思路很简单就是S如果现有的体积更大,就以对方为准去倒,但代码的加加减减看着很乱, 93 next_situation.now_volume_of_M = M; 94 next_situation.now_volume_of_N = this_situation.now_volume_of_N; 95 next_situation.step=this_situation.step+1; 96 next_situation.who_to_who=1; 97 q.push(next_situation); 98 } 99 else if(this_situation.now_volume_of_S < M-this_situation.now_volume_of_M){ 100 next_situation.now_volume_of_M=this_situation.now_volume_of_M + this_situation.now_volume_of_S; 101 next_situation.now_volume_of_S=0; 102 next_situation.now_volume_of_N=this_situation.now_volume_of_N; 103 next_situation.step=this_situation.step+1; 104 next_situation.who_to_who=1; 105 q.push(next_situation); 106 } 107 108 } 109 110 if(this_situation.who_to_who==0) 111 ;//N→S 112 else{ 113 if(this_situation.now_volume_of_N==0) 114 ; 115 else if(this_situation.now_volume_of_N >= S-this_situation.now_volume_of_S){ 116 next_situation.now_volume_of_N = this_situation.now_volume_of_N - (S-this_situation.now_volume_of_S); 117 next_situation.now_volume_of_S = S; 118 next_situation.now_volume_of_M = this_situation.now_volume_of_M; 119 next_situation.step=this_situation.step+1; 120 next_situation.who_to_who=2; 121 q.push(next_situation); 122 } 123 else if(this_situation.now_volume_of_N < S-this_situation.now_volume_of_S){ 124 next_situation.now_volume_of_S=this_situation.now_volume_of_N + this_situation.now_volume_of_S; 125 next_situation.now_volume_of_N=0; 126 next_situation.now_volume_of_M=this_situation.now_volume_of_M; 127 next_situation.step=this_situation.step+1; 128 next_situation.who_to_who=2; 129 // cout<<"#"<<next_situation.now_volume_of_S<<" "<<next_situation.now_volume_of_N<<" "<<next_situation.now_volume_of_M<<" "<<next_situation.step<<endl; 130 q.push(next_situation); 131 } 132 } 133 134 135 if(this_situation.who_to_who==4) 136 ;//N→M 137 else{ 138 if(this_situation.now_volume_of_N==0) 139 ; 140 else if(this_situation.now_volume_of_N >= M-this_situation.now_volume_of_M){ 141 next_situation.now_volume_of_N = this_situation.now_volume_of_N - (M-this_situation.now_volume_of_M); 142 next_situation.now_volume_of_M = M; 143 next_situation.now_volume_of_S = this_situation.now_volume_of_S; 144 next_situation.step=this_situation.step+1; 145 next_situation.who_to_who=3; 146 q.push(next_situation); 147 } 148 else if(this_situation.now_volume_of_N < M-this_situation.now_volume_of_M){ 149 next_situation.now_volume_of_M=this_situation.now_volume_of_M + this_situation.now_volume_of_N; 150 next_situation.now_volume_of_N=0; 151 next_situation.now_volume_of_S=this_situation.now_volume_of_S; 152 next_situation.step=this_situation.step+1; 153 next_situation.who_to_who=3; 154 q.push(next_situation); 155 } 156 } 157 158 if(this_situation.who_to_who==3) 159 ;//M→N 160 else{ 161 if(this_situation.now_volume_of_M==0) 162 ; 163 else if(this_situation.now_volume_of_M >= N-this_situation.now_volume_of_N){ 164 next_situation.now_volume_of_M=this_situation.now_volume_of_M-(N-this_situation.now_volume_of_N); 165 next_situation.now_volume_of_N=N; 166 next_situation.now_volume_of_S=this_situation.now_volume_of_S; 167 next_situation.step=this_situation.step+1; 168 next_situation.who_to_who=4; 169 // cout<<"!"<<next_situation.now_volume_of_S<<" "<<next_situation.now_volume_of_N<<" "<<next_situation.now_volume_of_M<<endl; 170 q.push(next_situation); 171 } 172 else if(this_situation.now_volume_of_M<N-this_situation.now_volume_of_N){ 173 next_situation.now_volume_of_N=this_situation.now_volume_of_N + this_situation.now_volume_of_M; 174 next_situation.now_volume_of_M=0; 175 next_situation.now_volume_of_S=this_situation.now_volume_of_S; 176 next_situation.step=this_situation.step+1; 177 next_situation.who_to_who=4; 178 q.push(next_situation); 179 } 180 } 181 182 if(this_situation.who_to_who==1) 183 ;//M→S 我想M倒给S,但如果上一步是S倒给M的就别折腾了 184 else{ 185 if(this_situation.now_volume_of_M==0)//M想倒你得有,没有还倒啥 186 ; 187 else if(this_situation.now_volume_of_M>=S-this_situation.now_volume_of_S){//确定有后,如果M此时装的可乐比S还能装的空间大 188 next_situation.now_volume_of_M=this_situation.now_volume_of_M-(S-this_situation.now_volume_of_S);//则M倒出S能承受的 189 next_situation.now_volume_of_S=S;//S肯定满上了 190 next_situation.now_volume_of_N=this_situation.now_volume_of_N;//跟N没关系,N还用上一个的可乐体积状态 191 next_situation.step=this_situation.step+1; 192 next_situation.who_to_who=5;//还用解释? 193 q.push(next_situation); 194 } 195 else if(this_situation.now_volume_of_M<S-this_situation.now_volume_of_S){//确定有后,如果M此时装的可乐比S还能装的空间小 196 next_situation.now_volume_of_S=this_situation.now_volume_of_S+this_situation.now_volume_of_M;//那不多逼逼,直接M全射给S 197 next_situation.now_volume_of_M=0;//M射空了 198 next_situation.now_volume_of_N=this_situation.now_volume_of_N; 199 next_situation.step=this_situation.step+1; 200 next_situation.who_to_who=5; 201 q.push(next_situation); 202 } 203 } 204 } 205 if(flag==0) 206 cout<<"-1"<<endl; 207 } 208 }
学习下别人的思路:
真高看你了,居然都是整数,奇数都直接NO了
写的嘎嘎好的博客,爱了,第一次看懂别人的代码
BFS还是不灵活啊,每次都知道vis是记录走过的状态,我是从操作的角度去想的,觉得S可以给N无数遍,就没设置vis,只是上一步走过的操作,这一步就不操作他的反操作,比如上一步S→N,那这一步就不要用N→S了
但其实应该从状态角度去想,只要再次操作使得状态出现过,就vis设置1
且 一共三个杯子,知道总和,所以只需要前两个杯子的状态,就可以进行判断了 这句更传神,优化内存的点睛之笔
另外这篇博客也是一等一的棒,其中边界值判断那也是贼棒,也相当于是优化,思路不错很受启发
自己写的时候发现确实不可以是小数,比如N此时状态是容积为小数1.5,那记录这个状态的vis数组,vis[1.5]就会出问题
只能设置成int,但int7/2会有问题,所以,这里奇数直接不用进入判断了直接NO。否则会有诸如7 3 4→3 1 3(此时7/2是3,3==3==S/2)就误以为被平分就输出的情况
如果题目要求小数,可以在node结构体里再定义3个double数组来存储小数状态
改完,代码写完又WA了
1 #include<stdio.h> 2 #include<iostream> 3 #include<string.h> 4 using namespace std; 5 int S; 6 int N; 7 int M; 8 #include<queue> 9 struct Situation 10 { 11 int now_volume_of_N;//此时装了装多少可乐 12 int now_volume_of_M; 13 int now_volume_of_S; 14 int step; 15 int who_to_who; 16 // -1:还没美开始走 17 // 0:S→N 18 // 1:S→M 19 // 2:N→S 20 // 3:N→M 21 // 4:M→N 22 // 5:M→S 23 }; 24 25 int vis[100][100]; 26 27 Situation situation; 28 queue<Situation>q; 29 int flag; 30 int main() 31 { 32 while(cin >> S >> N >> M && (N + M + S)) 33 { 34 if(S%2==1){ 35 cout<<"NO"<<endl; 36 continue; 37 } 38 while(!q.empty())//不在break那清空是怕根本找不到能平分的, 39 q.pop(); 40 memset(vis, 0, sizeof(vis)); 41 flag = 0; 42 situation.now_volume_of_N = 0; 43 situation.now_volume_of_M = 0; 44 situation.now_volume_of_S = S; 45 situation.step = 0; 46 situation.who_to_who = -1; 47 q.push(situation); 48 49 vis[situation.now_volume_of_S][situation.now_volume_of_M] = 1; 50 51 while(!q.empty()) 52 { 53 Situation this_situation; 54 this_situation = q.front(); 55 q.pop(); 56 57 if( 58 (this_situation.step != 0) && 59 ((this_situation.now_volume_of_M==this_situation.now_volume_of_N&&this_situation.now_volume_of_N==S/2) 60 || (this_situation.now_volume_of_M==this_situation.now_volume_of_S&&this_situation.now_volume_of_S==S/2) 61 || (this_situation.now_volume_of_S==this_situation.now_volume_of_N&&this_situation.now_volume_of_N==S/2)) 62 )//主要是为了过滤掉第一步,因为step==0的时候。俩杯子相等但都是0,没意义 63 { 64 cout << this_situation.step << endl; 65 flag = 1; 66 break; 67 } 68 69 Situation next_situation; 70 if(this_situation.who_to_who == 2) 71 ;//我想S→N,但上一步如果是N→S就别走了,白折腾,剪枝 72 else //S→N,首先S得有,有!要么对方剩余空间比我现在装的可乐体积大则全倒,要么我目前装的可乐体积大,以对方剩余空间为准给对方满上,相等情况,咋都行 73 { 74 if(this_situation.now_volume_of_S == 0) 75 ; 76 else if(this_situation.now_volume_of_S >= N - this_situation.now_volume_of_N) 77 { 78 next_situation.now_volume_of_S = this_situation.now_volume_of_S - (N - this_situation.now_volume_of_N); 79 // 这句话没必要看,因为思路很简单就是S如果现有的体积更大,就以对方为准去倒,但代码的加加减减看着很乱 80 next_situation.now_volume_of_N = N; 81 next_situation.now_volume_of_M = this_situation.now_volume_of_M; 82 next_situation.step = this_situation.step + 1; 83 next_situation.who_to_who = 0; 84 if(vis[next_situation.now_volume_of_S][next_situation.now_volume_of_M] == 0) 85 { 86 q.push(next_situation); 87 // cout<<"#0#"<<next_situation.now_volume_of_S<<" "<<next_situation.now_volume_of_N<<" "<<next_situation.now_volume_of_M<<" "<<next_situation.step<<endl; 88 vis[next_situation.now_volume_of_S][next_situation.now_volume_of_M] = 1; 89 90 } 91 } 92 else if(this_situation.now_volume_of_S < N - this_situation.now_volume_of_N) //S现在有的可乐比对方还能装的空间小,那直接全给对方 93 { 94 next_situation.now_volume_of_N = this_situation.now_volume_of_N + this_situation.now_volume_of_S; 95 next_situation.now_volume_of_S = 0; 96 next_situation.now_volume_of_M = this_situation.now_volume_of_M; 97 next_situation.step = this_situation.step + 1; 98 next_situation.who_to_who = 0; 99 if(vis[next_situation.now_volume_of_S][next_situation.now_volume_of_M] == 0) 100 { 101 q.push(next_situation); 102 // cout<<"#1#"<<next_situation.now_volume_of_S<<" "<<next_situation.now_volume_of_N<<" "<<next_situation.now_volume_of_M<<" "<<next_situation.step<<endl; 103 vis[next_situation.now_volume_of_S][next_situation.now_volume_of_M] = 1; 104 } 105 } 106 } 107 108 if(this_situation.who_to_who == 5) 109 ;//S→M,但上一步是M→S就别倒了 110 else 111 { 112 if(this_situation.now_volume_of_S == 0) 113 ; 114 else if(this_situation.now_volume_of_S >= M - this_situation.now_volume_of_M) 115 { 116 next_situation.now_volume_of_S = this_situation.now_volume_of_S - (M - this_situation.now_volume_of_M); 117 //这句话没必要看,因为思路很简单就是S如果现有的体积更大,就以对方为准去倒,但代码的加加减减看着很乱, 118 next_situation.now_volume_of_M = M; 119 next_situation.now_volume_of_N = this_situation.now_volume_of_N; 120 next_situation.step = this_situation.step + 1; 121 next_situation.who_to_who = 1; 122 if(vis[next_situation.now_volume_of_S][next_situation.now_volume_of_M] == 0) 123 { 124 q.push(next_situation); 125 // cout<<"@0@"<<next_situation.now_volume_of_S<<" "<<next_situation.now_volume_of_N<<" "<<next_situation.now_volume_of_M<<" "<<next_situation.step<<endl; 126 vis[next_situation.now_volume_of_S][next_situation.now_volume_of_M] = 1; 127 } 128 } 129 else if(this_situation.now_volume_of_S < M - this_situation.now_volume_of_M) 130 { 131 next_situation.now_volume_of_M = this_situation.now_volume_of_M + this_situation.now_volume_of_S; 132 next_situation.now_volume_of_S = 0; 133 next_situation.now_volume_of_N = this_situation.now_volume_of_N; 134 next_situation.step = this_situation.step + 1; 135 next_situation.who_to_who = 1; 136 if(vis[next_situation.now_volume_of_S][next_situation.now_volume_of_M] == 0) 137 { 138 q.push(next_situation); 139 // cout<<"@1@"<<next_situation.now_volume_of_S<<" "<<next_situation.now_volume_of_N<<" "<<next_situation.now_volume_of_M<<" "<<next_situation.step<<endl; 140 vis[next_situation.now_volume_of_S][next_situation.now_volume_of_M] = 1; 141 } 142 } 143 144 } 145 146 if(this_situation.who_to_who == 0) 147 ;//N→S 148 else 149 { 150 if(this_situation.now_volume_of_N == 0) 151 ; 152 else if(this_situation.now_volume_of_N >= S - this_situation.now_volume_of_S) 153 { 154 next_situation.now_volume_of_N = this_situation.now_volume_of_N - (S - this_situation.now_volume_of_S); 155 next_situation.now_volume_of_S = S; 156 next_situation.now_volume_of_M = this_situation.now_volume_of_M; 157 next_situation.step = this_situation.step + 1; 158 next_situation.who_to_who = 2; 159 160 if(vis[next_situation.now_volume_of_S][next_situation.now_volume_of_M] == 0) 161 { 162 q.push(next_situation); 163 // cout<<"!0!"<<next_situation.now_volume_of_S<<" "<<next_situation.now_volume_of_N<<" "<<next_situation.now_volume_of_M<<" "<<next_situation.step<<endl; 164 vis[next_situation.now_volume_of_S][next_situation.now_volume_of_M] = 1; 165 } 166 } 167 else if(this_situation.now_volume_of_N < S - this_situation.now_volume_of_S) 168 { 169 next_situation.now_volume_of_S = this_situation.now_volume_of_N + this_situation.now_volume_of_S; 170 next_situation.now_volume_of_N = 0; 171 next_situation.now_volume_of_M = this_situation.now_volume_of_M; 172 next_situation.step = this_situation.step + 1; 173 next_situation.who_to_who = 2; 174 175 if(vis[next_situation.now_volume_of_S][next_situation.now_volume_of_M] == 0) 176 { 177 q.push(next_situation); 178 // cout<<"!1!"<<next_situation.now_volume_of_S<<" "<<next_situation.now_volume_of_N<<" "<<next_situation.now_volume_of_M<<" "<<next_situation.step<<endl; 179 vis[next_situation.now_volume_of_S][next_situation.now_volume_of_M] = 1; 180 } 181 } 182 } 183 184 185 if(this_situation.who_to_who == 4) 186 ;//N→M 187 else 188 { 189 if(this_situation.now_volume_of_N == 0) 190 ; 191 else if(this_situation.now_volume_of_N >= M - this_situation.now_volume_of_M) 192 { 193 next_situation.now_volume_of_N = this_situation.now_volume_of_N - (M - this_situation.now_volume_of_M); 194 next_situation.now_volume_of_M = M; 195 next_situation.now_volume_of_S = this_situation.now_volume_of_S; 196 next_situation.step = this_situation.step + 1; 197 next_situation.who_to_who = 3; 198 199 if(vis[next_situation.now_volume_of_S][next_situation.now_volume_of_M] == 0) 200 { 201 // cout<<"$0$"<<next_situation.now_volume_of_S<<" "<<next_situation.now_volume_of_N<<" "<<next_situation.now_volume_of_M<<" "<<next_situation.step<<endl; 202 q.push(next_situation); 203 vis[next_situation.now_volume_of_S][next_situation.now_volume_of_M] = 1; 204 } 205 } 206 else if(this_situation.now_volume_of_N < M - this_situation.now_volume_of_M) 207 { 208 // cout<<"nm"<<this_situation.now_volume_of_N<<" "<<this_situation.step<<endl; 209 next_situation.now_volume_of_M = this_situation.now_volume_of_M + this_situation.now_volume_of_N; 210 next_situation.now_volume_of_N = 0; 211 next_situation.now_volume_of_S = this_situation.now_volume_of_S; 212 next_situation.step = this_situation.step + 1; 213 next_situation.who_to_who = 3; 214 215 if(vis[next_situation.now_volume_of_S][next_situation.now_volume_of_M] == 0) 216 { 217 q.push(next_situation); 218 // cout<<"$1$"<<next_situation.now_volume_of_S<<" "<<next_situation.now_volume_of_N<<" "<<next_situation.now_volume_of_M<<" "<<next_situation.step<<endl; 219 vis[next_situation.now_volume_of_S][next_situation.now_volume_of_M] = 1; 220 } 221 } 222 } 223 224 if(this_situation.who_to_who == 3) 225 ;//M→N 226 else 227 { 228 if(this_situation.now_volume_of_M == 0) 229 ; 230 else if(this_situation.now_volume_of_M >= N - this_situation.now_volume_of_N) 231 { 232 next_situation.now_volume_of_M = this_situation.now_volume_of_M - (N - this_situation.now_volume_of_N); 233 next_situation.now_volume_of_N = N; 234 next_situation.now_volume_of_S = this_situation.now_volume_of_S; 235 next_situation.step = this_situation.step + 1; 236 next_situation.who_to_who = 4; 237 238 if(vis[next_situation.now_volume_of_S][next_situation.now_volume_of_M] == 0) 239 { 240 // cout<<"%0%"<<next_situation.now_volume_of_S<<" "<<next_situation.now_volume_of_N<<" "<<next_situation.now_volume_of_M<<" "<<next_situation.step<<endl; 241 q.push(next_situation); 242 vis[next_situation.now_volume_of_S][next_situation.now_volume_of_M] = 1; 243 } 244 } 245 else if(this_situation.now_volume_of_M < N - this_situation.now_volume_of_N) 246 { 247 next_situation.now_volume_of_N = this_situation.now_volume_of_N + this_situation.now_volume_of_M; 248 next_situation.now_volume_of_M = 0; 249 next_situation.now_volume_of_S = this_situation.now_volume_of_S; 250 next_situation.step = this_situation.step + 1; 251 next_situation.who_to_who = 4; 252 253 if(vis[next_situation.now_volume_of_S][next_situation.now_volume_of_M] == 0) 254 { 255 // cout<<"%1%"<<next_situation.now_volume_of_S<<" "<<next_situation.now_volume_of_N<<" "<<next_situation.now_volume_of_M<<" "<<next_situation.step<<endl; 256 q.push(next_situation); 257 vis[next_situation.now_volume_of_S][next_situation.now_volume_of_M] = 1; 258 } 259 } 260 } 261 262 if(this_situation.who_to_who == 1) 263 ;//M→S 我想M倒给S,但如果上一步是S倒给M的就别折腾了 264 else 265 { 266 if(this_situation.now_volume_of_M == 0) //M想倒你得有,没有还倒啥 267 ; 268 else if(this_situation.now_volume_of_M >= S - this_situation.now_volume_of_S) //确定有后,如果M此时装的可乐比S还能装的空间大 269 { 270 next_situation.now_volume_of_M = this_situation.now_volume_of_M - (S - this_situation.now_volume_of_S); //则M倒出S能承受的 271 next_situation.now_volume_of_S = S; //S肯定满上了 272 next_situation.now_volume_of_N = this_situation.now_volume_of_N; //跟N没关系,N还用上一个的可乐体积状态 273 next_situation.step = this_situation.step + 1; 274 next_situation.who_to_who = 5; //还用解释? 275 if(vis[next_situation.now_volume_of_S][next_situation.now_volume_of_M] == 0) 276 { 277 // cout<<"&0&"<<next_situation.now_volume_of_S<<" "<<next_situation.now_volume_of_N<<" "<<next_situation.now_volume_of_M<<" "<<next_situation.step<<endl; 278 q.push(next_situation); 279 vis[next_situation.now_volume_of_S][next_situation.now_volume_of_M] = 1; 280 } 281 } 282 else if(this_situation.now_volume_of_M < S - this_situation.now_volume_of_S) //确定有后,如果M此时装的可乐比S还能装的空间小 283 { 284 next_situation.now_volume_of_S = this_situation.now_volume_of_S + this_situation.now_volume_of_M; //那不多逼逼,直接M全射给S 285 next_situation.now_volume_of_M = 0; //M射空了 286 next_situation.now_volume_of_N = this_situation.now_volume_of_N; 287 next_situation.step = this_situation.step + 1; 288 next_situation.who_to_who = 5; 289 if(vis[next_situation.now_volume_of_S][next_situation.now_volume_of_M] == 0) 290 { 291 q.push(next_situation); 292 // cout<<"&1&"<<next_situation.now_volume_of_S<<" "<<next_situation.now_volume_of_N<<" "<<next_situation.now_volume_of_M<<" "<<next_situation.step<<endl; 293 vis[next_situation.now_volume_of_S][next_situation.now_volume_of_M] = 1; 294 } 295 } 296 } 297 } 298 if(flag == 0) 299 cout << "NO" << endl; 300 } 301 }
感觉对拍和可看数据没啥区别呢。可能更高深的理由,还是对拍吧,不去用ACGO了
可是对拍也找不到差异,但我就是不想看ACGO的数据
发现题目一个是NO,一个是-1,改完ACGO上AC了
但HDOJ上WA了,这hdu平台也不错啊
卧槽!!数组vis从100改到101就对了
AC代码
1 #include<stdio.h> 2 #include<iostream> 3 #include<string.h> 4 using namespace std; 5 int S; 6 int N; 7 int M; 8 #include<queue> 9 struct Situation 10 { 11 int now_volume_of_N;//此时装了装多少可乐 12 int now_volume_of_M; 13 int now_volume_of_S; 14 int step; 15 int who_to_who; 16 // -1:还没美开始走 17 // 0:S→N 18 // 1:S→M 19 // 2:N→S 20 // 3:N→M 21 // 4:M→N 22 // 5:M→S 23 }; 24 25 int vis[101][100]; 26 27 Situation situation; 28 queue<Situation>q; 29 int flag; 30 int main() 31 { 32 while(cin >> S >> N >> M && (N + M + S)) 33 { 34 // if(S>100){ 35 // cout<<"CCC"<<endl; 36 // continue; 37 // } 38 if(S%2==1){ 39 cout<<"NO"<<endl; 40 continue; 41 } 42 while(!q.empty())//不在break那清空是怕根本找不到能平分的, 43 q.pop(); 44 memset(vis, 0, sizeof(vis)); 45 flag = 0; 46 situation.now_volume_of_N = 0; 47 situation.now_volume_of_M = 0; 48 situation.now_volume_of_S = S; 49 situation.step = 0; 50 situation.who_to_who = -1; 51 q.push(situation); 52 53 vis[situation.now_volume_of_S][situation.now_volume_of_M] = 1; 54 55 while(!q.empty()) 56 { 57 Situation this_situation; 58 this_situation = q.front(); 59 q.pop(); 60 61 if( 62 (this_situation.step != 0) && 63 ((this_situation.now_volume_of_M==this_situation.now_volume_of_N&&this_situation.now_volume_of_N==S/2) 64 || (this_situation.now_volume_of_M==this_situation.now_volume_of_S&&this_situation.now_volume_of_S==S/2) 65 || (this_situation.now_volume_of_S==this_situation.now_volume_of_N&&this_situation.now_volume_of_N==S/2)) 66 )//主要是为了过滤掉第一步,因为step==0的时候。俩杯子相等但都是0,没意义 67 { 68 cout << this_situation.step << endl; 69 flag = 1; 70 break; 71 } 72 73 Situation next_situation; 74 if(this_situation.who_to_who == 2) 75 ;//我想S→N,但上一步如果是N→S就别走了,白折腾,剪枝 76 else //S→N,首先S得有,有!要么对方剩余空间比我现在装的可乐体积大则全倒,要么我目前装的可乐体积大,以对方剩余空间为准给对方满上,相等情况,咋都行 77 { 78 if(this_situation.now_volume_of_S == 0) 79 ; 80 else if(this_situation.now_volume_of_S >= N - this_situation.now_volume_of_N) 81 { 82 next_situation.now_volume_of_S = this_situation.now_volume_of_S - (N - this_situation.now_volume_of_N); 83 // 这句话没必要看,因为思路很简单就是S如果现有的体积更大,就以对方为准去倒,但代码的加加减减看着很乱 84 next_situation.now_volume_of_N = N; 85 next_situation.now_volume_of_M = this_situation.now_volume_of_M; 86 next_situation.step = this_situation.step + 1; 87 next_situation.who_to_who = 0; 88 if(vis[next_situation.now_volume_of_S][next_situation.now_volume_of_M] == 0) 89 { 90 q.push(next_situation); 91 // cout<<"#0#"<<next_situation.now_volume_of_S<<" "<<next_situation.now_volume_of_N<<" "<<next_situation.now_volume_of_M<<" "<<next_situation.step<<endl; 92 vis[next_situation.now_volume_of_S][next_situation.now_volume_of_M] = 1; 93 94 } 95 } 96 else if(this_situation.now_volume_of_S < N - this_situation.now_volume_of_N) //S现在有的可乐比对方还能装的空间小,那直接全给对方 97 { 98 next_situation.now_volume_of_N = this_situation.now_volume_of_N + this_situation.now_volume_of_S; 99 next_situation.now_volume_of_S = 0; 100 next_situation.now_volume_of_M = this_situation.now_volume_of_M; 101 next_situation.step = this_situation.step + 1; 102 next_situation.who_to_who = 0; 103 if(vis[next_situation.now_volume_of_S][next_situation.now_volume_of_M] == 0) 104 { 105 q.push(next_situation); 106 // cout<<"#1#"<<next_situation.now_volume_of_S<<" "<<next_situation.now_volume_of_N<<" "<<next_situation.now_volume_of_M<<" "<<next_situation.step<<endl; 107 vis[next_situation.now_volume_of_S][next_situation.now_volume_of_M] = 1; 108 } 109 } 110 } 111 112 if(this_situation.who_to_who == 5) 113 ;//S→M,但上一步是M→S就别倒了 114 else 115 { 116 if(this_situation.now_volume_of_S == 0) 117 ; 118 else if(this_situation.now_volume_of_S >= M - this_situation.now_volume_of_M) 119 { 120 next_situation.now_volume_of_S = this_situation.now_volume_of_S - (M - this_situation.now_volume_of_M); 121 //这句话没必要看,因为思路很简单就是S如果现有的体积更大,就以对方为准去倒,但代码的加加减减看着很乱, 122 next_situation.now_volume_of_M = M; 123 next_situation.now_volume_of_N = this_situation.now_volume_of_N; 124 next_situation.step = this_situation.step + 1; 125 next_situation.who_to_who = 1; 126 if(vis[next_situation.now_volume_of_S][next_situation.now_volume_of_M] == 0) 127 { 128 q.push(next_situation); 129 // cout<<"@0@"<<next_situation.now_volume_of_S<<" "<<next_situation.now_volume_of_N<<" "<<next_situation.now_volume_of_M<<" "<<next_situation.step<<endl; 130 vis[next_situation.now_volume_of_S][next_situation.now_volume_of_M] = 1; 131 } 132 } 133 else if(this_situation.now_volume_of_S < M - this_situation.now_volume_of_M) 134 { 135 next_situation.now_volume_of_M = this_situation.now_volume_of_M + this_situation.now_volume_of_S; 136 next_situation.now_volume_of_S = 0; 137 next_situation.now_volume_of_N = this_situation.now_volume_of_N; 138 next_situation.step = this_situation.step + 1; 139 next_situation.who_to_who = 1; 140 if(vis[next_situation.now_volume_of_S][next_situation.now_volume_of_M] == 0) 141 { 142 q.push(next_situation); 143 // cout<<"@1@"<<next_situation.now_volume_of_S<<" "<<next_situation.now_volume_of_N<<" "<<next_situation.now_volume_of_M<<" "<<next_situation.step<<endl; 144 vis[next_situation.now_volume_of_S][next_situation.now_volume_of_M] = 1; 145 } 146 } 147 148 } 149 150 if(this_situation.who_to_who == 0) 151 ;//N→S 152 else 153 { 154 if(this_situation.now_volume_of_N == 0) 155 ; 156 else if(this_situation.now_volume_of_N >= S - this_situation.now_volume_of_S) 157 { 158 next_situation.now_volume_of_N = this_situation.now_volume_of_N - (S - this_situation.now_volume_of_S); 159 next_situation.now_volume_of_S = S; 160 next_situation.now_volume_of_M = this_situation.now_volume_of_M; 161 next_situation.step = this_situation.step + 1; 162 next_situation.who_to_who = 2; 163 164 if(vis[next_situation.now_volume_of_S][next_situation.now_volume_of_M] == 0) 165 { 166 q.push(next_situation); 167 // cout<<"!0!"<<next_situation.now_volume_of_S<<" "<<next_situation.now_volume_of_N<<" "<<next_situation.now_volume_of_M<<" "<<next_situation.step<<endl; 168 vis[next_situation.now_volume_of_S][next_situation.now_volume_of_M] = 1; 169 } 170 } 171 else if(this_situation.now_volume_of_N < S - this_situation.now_volume_of_S) 172 { 173 next_situation.now_volume_of_S = this_situation.now_volume_of_N + this_situation.now_volume_of_S; 174 next_situation.now_volume_of_N = 0; 175 next_situation.now_volume_of_M = this_situation.now_volume_of_M; 176 next_situation.step = this_situation.step + 1; 177 next_situation.who_to_who = 2; 178 179 if(vis[next_situation.now_volume_of_S][next_situation.now_volume_of_M] == 0) 180 { 181 q.push(next_situation); 182 // cout<<"!1!"<<next_situation.now_volume_of_S<<" "<<next_situation.now_volume_of_N<<" "<<next_situation.now_volume_of_M<<" "<<next_situation.step<<endl; 183 vis[next_situation.now_volume_of_S][next_situation.now_volume_of_M] = 1; 184 } 185 } 186 } 187 188 189 if(this_situation.who_to_who == 4) 190 ;//N→M 191 else 192 { 193 if(this_situation.now_volume_of_N == 0) 194 ; 195 else if(this_situation.now_volume_of_N >= M - this_situation.now_volume_of_M) 196 { 197 next_situation.now_volume_of_N = this_situation.now_volume_of_N - (M - this_situation.now_volume_of_M); 198 next_situation.now_volume_of_M = M; 199 next_situation.now_volume_of_S = this_situation.now_volume_of_S; 200 next_situation.step = this_situation.step + 1; 201 next_situation.who_to_who = 3; 202 203 if(vis[next_situation.now_volume_of_S][next_situation.now_volume_of_M] == 0) 204 { 205 // cout<<"$0$"<<next_situation.now_volume_of_S<<" "<<next_situation.now_volume_of_N<<" "<<next_situation.now_volume_of_M<<" "<<next_situation.step<<endl; 206 q.push(next_situation); 207 vis[next_situation.now_volume_of_S][next_situation.now_volume_of_M] = 1; 208 } 209 } 210 else if(this_situation.now_volume_of_N < M - this_situation.now_volume_of_M) 211 { 212 // cout<<"nm"<<this_situation.now_volume_of_N<<" "<<this_situation.step<<endl; 213 next_situation.now_volume_of_M = this_situation.now_volume_of_M + this_situation.now_volume_of_N; 214 next_situation.now_volume_of_N = 0; 215 next_situation.now_volume_of_S = this_situation.now_volume_of_S; 216 next_situation.step = this_situation.step + 1; 217 next_situation.who_to_who = 3; 218 219 if(vis[next_situation.now_volume_of_S][next_situation.now_volume_of_M] == 0) 220 { 221 q.push(next_situation); 222 // cout<<"$1$"<<next_situation.now_volume_of_S<<" "<<next_situation.now_volume_of_N<<" "<<next_situation.now_volume_of_M<<" "<<next_situation.step<<endl; 223 vis[next_situation.now_volume_of_S][next_situation.now_volume_of_M] = 1; 224 } 225 } 226 } 227 228 if(this_situation.who_to_who == 3) 229 ;//M→N 230 else 231 { 232 if(this_situation.now_volume_of_M == 0) 233 ; 234 else if(this_situation.now_volume_of_M >= N - this_situation.now_volume_of_N) 235 { 236 next_situation.now_volume_of_M = this_situation.now_volume_of_M - (N - this_situation.now_volume_of_N); 237 next_situation.now_volume_of_N = N; 238 next_situation.now_volume_of_S = this_situation.now_volume_of_S; 239 next_situation.step = this_situation.step + 1; 240 next_situation.who_to_who = 4; 241 242 if(vis[next_situation.now_volume_of_S][next_situation.now_volume_of_M] == 0) 243 { 244 // cout<<"%0%"<<next_situation.now_volume_of_S<<" "<<next_situation.now_volume_of_N<<" "<<next_situation.now_volume_of_M<<" "<<next_situation.step<<endl; 245 q.push(next_situation); 246 vis[next_situation.now_volume_of_S][next_situation.now_volume_of_M] = 1; 247 } 248 } 249 else if(this_situation.now_volume_of_M < N - this_situation.now_volume_of_N) 250 { 251 next_situation.now_volume_of_N = this_situation.now_volume_of_N + this_situation.now_volume_of_M; 252 next_situation.now_volume_of_M = 0; 253 next_situation.now_volume_of_S = this_situation.now_volume_of_S; 254 next_situation.step = this_situation.step + 1; 255 next_situation.who_to_who = 4; 256 257 if(vis[next_situation.now_volume_of_S][next_situation.now_volume_of_M] == 0) 258 { 259 // cout<<"%1%"<<next_situation.now_volume_of_S<<" "<<next_situation.now_volume_of_N<<" "<<next_situation.now_volume_of_M<<" "<<next_situation.step<<endl; 260 q.push(next_situation); 261 vis[next_situation.now_volume_of_S][next_situation.now_volume_of_M] = 1; 262 } 263 } 264 } 265 266 if(this_situation.who_to_who == 1) 267 ;//M→S 我想M倒给S,但如果上一步是S倒给M的就别折腾了 268 else 269 { 270 if(this_situation.now_volume_of_M == 0) //M想倒你得有,没有还倒啥 271 ; 272 else if(this_situation.now_volume_of_M >= S - this_situation.now_volume_of_S) //确定有后,如果M此时装的可乐比S还能装的空间大 273 { 274 next_situation.now_volume_of_M = this_situation.now_volume_of_M - (S - this_situation.now_volume_of_S); //则M倒出S能承受的 275 next_situation.now_volume_of_S = S; //S肯定满上了 276 next_situation.now_volume_of_N = this_situation.now_volume_of_N; //跟N没关系,N还用上一个的可乐体积状态 277 next_situation.step = this_situation.step + 1; 278 next_situation.who_to_who = 5; //还用解释? 279 if(vis[next_situation.now_volume_of_S][next_situation.now_volume_of_M] == 0) 280 { 281 // cout<<"&0&"<<next_situation.now_volume_of_S<<" "<<next_situation.now_volume_of_N<<" "<<next_situation.now_volume_of_M<<" "<<next_situation.step<<endl; 282 q.push(next_situation); 283 vis[next_situation.now_volume_of_S][next_situation.now_volume_of_M] = 1; 284 } 285 } 286 else if(this_situation.now_volume_of_M < S - this_situation.now_volume_of_S) //确定有后,如果M此时装的可乐比S还能装的空间小 287 { 288 next_situation.now_volume_of_S = this_situation.now_volume_of_S + this_situation.now_volume_of_M; //那不多逼逼,直接M全射给S 289 next_situation.now_volume_of_M = 0; //M射空了 290 next_situation.now_volume_of_N = this_situation.now_volume_of_N; 291 next_situation.step = this_situation.step + 1; 292 next_situation.who_to_who = 5; 293 if(vis[next_situation.now_volume_of_S][next_situation.now_volume_of_M] == 0) 294 { 295 q.push(next_situation); 296 // cout<<"&1&"<<next_situation.now_volume_of_S<<" "<<next_situation.now_volume_of_N<<" "<<next_situation.now_volume_of_M<<" "<<next_situation.step<<endl; 297 vis[next_situation.now_volume_of_S][next_situation.now_volume_of_M] = 1; 298 } 299 } 300 } 301 } 302 if(flag == 0) 303 cout << "NO" << endl; 304 } 305 }
int vis[101][100] AC
int vis[100][100] WA
又是这个问题,上次oil油田题目,可以理解为字符串末尾的\0会多一个存储空间
我把数组开到了101,但在循环输入后加了一句, if(S>100){ cout<<"CCC"<<endl; continue; } ,发现依旧AC
又把数组改成 int vis[100][101] 也WA
卧槽我突然懂了,输入S容积为100的时候vis100当然溢出了!!!
这下都搞懂以后可以直接无脑开大数组了
但我是真没看懂,为啥
第一个瓶子的容积*(第一个瓶子倒出的次数-倒进的次数)+第二个瓶子的容积*(第二个瓶子倒出的次数-倒进的次数)=(a+b)/2,
倒入倒出的差乘整个容积没道理啊,每次瓶子都是空着的供你倒入倒出?以为会有什么约掉,写了一堆也没发现能约的
再看下面的ax+by=(a+b)/2不更扯犊子么
那也就是2ax+2by=a+b,如果我令2x是X,2y是Y,那aX+bY=a+b???XY都是次数,这取啥值会成立不用我说了吧
但hdoj能AC肯定有点东西,先搁置
这tm不知道是哪个大佬笔误写的,然后全网都抄袭他的博客,都一模一样的分析解释,因为用4 1 3实际例子去代入那个s.t.根本不成立
看了这个博客,发现min函数可以直接用,之前C语言都手写,现在有了现成的min函数可以在模拟的时候用min简便代码了
网上一堆烂博客,实操发现,min跟cout一样,只需要这两句 #include<iostream> using namespace std;
hdoj你真是好的很啊,题目下面的discuss又上不去了,想看个讨论都看不成md
期间遇到的坑:
###:感觉有必要把标题或者文章摘要写成“XXX没思路”(后来发现ACGO不错啊)
因为我就是实在想不出来就总会搜“XXX没思路”,因为不想看答案,只想给个提示,但没这种文章,只能放大300%网页,一句一句读,哪怕看到题解也会尽量过滤掉,尽量忽略题解和提示,这样想破脑袋的刷题才有用,让题解博客起到哪里想不下去断了思路,去接上的作用,然后继续想(q神校友51nod站长想破脑袋和高中数学女老师的学习方法),而不是全部都看,这样题等于没刷。需要啥用啥,别上来就无脑敲一堆模版,有时候题都是不一样的模板打一堆发现是变种,那模板反而扰乱思路。江苏铜牌考上交那个说都这么大了不能老刷题了,他找C++学了老多东西,找我我不跟着学。大厂算法题远没acm难吧,知乎我评论过竞赛难度比较,为啥那些大acm的没高薪的感觉,反而二本的宣城听说都赚挺多呵呵。貌似很多打竞赛的好多都没太好的薪资,那些大厂的写公众号的他们牛在哪
###:
1、不能连等,连等1==1==1有输出是巧合,上面有说
2、memset0 -1 1中0和-1也是巧合,memset1就显现了,具体之前博客有说
3、关于scanf/cin返回值之前研究过了,这里再回顾下整型变量输入浮点型会输出啥,不错的博客解释,里面运行不是他写的那个结果,但他的解释很棒,抛砖引玉
1 int main() 2 { 3 // int a; 4 // int b; 5 // cin>>a; 6 // cin>>b; 7 // cout<<a<<" "<<b<<endl; 8 9 // int c; 10 // int d; 11 // scanf("%d%d",&c,&d); 12 // printf("%d %d\n",c,d); 13 14 // int e; 15 // printf("%d\n",scanf("%d",&e)); 16 17 // int f; 18 // printf("%d\n",cin>>f);//之前说过,没返回值 19 }
###:cout输出double类型的时候不像printf,搞那么多位,实际是几位就是几位,没有多余的0
1 #include<stdio.h> 2 #include<iostream> 3 using namespace std; 4 int main() 5 { 6 double a=1; 7 cout<<a<<endl; 8 printf("%f\n",a); 9 10 double b=1.534; 11 cout<<b<<endl; 12 }
###:妈的自己写的这代码变量名称太难维护了,以后再也不写这么长了
###:对拍代码
1 //#include<bits/stdc++.h> 2 #include<stdio.h> 3 #include<iostream> 4 #include<time.h> 5 using namespace std; 6 char x[3]={'@','.','#'}; 7 char map[100][100]; 8 int main() { 9 srand(time(0) + (unsigned long long)(new char)); 10 11 int S = rand() % 100+1; 12 int N = rand() % S+1; 13 int M = S-N; 14 15 cout<<S<<" "<<N<<" "<<M<<endl; 16 17 }
###:
1 虹HIT复试就是这个题,原题