@@ -180,6 +180,208 @@ class Solution {
180
180
}
181
181
```
182
182
183
+ #### C++
184
+
185
+ ``` cpp
186
+ class Solution {
187
+ public:
188
+ int minimumMoves(vector<vector<int >>& grid) {
189
+ queue<string > q;
190
+ q.push(f(grid));
191
+ unordered_set<string > vis;
192
+ vis.insert(f(grid));
193
+ vector<int > dirs = {-1, 0, 1, 0, -1};
194
+
195
+ for (int ans = 0;; ++ans) {
196
+ int sz = q.size();
197
+ while (sz--) {
198
+ string p = q.front();
199
+ q.pop();
200
+ if (p == "111111111") {
201
+ return ans;
202
+ }
203
+ vector<vector<int >> cur = g(p);
204
+
205
+ for (int i = 0 ; i < 3 ; ++i) {
206
+ for (int j = 0; j < 3; ++j) {
207
+ if (cur[i][j] > 1) {
208
+ for (int d = 0; d < 4; ++d) {
209
+ int x = i + dirs[d];
210
+ int y = j + dirs[d + 1];
211
+ if (x >= 0 && x < 3 && y >= 0 && y < 3 && cur[x][y] < 2) {
212
+ vector<vector<int>> nxt = cur;
213
+ nxt[i][j]--;
214
+ nxt[x][y]++;
215
+ string s = f(nxt);
216
+ if (!vis.count(s)) {
217
+ vis.insert(s);
218
+ q.push(s);
219
+ }
220
+ }
221
+ }
222
+ }
223
+ }
224
+ }
225
+ }
226
+ }
227
+ }
228
+
229
+ private:
230
+ string f(const vector<vector<int >>& grid) {
231
+ string s;
232
+ for (const auto& row : grid) {
233
+ for (int x : row) {
234
+ s += to_string(x);
235
+ }
236
+ }
237
+ return s;
238
+ }
239
+
240
+ vector<vector<int>> g(const string& s) {
241
+ vector<vector<int>> grid(3, vector<int>(3));
242
+ for (int i = 0; i < 3; ++i) {
243
+ for (int j = 0; j < 3; ++j) {
244
+ grid[i][j] = s[i * 3 + j] - '0';
245
+ }
246
+ }
247
+ return grid;
248
+ }
249
+ };
250
+ ```
251
+
252
+ #### Go
253
+
254
+ ``` go
255
+ type Queue []string
256
+
257
+ func (q *Queue ) Push (s string ) {
258
+ *q = append (*q, s)
259
+ }
260
+
261
+ func (q *Queue ) Pop () string {
262
+ s := (*q)[0 ]
263
+ *q = (*q)[1 :]
264
+ return s
265
+ }
266
+
267
+ func (q *Queue ) Empty () bool {
268
+ return len (*q) == 0
269
+ }
270
+
271
+ func minimumMoves (grid [][]int ) int {
272
+ q := Queue{f (grid)}
273
+ vis := map [string ]bool {f (grid): true }
274
+ dirs := []int {-1 , 0 , 1 , 0 , -1 }
275
+
276
+ for ans := 0 ; ; ans++ {
277
+ sz := len (q)
278
+ for ; sz > 0 ; sz-- {
279
+ p := q.Pop ()
280
+ if p == " 111111111" {
281
+ return ans
282
+ }
283
+ cur := g (p)
284
+
285
+ for i := 0 ; i < 3 ; i++ {
286
+ for j := 0 ; j < 3 ; j++ {
287
+ if cur[i][j] > 1 {
288
+ for d := 0 ; d < 4 ; d++ {
289
+ x , y := i+dirs[d], j+dirs[d+1 ]
290
+ if x >= 0 && x < 3 && y >= 0 && y < 3 && cur[x][y] < 2 {
291
+ nxt := make ([][]int , 3 )
292
+ for r := range nxt {
293
+ nxt[r] = append ([]int (nil ), cur[r]...)
294
+ }
295
+ nxt[i][j]--
296
+ nxt[x][y]++
297
+ s := f (nxt)
298
+ if !vis[s] {
299
+ vis[s] = true
300
+ q.Push (s)
301
+ }
302
+ }
303
+ }
304
+ }
305
+ }
306
+ }
307
+ }
308
+ }
309
+ }
310
+
311
+ func f (grid [][]int ) string {
312
+ var sb strings.Builder
313
+ for _ , row := range grid {
314
+ for _ , x := range row {
315
+ sb.WriteByte (byte (x) + ' 0' )
316
+ }
317
+ }
318
+ return sb.String ()
319
+ }
320
+
321
+ func g (s string ) [][]int {
322
+ grid := make ([][]int , 3 )
323
+ for i := range grid {
324
+ grid[i] = make ([]int , 3 )
325
+ for j := 0 ; j < 3 ; j++ {
326
+ grid[i][j] = int (s[i*3 +j] - ' 0' )
327
+ }
328
+ }
329
+ return grid
330
+ }
331
+ ```
332
+
333
+ #### TypeScript
334
+
335
+ ``` ts
336
+ function minimumMoves(grid : number [][]): number {
337
+ const q: string [] = [f (grid )];
338
+ const vis: Set <string > = new Set ([f (grid )]);
339
+ const dirs: number [] = [- 1 , 0 , 1 , 0 , - 1 ];
340
+
341
+ for (let ans = 0 ; ; ans ++ ) {
342
+ let sz = q .length ;
343
+ while (sz -- > 0 ) {
344
+ const p = q .shift ()! ;
345
+ if (p === ' 111111111' ) {
346
+ return ans ;
347
+ }
348
+ const cur = g (p );
349
+
350
+ for (let i = 0 ; i < 3 ; i ++ ) {
351
+ for (let j = 0 ; j < 3 ; j ++ ) {
352
+ if (cur [i ][j ] > 1 ) {
353
+ for (let d = 0 ; d < 4 ; d ++ ) {
354
+ const x = i + dirs [d ],
355
+ y = j + dirs [d + 1 ];
356
+ if (x >= 0 && x < 3 && y >= 0 && y < 3 && cur [x ][y ] < 2 ) {
357
+ const nxt = cur .map (row => [... row ]);
358
+ nxt [i ][j ]-- ;
359
+ nxt [x ][y ]++ ;
360
+ const s = f (nxt );
361
+ if (! vis .has (s )) {
362
+ vis .add (s );
363
+ q .push (s );
364
+ }
365
+ }
366
+ }
367
+ }
368
+ }
369
+ }
370
+ }
371
+ }
372
+ }
373
+
374
+ function f(grid : number [][]): string {
375
+ return grid .flat ().join (' ' );
376
+ }
377
+
378
+ function g(s : string ): number [][] {
379
+ return Array .from ({ length: 3 }, (_ , i ) =>
380
+ Array .from ({ length: 3 }, (_ , j ) => Number (s [i * 3 + j ])),
381
+ );
382
+ }
383
+ ```
384
+
183
385
<!-- tabs:end -->
184
386
185
387
<!-- solution:end -->
0 commit comments