POTS || 最少操作数问题
一眼思路题。思路比较明显做法也比较套路,只不过实现过程中重复的项比较多写起来比较麻烦不过写的时候基本也不用动脑子按照题目的要求一点一点实现就好了。
只不过昨天做的时候快做完了结果教室关门了只好留到今天做,今天早上不想接着昨晚的写了直接删了重写。做到最后发现卡到输出方案了,因为我在做bfs的时候是开了个结构体记录但是没有想好怎么维护所有情况,然后就有点小烦躁因为这个代码相对来说稍微一点点长然后再重写就很浪费时间,而且这种简单题也不想浪费太多时间,但是a掉还总感觉有点难受,然后在纠结要不要重写的时候发现其实bfs完全可以做到就是说搜到了答案立马返回就好了,没必要最后求完所有情况再寻找最优解(况且这个题最后找最优解也是有点小烦),因为bfs的两段性所以第一次搜到的情况肯定是最优解了,直接返回就好了正好可以完成,然后第一次测样例发现错的很离谱,没有什么奇怪的错误就是答案错了,找了好久发现一个小地方把y打成1了。。。改完立马对了,直接交了结果很快的WA了,很懵逼的手算了几个数据都没问题,就开始找拼写错误,来回找也没找到,测得样例也都没问题,一气之下我直接拿了个评论区的代码和我的代码拍了一下,结果发现是我的输出多了个空格("POUR(i,j)"写成了"POUR(i, j)")。额就十分无语,真的眼瞎看不出来反正。
然后回到这个题,反正就是说bfs求最优解的时候可以直接搜到了提前返回没必要把所有情况都搜到,然后就是这个题要输出方案,记录这个string的时候其实可以暴力来做不用用别的东西维护,只需要结构体里开一个,然后每次加方案就直接硬在上一个方案的操作字符串后面加就行了,因为每一个方案其实只有一个' ) ',所以输出的时候遇到' ) '直接输出一个换行就可以了,当然如果遇到的题没有这种特殊性质,也可以每次加方案的时候特地加一个空格就好了,同理输出的时候每次遇到空格就输出一个回车。
代码 :
1 #include <iostream> 2 #include <queue> 3 #include <cstring> 4 #include <algorithm> 5 #define gogo ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); 6 using namespace std; 7 //using i64 = long long; 8 //#define endl '\n'; 9 const string YES = "Yes"; 10 const string NO = "No"; 11 const int N = 1010; 12 struct info { 13 int a, b; 14 string s; 15 info(int aa, int bb, string ss) : a(aa), b(bb), s(ss) {} 16 }; 17 bool st[N][N]; 18 int dist[N][N]; 19 int a, b, c; 20 bool check(int x, int y) { 21 return (x == c || y == c); 22 } 23 void dis(string s) { 24 int n = s.size(); 25 for (int i = 0;i < n;i ++) { 26 cout << s[i]; 27 if (s[i] == ')') 28 cout << '\n'; 29 } 30 } 31 32 int bfs() { 33 memset(dist, 0x3f, sizeof dist); 34 queue<info> q; 35 q.push(info(0, 0, "")); 36 st[0][0] = true; 37 dist[0][0] = 0; 38 39 while (q.size()) { 40 info t = q.front(); 41 q.pop(); 42 43 //1 44 for (int i = 0;i < 2;i ++) { 45 if (i == 0) { 46 int x = t.a, y = t.b; 47 int nx = a, ny = t.b; 48 if (!st[nx][ny]) { 49 st[nx][ny] = true; 50 dist[nx][ny] = dist[x][y] + 1; 51 string ns = t.s + "FILL(1)"; 52 q.push(info(nx, ny, ns)); 53 if (check(nx, ny)) { 54 cout << dist[nx][ny] << '\n'; 55 dis(ns); 56 return 0; 57 } 58 } 59 } else { 60 int x = t.a, y = t.b; 61 int nx = t.a, ny = b; 62 if (!st[nx][ny]) { 63 st[nx][ny] = true; 64 dist[nx][ny] = dist[x][y] + 1; 65 q.push(info(nx, ny, t.s + "FILL(2)")); 66 string ns = t.s + "FILL(2)"; 67 if (check(nx, ny)) { 68 cout << dist[nx][ny] << '\n'; 69 dis(ns); 70 return 0; 71 } 72 } 73 } 74 } 75 76 //2 77 for (int i = 0;i < 2;i ++) { 78 if (i == 1) { 79 int x = t.a, y = t.b; 80 int nx = 0, ny = t.b; 81 if (!st[nx][ny]) { 82 st[nx][ny] = true; 83 dist[nx][ny] = dist[x][y] + 1; 84 q.push(info(nx, ny, t.s + "DROP(1)")); 85 string ns = t.s + "DROP(1)"; 86 if (check(nx, ny)) { 87 cout << dist[nx][ny] << '\n'; 88 dis(ns); 89 return 0; 90 } 91 } 92 } else { 93 int x = t.a, y = t.b; 94 int nx = t.a, ny = 0; 95 if (!st[nx][ny]) { 96 st[nx][ny] = true; 97 dist[nx][ny] = dist[x][y] + 1; 98 q.push(info(nx, ny, t.s + "DROP(2)")); 99 string ns = t.s + "DROP(2)"; 100 if (check(nx, ny)) { 101 cout << dist[nx][ny] << '\n'; 102 dis(ns); 103 return 0; 104 } 105 } 106 } 107 } 108 109 //3 110 for (int i = 0;i < 2;i ++) { 111 if (i == 1) { 112 int x = t.a, y = t.b; 113 int na = max(0, x + y - b), nb = min(y + x, b); 114 if (!st[na][nb]) { 115 st[na][nb] = true; 116 dist[na][nb] = dist[x][y] + 1; 117 q.push(info(na, nb, t.s + "POUR(1,2)")); 118 string ns = t.s + "POUR(1,2)"; 119 if (check(na, nb)) { 120 cout << dist[na][nb] << '\n'; 121 dis(ns); 122 return 0; 123 } 124 } 125 } else { 126 int x = t.a, y = t.b; 127 int na = min(x + y, a), nb = max(0, x + y - a); 128 if (!st[na][nb]) { 129 st[na][nb] = true; 130 dist[na][nb] = dist[x][y] + 1; 131 q.push(info(na, nb, t.s + "POUR(2,1)")); 132 string ns = t.s + "POUR(2,1)"; 133 if (check(na, nb)) { 134 // cout << "SS" << ' ' << t.a << ' ' << t.b << endl; 135 // cout << "NN" << ' ' << na << ' ' << nb << endl; 136 cout << dist[na][nb] << '\n';xx 137 dis(ns); 138 return 0; 139 } 140 } 141 } 142 } 143 } 144 return -1; 145 } 146 int main() { 147 gogo; 148 cin >> a >> b >> c; 149 int ans = bfs(); 150 if (ans == -1) 151 cout << "impossible" << '\n'; 152 }

浙公网安备 33010602011771号