Submission #80321
ソースコード
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | #include<bits/stdc++.h> using namespace std; map<vector< int >, int >ma; //ofstream arrr("kariout.txt"); main(){ vector< int >a(9); for ( int i=0;i<8;i++){ a[i]=i+1; } queue<vector< int >>q; q.push(a); ma[a]=0; while (!q.empty()){ a=q.front(); q.pop(); int x,y; for ( int i=0;i<9;i++){ if (a[i]==0){ x=i/3; y=i%3; } } if (x!=0){ vector< int >b(9); b=a; swap(b[x*3+y],b[x*3+y-3]); if (!ma.count(b)){ ma[b]=ma[a]+1; q.push(b); } } if (x!=2){ vector< int >b(9); b=a; swap(b[x*3+y],b[x*3+y+3]); if (!ma.count(b)){ ma[b]=ma[a]+1; q.push(b); } } if (y!=0){ vector< int >b(9); b=a; swap(b[x*3+y],b[x*3+y-1]); if (!ma.count(b)){ ma[b]=ma[a]+1; q.push(b); } } if (y!=2){ vector< int >b(9); b=a; swap(b[x*3+y],b[x*3+y+1]); if (!ma.count(b)){ ma[b]=ma[a]+1; q.push(b); } } } int w;cin>>w; while (w--){ vector< int >c(9); for ( int i=0;i<9;i++){ cin>>c[i]; } if (ma.count(c)){ cout<<ma[c]<< "\n" ; } else { cout<< "-1\n" ; } } } |
ステータス
項目 | データ |
---|---|
問題 | 1832 - 3×3Puzzle |
ユーザー名 | ei2326 |
投稿日時 | 2024-08-07 11:33:54 |
言語 | C++17 |
状態 | Accepted |
得点 | 1230 |
ソースコード長 | 1615 Byte |
最大実行時間 | 732 ms |
最大メモリ使用量 | 24064 KB |
セット
セット | 得点 | Cases | |
---|---|---|---|
1 | ALL | 1230 / 1230 | * |
テストケース
ファイル名 | 状態 | 実行時間 | メモリ使用量 | # |
---|---|---|---|---|
in1.txt | AC | 355 ms | 23512 KB |
1
|
in2.txt | AC | 356 ms | 23284 KB |
1
|
in3.txt | AC | 392 ms | 23320 KB |
1
|
in4.txt | AC | 725 ms | 23736 KB |
1
|
in5.txt | AC | 732 ms | 24028 KB |
1
|
in6.txt | AC | 345 ms | 24064 KB |
1
|