POJ3414-Pots
继续刷邝斌飞搜索专题
这题拿到手就感觉很简单啊
题意:俩容积为A和B的空罐子,给你三种操作,
1:FILL( i ) (1 ≤ i ≤ 2) ,代表 i 从水龙头接满水
2:DROP( i ) , i 倒入下水道
3:POUR( i , j ) ,i 给 j 倒,可能 j 满了 i 还有,也可能 j 不满,但 i 空了
问你最少几次操作后可以得到容积C
这不是之前那个非常可乐么?
之前就说过,可用平台的评分确实不准,这个题居然是变态难度???
AC代码
1 #include<stdio.h> 2 #include<queue> 3 #include<iostream> 4 using namespace std; 5 #include<map> 6 #include <sstream> 7 8 int A,B,C; 9 struct States{ 10 int newA; 11 int newB; 12 string operation_process; 13 // 1代表给A灌满 14 // 2代表给B灌满 15 // 3代表清空A 16 // 4代表清空B 17 // 5代表A倒给B 18 // 6代表B倒给A 19 int step; 20 }; 21 queue<States>q; 22 States firststates; 23 States thisstates; 24 States nextstates; 25 int flag; 26 map<string,int>vis; 27 28 stringstream ssA; 29 stringstream ssB; 30 31 int main() 32 { 33 while(cin>>A>>B>>C){ 34 vis.clear(); 35 while(!q.empty()) 36 q.pop(); 37 flag=0; 38 firststates.newA=0; 39 firststates.newB=0; 40 firststates.operation_process={}; 41 firststates.step=0; 42 string first="00"; 43 vis[first]++; 44 q.push(firststates); 45 while(!q.empty()) 46 { 47 thisstates=q.front(); 48 q.pop(); 49 if(thisstates.newA==C||thisstates.newB==C){ 50 cout<<thisstates.step<<endl; 51 52 int len=thisstates.operation_process.size(); 53 for(int i=0;i<len;i++){ 54 if(thisstates.operation_process[i]=='1') 55 cout<<"FILL(1)"<<endl; 56 if(thisstates.operation_process[i]=='2') 57 cout<<"FILL(2)"<<endl; 58 if(thisstates.operation_process[i]=='3') 59 cout<<"DROP(1)"<<endl; 60 if(thisstates.operation_process[i]=='4') 61 cout<<"DROP(2)"<<endl; 62 if(thisstates.operation_process[i]=='5') 63 cout<<"POUR(1,2)"<<endl; 64 if(thisstates.operation_process[i]=='6') 65 cout<<"POUR(2,1)"<<endl; 66 } 67 68 flag=1; 69 break; 70 } 71 if(thisstates.newA<A){ 72 nextstates.newA=A; 73 nextstates.newB=thisstates.newB; 74 nextstates.operation_process=thisstates.operation_process+'1'; 75 nextstates.step=thisstates.step+1; 76 77 ssA<<nextstates.newA; 78 string strA = ssA.str(); 79 ssB<<nextstates.newB; 80 string strB = ssB.str(); 81 82 if(!vis.count(strA+strB)){ 83 vis[strA+strB]++; 84 85 ssA.clear(); 86 ssA.str(""); 87 ssB.clear(); 88 ssB.str(""); 89 90 q.push(nextstates); 91 } 92 } 93 if(thisstates.newB<B){ 94 nextstates.newA=thisstates.newA; 95 nextstates.newB=B; 96 nextstates.operation_process=thisstates.operation_process+'2'; 97 nextstates.step=thisstates.step+1; 98 99 ssA<<nextstates.newA; 100 string strA = ssA.str(); 101 ssB<<nextstates.newB; 102 string strB = ssB.str(); 103 104 if(!vis.count(strA+strB)){ 105 vis[strA+strB]++; 106 107 ssA.clear(); 108 ssA.str(""); 109 ssB.clear(); 110 ssB.str(""); 111 112 q.push(nextstates); 113 } 114 } 115 if(thisstates.newA>0){ 116 nextstates.newA=0; 117 nextstates.newB=thisstates.newB; 118 nextstates.operation_process=thisstates.operation_process+'3'; 119 nextstates.step=thisstates.step+1; 120 121 ssA<<nextstates.newA; 122 string strA = ssA.str(); 123 ssB<<nextstates.newB; 124 string strB = ssB.str(); 125 126 if(!vis.count(strA+strB)){ 127 vis[strA+strB]++; 128 129 ssA.clear(); 130 ssA.str(""); 131 ssB.clear(); 132 ssB.str(""); 133 134 q.push(nextstates); 135 } 136 137 } 138 if(thisstates.newB>0){ 139 nextstates.newA=thisstates.newA; 140 nextstates.newB=0; 141 nextstates.operation_process=thisstates.operation_process+'4'; 142 nextstates.step=thisstates.step+1; 143 144 ssA<<nextstates.newA; 145 string strA = ssA.str(); 146 ssB<<nextstates.newB; 147 string strB = ssB.str(); 148 149 if(!vis.count(strA+strB)){ 150 vis[strA+strB]++; 151 152 ssA.clear(); 153 ssA.str(""); 154 ssB.clear(); 155 ssB.str(""); 156 157 q.push(nextstates); 158 } 159 160 161 } 162 if(thisstates.newA>0&&thisstates.newB!=B){ 163 if(thisstates.newA>B-thisstates.newB){ 164 nextstates.newB=B; 165 nextstates.newA=thisstates.newA-(B-thisstates.newB); 166 } 167 if(thisstates.newA<B-thisstates.newB){ 168 nextstates.newA=0; 169 nextstates.newB=thisstates.newB+thisstates.newA; 170 } 171 nextstates.operation_process=thisstates.operation_process+'5'; 172 nextstates.step=thisstates.step+1; 173 174 ssA<<nextstates.newA; 175 string strA = ssA.str(); 176 ssB<<nextstates.newB; 177 string strB = ssB.str(); 178 179 if(!vis.count(strA+strB)){ 180 vis[strA+strB]++; 181 182 ssA.clear(); 183 ssA.str(""); 184 ssB.clear(); 185 ssB.str(""); 186 187 q.push(nextstates); 188 } 189 } 190 if(thisstates.newB>0&&thisstates.newA!=A){ 191 if(thisstates.newB>A-thisstates.newA){ 192 nextstates.newA=A; 193 nextstates.newB=thisstates.newB-(A-thisstates.newA); 194 } 195 if(thisstates.newB<A-thisstates.newA){ 196 nextstates.newB=0; 197 nextstates.newA=thisstates.newA+thisstates.newB; 198 } 199 nextstates.operation_process=thisstates.operation_process+'6'; 200 nextstates.step=thisstates.step+1; 201 202 ssA<<nextstates.newA; 203 string strA = ssA.str(); 204 ssB<<nextstates.newB; 205 string strB = ssB.str(); 206 207 if(!vis.count(strA+strB)){ 208 vis[strA+strB]++; 209 210 ssA.clear(); 211 ssA.str(""); 212 ssB.clear(); 213 ssB.str(""); 214 215 q.push(nextstates); 216 } 217 } 218 } 219 if(flag==0) 220 cout<<"impossible"<<endl; 221 } 222 }
改用另一种int转string的方法,就不涉及释放内存的事了(见下面的3、)
AC代码(to_string)
1 #include<stdio.h> 2 #include<queue> 3 #include<iostream> 4 using namespace std; 5 #include<map> 6 #include <sstream> 7 8 int A,B,C; 9 struct States{ 10 int newA; 11 int newB; 12 string operation_process; 13 // 1代表给A灌满 14 // 2代表给B灌满 15 // 3代表清空A 16 // 4代表清空B 17 // 5代表A倒给B 18 // 6代表B倒给A 19 int step; 20 }; 21 queue<States>q; 22 States firststates; 23 States thisstates; 24 States nextstates; 25 int flag; 26 map<string,int>vis; 27 28 29 int main() 30 { 31 while(cin>>A>>B>>C){ 32 vis.clear(); 33 while(!q.empty()) 34 q.pop(); 35 flag=0; 36 firststates.newA=0; 37 firststates.newB=0; 38 firststates.operation_process={}; 39 firststates.step=0; 40 string first="00"; 41 vis[first]++; 42 q.push(firststates); 43 while(!q.empty()) 44 { 45 thisstates=q.front(); 46 q.pop(); 47 if(thisstates.newA==C||thisstates.newB==C){ 48 cout<<thisstates.step<<endl; 49 50 int len=thisstates.operation_process.size(); 51 for(int i=0;i<len;i++){ 52 if(thisstates.operation_process[i]=='1') 53 cout<<"FILL(1)"<<endl; 54 if(thisstates.operation_process[i]=='2') 55 cout<<"FILL(2)"<<endl; 56 if(thisstates.operation_process[i]=='3') 57 cout<<"DROP(1)"<<endl; 58 if(thisstates.operation_process[i]=='4') 59 cout<<"DROP(2)"<<endl; 60 if(thisstates.operation_process[i]=='5') 61 cout<<"POUR(1,2)"<<endl; 62 if(thisstates.operation_process[i]=='6') 63 cout<<"POUR(2,1)"<<endl; 64 } 65 66 flag=1; 67 break; 68 } 69 if(thisstates.newA<A){ 70 nextstates.newA=A; 71 nextstates.newB=thisstates.newB; 72 nextstates.operation_process=thisstates.operation_process+'1'; 73 nextstates.step=thisstates.step+1; 74 75 string strA=to_string(nextstates.newA); 76 string strB=to_string(nextstates.newB); 77 78 if(!vis.count(strA+strB)){ 79 vis[strA+strB]++; 80 q.push(nextstates); 81 } 82 } 83 if(thisstates.newB<B){ 84 nextstates.newA=thisstates.newA; 85 nextstates.newB=B; 86 nextstates.operation_process=thisstates.operation_process+'2'; 87 nextstates.step=thisstates.step+1; 88 89 string strA=to_string(nextstates.newA); 90 string strB=to_string(nextstates.newB); 91 92 93 if(!vis.count(strA+strB)){ 94 vis[strA+strB]++; 95 96 q.push(nextstates); 97 } 98 } 99 if(thisstates.newA>0){ 100 nextstates.newA=0; 101 nextstates.newB=thisstates.newB; 102 nextstates.operation_process=thisstates.operation_process+'3'; 103 nextstates.step=thisstates.step+1; 104 105 string strA=to_string(nextstates.newA); 106 string strB=to_string(nextstates.newB); 107 108 if(!vis.count(strA+strB)){ 109 vis[strA+strB]++; 110 111 q.push(nextstates); 112 } 113 114 } 115 if(thisstates.newB>0){ 116 nextstates.newA=thisstates.newA; 117 nextstates.newB=0; 118 nextstates.operation_process=thisstates.operation_process+'4'; 119 nextstates.step=thisstates.step+1; 120 121 string strA=to_string(nextstates.newA); 122 string strB=to_string(nextstates.newB); 123 124 125 if(!vis.count(strA+strB)){ 126 vis[strA+strB]++; 127 128 q.push(nextstates); 129 } 130 131 } 132 if(thisstates.newA>0&&thisstates.newB!=B){ 133 if(thisstates.newA>B-thisstates.newB){ 134 nextstates.newB=B; 135 nextstates.newA=thisstates.newA-(B-thisstates.newB); 136 } 137 if(thisstates.newA<B-thisstates.newB){ 138 nextstates.newA=0; 139 nextstates.newB=thisstates.newB+thisstates.newA; 140 } 141 nextstates.operation_process=thisstates.operation_process+'5'; 142 nextstates.step=thisstates.step+1; 143 144 145 string strA=to_string(nextstates.newA); 146 string strB=to_string(nextstates.newB); 147 148 if(!vis.count(strA+strB)){ 149 vis[strA+strB]++; 150 151 q.push(nextstates); 152 } 153 } 154 if(thisstates.newB>0&&thisstates.newA!=A){ 155 if(thisstates.newB>A-thisstates.newA){ 156 nextstates.newA=A; 157 nextstates.newB=thisstates.newB-(A-thisstates.newA); 158 } 159 if(thisstates.newB<A-thisstates.newA){ 160 nextstates.newB=0; 161 nextstates.newA=thisstates.newA+thisstates.newB; 162 } 163 nextstates.operation_process=thisstates.operation_process+'6'; 164 nextstates.step=thisstates.step+1; 165 166 string strA=to_string(nextstates.newA); 167 string strB=to_string(nextstates.newB); 168 169 if(!vis.count(strA+strB)){ 170 vis[strA+strB]++; 171 q.push(nextstates); 172 } 173 } 174 } 175 if(flag==0) 176 cout<<"impossible"<<endl; 177 } 178 }
不想看题解了,有说链表的有说其他的,后面再学这些吧
这几个问题的百度智能回答不错
1、C++map输出每对元素(不咋会输出,抄过来找找感觉)
1 #include <iostream> 2 #include <map> 3 4 int main() { 5 std::map<int, std::string> exampleMap = { 6 {1, "One"}, 7 {2, "Two"}, 8 {3, "Three"} 9 }; 10 11 for (const auto &pair : exampleMap) { 12 std::cout << "Key: " << pair.first << " Value: " << pair.second << std::endl; 13 } 14 15 return 0; 16 }
2、string赋值
3、C++int转string,百度给了三种,我选了第二种,结果写的时候,我思路是比如3 5 4,第一次A满上状态我是用map[A+B],即为30,第二次B满上,状态应该是05,但tmd状态存成了3005,把第一次的A是3,B是0,跟这次的A是0,B是5也存进来了
后来查博客发现要释放内存,卡了我好久
有趣的发现:
些许慰藉,发现现在脑子库库直冒出来的思路都是之前想破脑袋才想出来的东西,看题解的基本都忘了
BFS挺无脑的,深搜才牛逼,递归函数不好想
切题比撸管子有意思多了