Problem:
8 Puzzle
The goal of the 8 puzzle problem is to complete pieces on $3 \times 3$ cells where one of the cells is empty space.
In this problem, the space is represented by 0 and pieces are represented by integers from 1 to 8 as shown below.
1 3 0
4 2 5
7 8 6
You can move a piece toward the empty space at one step. Your goal is to make the pieces the following configuration in the shortest move (fewest steps).
1 2 3
4 5 6
7 8 0
Write a program which reads an initial state of the puzzle and prints the fewest steps to solve the puzzle.
Input
The $3 \times 3$ integers denoting the pieces or space are given.
Output
Print the fewest steps in a line.
Constraints
There is a solution.
Sample Input
1 3 0
4 2 5
7 8 6
Sample Output
4

Submitted solutions:

code_snippet: #include <cstdio>
#include <queue>
#include <map>
#include <string>
#include <algorithm>
#include <iostream>
const int N=3;//计算零点的上下左右
const int N2=9;
using namespace std;
struct Puzzle{
    int f[N2];//拼图按顺序对应的数字
    int space;//零所在的位置
    string path;//对零点实施的各种操作
    bool operator <(const Puzzle&p) const {//定义一种比较规则，这样puzzle才能在map自动比较的数据结构作为键
        for(int i=0;i<N2;i++){
            if(f[i]==p.f[i]) continue;
            return f[i]>p.f[i];
        }
        return false;//所有元素都相等
    }
};
const int dx[4]={-1,1,0,0};//i相同分别对应左右上下的操作
const int dy[4]={0,0,-1,1};
const char dir[4]={'l','r','u','d'};
bool isTarget(Puzzle &p){
    for(int i=0;i<N2;i++){
        if(p.f[i]!=(i+1))//比较各个位置是不是放对位置，123...
        return false;
    }
    return true;
}
string bfs(Puzzle s){
    Puzzle u,v;//u是队列开头，v用来更新队列
    map<Puzzle,bool> V;
    queue<Puzzle> Q;
    s.path="";
    Q.push(s);
    V[s]=true;//访问过
    while(!Q.empty()){
        u=Q.front();Q.pop();
        if(isTarget(u)) return u.path;//拼好图后，返回操作最后转为size就是操作数
        int sx=u.space%N;//求出空格的x，y坐标，我把x弄成横的了，答案是竖的
        int sy=u.space/N;
        for(int r=0;r<4;r++){//对零的上下左右操作
            int tx=sx+dx[r];//求出操作空格的x，y坐标
            int ty=sy+dy[r];
            if(tx<0||ty<0||tx>=N||ty>=N) continue;
            v=u;
            swap(v.f[u.space],v.f[ty*N+tx]);//把操作后的空格移到正确的位置
            v.space=ty*N+tx;
            v.path+=dir[r];//加一步操作
            if(!V[v]){//看看操作后这样的排序有没有被访问过
                V[v]=true;
                Q.push(v);
            }
        }

    }
    return "unsolvable";//此题必有解，所以这个基本没用，但是有返回类型的函数结尾必须加这一句话
}
int main(){
    Puzzle in;
    for(int j=0;j<N2;j++){
        int c;
        scanf("%d",&c);
        if(c==0){
            in.f[j]=N2;//零值变成N2才可以和上面对上
            in.space=j;
        }else in.f[j]=c;
    }
    string g=bfs(in);
    printf("%d\n",(int)g.size());
    return 0;
}
accuracy: 1
time_ms: 190
memory_kb: 33728
final_rank: 12

code_snippet: #include <cstdio>
#include <queue>
#include <map>
#include <string>
#include <algorithm>
#include <iostream>
const int N=3;//计算零点的上下左右
const int N2=9;
using namespace std;
struct Puzzle{
    int f[N2];//拼图按顺序对应的数字
    int space;//零所在的位置
    string path;//对零点实施的各种操作
    bool operator <(const Puzzle&p) const {//定义一种比较规则，这样puzzle才能在map自动比较的数据结构作为键
        for(int i=0;i<N2;i++){
            if(f[i]==p.f[i]) continue;
            return f[i]>p.f[i];
        }
        return false;//所有元素都相等
    }
};
const int dx[4]={-1,1,0,0};//i相同分别对应左右上下的操作
const int dy[4]={0,0,-1,1};
const char dir[4]={'l','r','u','d'};
bool isTarget(Puzzle &p){
    for(int i=0;i<N2;i++){
        if(p.f[i]!=(i+1))//比较各个位置是不是放对位置，123...
        return false;
    }
    return true;
}
string bfs(Puzzle s){
    Puzzle u,v;//u是队列开头，v用来更新队列
    map<Puzzle,bool> V;
    queue<Puzzle> Q;
    s.path="";
    Q.push(s);
    V[s]=true;//访问过
    while(!Q.empty()){
        u=Q.front();Q.pop();
        if(isTarget(u)) return u.path;//拼好图后，返回操作最后转为size就是操作数
        int sx=u.space%N;//求出空格的x，y坐标，我把x弄成横的了，答案是竖的
        int sy=u.space/N;
        for(int r=0;r<4;r++){//对零的上下左右操作
            int tx=sx+dx[r];//求出操作空格的x，y坐标
            int ty=sy+dy[r];
            if(tx<0||ty<0||tx>=N||ty>=N) continue;
            v=u;
            swap(v.f[u.space],v.f[ty*N+tx]);//把操作后的空格移到正确的位置
            v.space=ty*N+tx;
            if(!V[v]){//看看操作后这样的排序有没有被访问过
                V[v]=true;
                v.path+=dir[r];//加一步操作
                Q.push(v);
            }
        }

    }
    return "unsolvable";//此题必有解，所以这个基本没用，但是有返回类型的函数结尾必须加这一句话
}
int main(){
    Puzzle in;
    for(int j=0;j<N2;j++){
        int c;
        scanf("%d",&c);
        if(c==0){
            in.f[j]=N2;//零值变成N2才可以和上面对上
            in.space=j;
        }else in.f[j]=c;
    }
    string g=bfs(in);
    printf("%d\n",(int)g.size());
    return 0;
}
accuracy: 1
time_ms: 190
memory_kb: 32692
final_rank: 8

code_snippet: #include <cstdio>
#include <queue>
#include <map>
#include <string>
#include <algorithm>
#include <iostream>
const int N=3;//计算零点的上下左右
const int N2=9;
using namespace std;
struct Puzzle{
    int f[N2];//拼图按顺序对应的数字
    int space;//零所在的位置
    string path;//对零点实施的各种操作
    bool operator <(const Puzzle&p) const {//定义一种比较规则，这样puzzle才能在map自动比较的数据结构作为键
        for(int i=0;i<N2;i++){
            if(f[i]==p.f[i]) continue;
            return f[i]>p.f[i];
        }
        return false;//所有元素都相等
    }
};
const int dx[4]={-1,1,0,0};//i相同分别对应左右上下的操作
const int dy[4]={0,0,-1,1};
const char dir[4]={'l','r','u','d'};
bool isTarget(Puzzle &p){
    for(int i=0;i<N2;i++){
        if(p.f[i]!=(i+1))//比较各个位置是不是放对位置，123...
        return false;
    }
    return true;
}
string bfs(Puzzle s){
    Puzzle u,v;//u是队列开头，v用来更新队列
    map<Puzzle,bool> V;
    queue<Puzzle> Q;
    s.path="";
    Q.push(s);
    V[s]=true;//访问过
    while(!Q.empty()){
        u=Q.front();Q.pop();
        if(isTarget(u)) return u.path;//拼好图后，返回操作最后转为size就是操作数
        int sx=u.space%N;//求出空格的x，y坐标，我把x弄成横的了，答案是竖的
        int sy=u.space/N;
        for(int r=0;r<4;r++){//对零的上下左右操作
            int tx=sx+dx[r];//求出操作空格的x，y坐标
            int ty=sy+dy[r];
            if(tx<0||ty<0||tx>=N||ty>=N) continue;
            v=u;
            swap(v.f[u.space],v.f[ty*N+tx]);//把操作后的空格移到正确的位置
            v.space=ty*N+tx;
            if(!V[v]){//看看操作后这样的排序有没有被访问过
                V[v]=true;
                v.path+=dir[r];//加一步操作
                Q.push(v);
            }
        }

    }
    return "unsolvable";//此题必有解，所以这个基本没用，但是有返回类型的函数结尾必须加这一句话
}
int main(){
    Puzzle in;
    for(int j=0;j<N2;j++){
        int c;
        scanf("%d",&c);
        if(c==0){
            in.f[j]=N2;//零值变N；才符合上面n+1的判断，才能按顺序放到最后
            in.space=j;
        }else in.f[j]=c;
    }
    string g=bfs(in);
    printf("%d\n",(int)g.size());
    return 0;
}
accuracy: 1
time_ms: 190
memory_kb: 32728
final_rank: 9

code_snippet: #include <bits/stdc++.h>
using namespace std;

//幅優先探索で解く

const int dx[] = {-1, 0, 0, 1};
const int dy[] = {0, 1, -1, 0};

const char dir[] = {'l', 'd', 'u', 'r'};

struct puzzle
{
    int grid[9];
    int n;
    int space = -1;
    string path;

    bool operator < (const puzzle &p) const
    {
        for (int i = 0; i < 9; i++)
        {
            if (grid[i] == p.grid[i]) continue;
            return p.grid[i] > grid[i];
        }

        return false;
    }

    string hash()
    {
        string ans = "";
        for (int i = 0; i < 9; i++) ans += to_string(grid[i]);
        return ans;
    }
};

int bfs()
{
    queue<puzzle> q;
    map<string, bool> checker;

    puzzle s;
    for (int i = 0; i < 9; i++)
    {
        cin >> s.grid[i];
        if (s.grid[i] == 0) s.space = i;
    }
    s.n = 0;
    q.push(s);
    checker[s.hash()] = true;

    if (s.hash() == "123456780") return 0;

    while (!q.empty())
    {
        puzzle c = q.front(); q.pop();
        int i = c.space;

        for (int d = 0; d < 4; d++)
        {
            int ic = i%3; int ir = i/3;
            int mx = ic+dx[d];
            int my = ir+dy[d];
            int m = i+dx[d]+3*dy[d];
            if ( 0<=mx&&mx<3 && 0<=my&&my<3 )
            {
                c.n++;
                swap(c.grid[i], c.grid[m]);
                if ( !checker[c.hash()] )
                {
                    puzzle copy = c;
                    copy.space = m;
                    copy.path += dir[d];

                    if (c.hash() == "123456780")
                    {
                        //cout << copy.path << endl;
                        return c.n;
                    }
                    q.push(copy);

                    checker[c.hash()] = true;
                }
                swap(c.grid[i], c.grid[m]);
                c.n--;
            }
        }
    }

    return 0;
}

int main()
{
    cout << bfs() << endl;

    return 0;
}
accuracy: 1
time_ms: 510
memory_kb: 19572
final_rank: 13

code_snippet: #include <bits/stdc++.h>
using namespace std;

//幅優先探索で解く

const int dx[] = {-1, 0, 0, 1};
const int dy[] = {0, 1, -1, 0};

const char dir[] = {'l', 'd', 'u', 'r'};

struct puzzle
{
    int grid[9];
    int n;
    int space = -1;
    string path;

    bool operator < (const puzzle &p) const
    {
        for (int i = 0; i < 9; i++)
        {
            if (grid[i] == p.grid[i]) continue;
            return p.grid[i] > grid[i];
        }

        return false;
    }

    string hash()
    {
        string ans = "";
        for (int i = 0; i < 9; i++) ans += to_string(grid[i]);
        return ans;
    }
};

int bfs()
{
    queue<puzzle> q;
    map<string, bool> checker;

    puzzle s;
    for (int i = 0; i < 9; i++)
    {
        cin >> s.grid[i];
        if (s.grid[i] == 0) s.space = i;
    }
    s.n = 0;
    q.push(s);
    checker[s.hash()] = true;

    while (!q.empty())
    {
        puzzle c = q.front(); q.pop();
        int i = c.space;

        for (int d = 0; d < 4; d++)
        {
            int ic = i%3; int ir = i/3;
            int mx = ic+dx[d];
            int my = ir+dy[d];
            int m = i+dx[d]+3*dy[d];
            if ( 0<=mx&&mx<3 && 0<=my&&my<3 )
            {
                c.n++;
                swap(c.grid[i], c.grid[m]);
                if ( !checker[c.hash()] )
                {
                    puzzle copy = c;
                    copy.space = m;
                    copy.path += dir[d];

                    if (c.hash() == "123456780")
                    {
                        //cout << copy.path << endl;
                        return c.n;
                    }
                    q.push(copy);

                    checker[c.hash()] = true;
                }
                swap(c.grid[i], c.grid[m]);
                c.n--;
            }
        }
    }

    return -1;
}

int main()
{
    cout << bfs() << endl;

    return 0;
}
accuracy: 0.8214285714285714
time_ms: 510
memory_kb: 19728
final_rank: 16

code_snippet: #include <bits/stdc++.h>
using namespace std;

//幅優先探索で解く

const int dx[] = {-1, 0, 0, 1};
const int dy[] = {0, 1, -1, 0};

struct puzzle
{
    int grid[9];
    int n;
    int space = -1;
    string path;

    bool operator < (const puzzle &p) const
    {
        for (int i = 0; i < 9; i++)
        {
            if (grid[i] == p.grid[i]) continue;
            return p.grid[i] > grid[i];
        }

        return false;
    }

    string hash()
    {
        string ans = "";
        for (int i = 0; i < 9; i++) ans += to_string(grid[i]);
        return ans;
    }

    void print()
    {
        cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;

        for (int i = 0; i < 9;)
        {
            for (int j = i; j+3 > i ; i++)
                cout << grid[i] << " ";
            cout << endl;
        }

        cout << this->hash() << endl;
    }
};

int bfs()
{
    queue<puzzle> q;
    map<string, bool> checker;

    puzzle s;
    for (int i = 0; i < 9; i++)
    {
        cin >> s.grid[i];
        if (s.grid[i] == 0) s.space = i;
    }
    s.n = 0;
    q.push(s);
    checker[s.hash()] = true;

    while (!q.empty())
    {
        puzzle c = q.front(); q.pop();
        int i = c.space;

        for (int d = 0; d < 4; d++)
        {
            int m = i+dx[d]+3*dy[d];
            if ( m >= 0 && m < 9 )
            {
                c.n++;
                swap(c.grid[i], c.grid[m]);
                if ( !checker[c.hash()] )
                {

                    if (c.hash() == "123456780")
                    {
                        return c.n;
                    }

                    int temp = c.space;
                    c.space = m;
                    q.push(c);
                    c.space = temp;
                    checker[c.hash()] = true;
                }
                swap(c.grid[i], c.grid[m]);
                c.n--;
            }
        }
    }

    return -1;
}

int main()
{
    cout << bfs() << endl;

    return 0;
}
accuracy: 0.39285714285714285
time_ms: 20
memory_kb: 4984
final_rank: 17

code_snippet: #include <bits/stdc++.h>
using namespace std;

//幅優先探索で解く

const int dx[] = {-1, 0, 0, 1};
const int dy[] = {0, 1, -1, 0};

struct puzzle
{
    int grid[9];
    int n;
    string path;

    bool operator < (const puzzle &p) const
    {
        for (int i = 0; i < 9; i++)
        {
            if (grid[i] == p.grid[i]) continue;
            return p.grid[i] > grid[i];
        }

        return false;
    }

    string hash()
    {
        string ans = "";
        for (int i = 0; i < 9; i++) ans += to_string(grid[i]);
        return ans;
    }

    void print()
    {
        cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;

        for (int i = 0; i < 9;)
        {
            for (int j = i; j+3 > i ; i++)
                cout << grid[i] << " ";
            cout << endl;
        }

        cout << this->hash() << endl;
    }
};

int bfs()
{
    queue<puzzle> q;
    map<string, bool> checker;

    puzzle s;
    for (int i = 0; i < 9; i++)
    {
        cin >> s.grid[i];
    }
    s.n = 0;
    q.push(s);
    checker[s.hash()] = true;

    while (!q.empty())
    {
        puzzle c = q.front(); q.pop();

        for (int i = 0; i < 9; i++)
        {
            for (int d = 0; d < 4; d++)
            {
                int m = i+dx[d]+3*dy[d];
                if ( m >= 0 && m < 9 )
                {
                    c.n++;
                    swap(c.grid[i], c.grid[m]);
                    if ( !checker[c.hash()] )
                    {

                        if (c.hash() == "123456780")
                        {
                            return c.n;
                        }

                        q.push(c);
                        checker[c.hash()] = true;
                    }
                    swap(c.grid[i], c.grid[m]);
                    c.n--;
                }
            }
        }
    }

    return -1;
}

int main()
{
    cout << bfs() << endl;

    return 0;
}
accuracy: 0.39285714285714285
time_ms: 300
memory_kb: 9720
final_rank: 20

code_snippet: #include <bits/stdc++.h>
using namespace std;

//幅優先探索で解く

const int dx[] = {-1, 0, 0, 1};
const int dy[] = {0, 1, -1, 0};

struct puzzle
{
    int grid[9];
    int n;

    bool operator < (const puzzle &p) const
    {
        for (int i = 0; i < 9; i++)
        {
            if (grid[i] == p.grid[i]) continue;
            return p.grid[i] > grid[i];
        }

        return false;
    }

    string hash()
    {
        string ans = "";
        for (int i = 0; i < 9; i++) ans += to_string(grid[i]);
        return ans;
    }

    void print()
    {
        for (int i = 0; i < 9;)
        {
            for (int j = i; j+3 > i ; i++)
                cout << grid[i] << " ";
            cout << endl;
        }

        cout << this->hash() << endl;

        cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;
    }
};

int bfs()
{
    queue<puzzle> q;
    map<string, bool> checker;

    puzzle s;
    for (int i = 0; i < 9; i++)
    {
        cin >> s.grid[i];
    }
    s.n = 0;
    q.push(s);
    checker[s.hash()] = true;

    while (!q.empty())
    {
        puzzle c = q.front(); q.pop();

        for (int i = 0; i < 9; i++)
        {
            for (int d = 0; d < 4; d++)
            {
                int m = i+dx[d]+3*dy[d];
                if ( m >= 0 && m < 9 )
                {
                    c.n++;
                    swap(c.grid[i], c.grid[m]);
                    if ( !checker[c.hash()] )
                    {
                        //c.print();

                        if (c.hash() == "123456780")
                        {
                            return c.n;
                        }

                        q.push(c);
                        checker[c.hash()] = true;
                    }
                    swap(c.grid[i], c.grid[m]);
                    c.n--;
                }
            }
        }
    }

    return -1;
}

int main()
{
    cout << bfs() << endl;

    return 0;
}
accuracy: 0.39285714285714285
time_ms: 290
memory_kb: 8828
final_rank: 19

code_snippet: #include <bits/stdc++.h>
using namespace std;

//幅優先探索で解く

const int dx[] = {-1, 0, 0, 1};
const int dy[] = {0, 1, -1, 0};
const int ans[]  = {1, 2, 3, 4, 5, 6, 7, 8, 0};

struct puzzle
{
    int grid[9];
    int n;

    bool operator < (const puzzle &p) const
    {
        for (int i = 0; i < 9; i++)
        {
            if (grid[i] == p.grid[i]) continue;
            return p.grid[i] > grid[i];
        }

        return false;
    }

    void print()
    {
        cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;

        for (int i = 0; i < 9;)
        {
            for (int j = i; j+3 > i ; i++)
                cout << grid[i] << " ";
            cout << endl;
        }

        cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;
    }
};

int bfs()
{
    queue<puzzle> q;
    map<puzzle, bool> checker;

    puzzle s;
    for (int i = 0; i < 9; i++)
    {
        cin >> s.grid[i];
    }
    s.n = 0;
    q.push(s);
    checker[s] = true;

    while (!q.empty())
    {
        puzzle c = q.front(); q.pop();

        for (int i = 0; i < 9; i++)
        {
            for (int d = 0; d < 4; d++)
            {
                int m = i+dx[d]+3*dy[d];
                if ( m >= 0 && m < 9 )
                {
                    puzzle copy = c;
                    copy.n = c.n + 1;
                    swap(copy.grid[i], copy.grid[m]);
                    if ( !checker[copy] )
                    {
                        //copy.print();

                        if (equal( copy.grid, copy.grid+9, ans ))
                        {
                            return copy.n;
                        }

                        q.push(copy);
                        checker[copy] = true;
                    }
                }
            }
        }
    }

    return -1;
}

int main()
{
    cout << bfs() << endl;

    return 0;
}
accuracy: 0.39285714285714285
time_ms: 60
memory_kb: 9288
final_rank: 18

code_snippet: #include <vector>
#include <iostream>
#include <cmath>
#include <unordered_set>
using namespace std;
int main()
{
    vector<vector<int>> InitPlacement(3, vector<int>(3));
    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            cin >> InitPlacement.at(i).at(j);
        }
    }
    unordered_set<int> Visited;
    int64_t InitScore = 0;
    int PlayCount = 0;
    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            InitScore += InitPlacement.at(i).at(j) * int64_t(pow(9, 3 * i + j));
        }
    }
    Visited.insert(InitScore);
    vector<vector<int64_t>> AllNowPlacementScore = {{InitScore}};
    while (AllNowPlacementScore.size() > 0 && !Visited.contains(42374116))
    {
        vector<int64_t> NextPlacementScore(0);
        vector<int64_t> NowPlacementScore = AllNowPlacementScore.back();
        AllNowPlacementScore.pop_back();
        for (int64_t Score : NowPlacementScore)
        {
            int64_t CopiedScore = Score;
            vector<vector<int>> ScoreToPlacement(3, vector<int>(3));
            pair<int, int> EmptyPlace = {0, 0};
            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    ScoreToPlacement.at(i).at(j) = Score % 9;
                    if (Score % 9 == 0)
                    {
                        EmptyPlace = {i, j};
                    }
                    Score /= 9;
                }
            }
            if (EmptyPlace.first >= 1)
            {
                int64_t NewScore = CopiedScore + ScoreToPlacement.at(EmptyPlace.first - 1).at(EmptyPlace.second) *
                                                     (int64_t(pow(9, 3 * EmptyPlace.first + EmptyPlace.second)) - int64_t(pow(9, 3 * (EmptyPlace.first - 1) + EmptyPlace.second)));
                if (!Visited.contains(NewScore)){
                    Visited.insert(NewScore);
                    NextPlacementScore.push_back(NewScore);
                }
            }
            if (EmptyPlace.first <= 1)
            {
                int64_t NewScore = CopiedScore + ScoreToPlacement.at(EmptyPlace.first + 1).at(EmptyPlace.second) *
                                                     (int64_t(pow(9, 3 * EmptyPlace.first + EmptyPlace.second)) - int64_t(pow(9, 3 * (EmptyPlace.first + 1) + EmptyPlace.second)));
                if (!Visited.contains(NewScore)){
                    Visited.insert(NewScore);
                    NextPlacementScore.push_back(NewScore);
                }
            }
            if (EmptyPlace.second >= 1)
            {
                int64_t NewScore = CopiedScore + ScoreToPlacement.at(EmptyPlace.first).at(EmptyPlace.second - 1) *
                                                     (int64_t(pow(9, 3 * EmptyPlace.first + EmptyPlace.second)) - int64_t(pow(9, 3 * (EmptyPlace.first) + EmptyPlace.second - 1)));
                if (!Visited.contains(NewScore)){
                    Visited.insert(NewScore);
                    NextPlacementScore.push_back(NewScore);
                }
            }
            if (EmptyPlace.second <= 1)
            {
                int64_t NewScore = CopiedScore + ScoreToPlacement.at(EmptyPlace.first).at(EmptyPlace.second + 1) *
                                                     (int64_t(pow(9, 3 * EmptyPlace.first + EmptyPlace.second)) - int64_t(pow(9, 3 * (EmptyPlace.first) + EmptyPlace.second + 1)));
                if (!Visited.contains(NewScore)){
                    Visited.insert(NewScore);
                    NextPlacementScore.push_back(NewScore);
                }
            }

        }
        PlayCount ++;
        if (NextPlacementScore.size() > 0){
            AllNowPlacementScore.push_back(NextPlacementScore);
        }
    }
    cout << PlayCount << "\n";
}
accuracy: 1
time_ms: 40
memory_kb: 13292
final_rank: 3

code_snippet: #include <vector>
#include <iostream>
#include <cmath>
#include <unordered_set>
using namespace std;
int main()
{
    vector<vector<int>> InitPlacement(3, vector<int>(3));
    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            cin >> InitPlacement.at(i).at(j);
        }
    }
    unordered_set<int> Visited;
    int64_t InitScore = 0;
    int PlayCount = 0;
    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            InitScore += InitPlacement.at(i).at(j) * int64_t(pow(9, 3 * i + j));
        }
    }
    Visited.insert(InitScore);
    vector<vector<int64_t>> AllNowPlacementScore = {{InitScore}};
    while (AllNowPlacementScore.size() > 0)
    {
        vector<int64_t> NextPlacementScore(0);
        vector<int64_t> NowPlacementScore = AllNowPlacementScore.back();
        AllNowPlacementScore.pop_back();
        for (int64_t Score : NowPlacementScore)
        {
            int64_t CopiedScore = Score;
            vector<vector<int>> ScoreToPlacement(3, vector<int>(3));
            pair<int, int> EmptyPlace = {0, 0};
            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    ScoreToPlacement.at(i).at(j) = Score % 9;
                    if (Score % 9 == 0)
                    {
                        EmptyPlace = {i, j};
                    }
                    Score /= 9;
                }
            }
            if (EmptyPlace.first >= 1)
            {
                int64_t NewScore = CopiedScore + ScoreToPlacement.at(EmptyPlace.first - 1).at(EmptyPlace.second) *
                                                     (int64_t(pow(9, 3 * EmptyPlace.first + EmptyPlace.second)) - int64_t(pow(9, 3 * (EmptyPlace.first - 1) + EmptyPlace.second)));
                if (!Visited.contains(NewScore)){
                    Visited.insert(NewScore);
                    NextPlacementScore.push_back(NewScore);
                }
            }
            if (EmptyPlace.first <= 1)
            {
                int64_t NewScore = CopiedScore + ScoreToPlacement.at(EmptyPlace.first + 1).at(EmptyPlace.second) *
                                                     (int64_t(pow(9, 3 * EmptyPlace.first + EmptyPlace.second)) - int64_t(pow(9, 3 * (EmptyPlace.first + 1) + EmptyPlace.second)));
                if (!Visited.contains(NewScore)){
                    Visited.insert(NewScore);
                    NextPlacementScore.push_back(NewScore);
                }
            }
            if (EmptyPlace.second >= 1)
            {
                int64_t NewScore = CopiedScore + ScoreToPlacement.at(EmptyPlace.first).at(EmptyPlace.second - 1) *
                                                     (int64_t(pow(9, 3 * EmptyPlace.first + EmptyPlace.second)) - int64_t(pow(9, 3 * (EmptyPlace.first) + EmptyPlace.second - 1)));
                if (!Visited.contains(NewScore)){
                    Visited.insert(NewScore);
                    NextPlacementScore.push_back(NewScore);
                }
            }
            if (EmptyPlace.second <= 1)
            {
                int64_t NewScore = CopiedScore + ScoreToPlacement.at(EmptyPlace.first).at(EmptyPlace.second + 1) *
                                                     (int64_t(pow(9, 3 * EmptyPlace.first + EmptyPlace.second)) - int64_t(pow(9, 3 * (EmptyPlace.first) + EmptyPlace.second + 1)));
                if (!Visited.contains(NewScore)){
                    Visited.insert(NewScore);
                    NextPlacementScore.push_back(NewScore);
                }
            }

        }
        PlayCount ++;
        if (NextPlacementScore.size() > 0){
            AllNowPlacementScore.push_back(NextPlacementScore);
        }
        if (Visited.contains(42374116)){
            break;
        }
    }
    cout << PlayCount << "\n";
}
accuracy: 0.8214285714285714
time_ms: 20
memory_kb: 9308
final_rank: 15

code_snippet: #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <queue>
#include <unordered_map>

using namespace std;

const int dx[4] = {-1, 1, 0, 0}; // 上下左右
const int dy[4] = {0, 0, -1, 1};
const char* goal = "123456780";

// パズル状態を文字列として管理する
struct State {
    char board[10]; // 9桁+終端
    int step;
};

int bfs(const char* start) {
    unordered_map<string, int> visited;
    queue<State> q;

    State s;
    strcpy(s.board, start);
    s.step = 0;
    q.push(s);
    visited[s.board] = 1;

    while (!q.empty()) {
        State cur = q.front(); q.pop();

        if (strcmp(cur.board, goal) == 0) {
            return cur.step;
        }

        int pos = strchr(cur.board, '0') - cur.board;
        int x = pos / 3, y = pos % 3;

        for (int i = 0; i < 4; i++) {
            int nx = x + dx[i], ny = y + dy[i];
            if (nx < 0 || nx >= 3 || ny < 0 || ny >= 3) continue;

            int npos = nx * 3 + ny;
            State next = cur;
            next.board[pos] = cur.board[npos];
            next.board[npos] = '0';
            next.step = cur.step + 1;

            if (!visited[next.board]) {
                visited[next.board] = 1;
                q.push(next);
            }
        }
    }

    return -1; // 解けない場合（ただし問題文では常に解ける）
}

int main() {
    char input[10];
    int idx = 0;
    for (int i = 0; i < 9; i++) {
        int x;
        scanf("%d", &x);
        input[idx++] = x + '0';
    }
    input[9] = '\0';

    int ans = bfs(input);
    printf("%d\n", ans);
    return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 15784
final_rank: 5

code_snippet: #include <stdio.h>

// 状態を表す構造体
typedef struct {
    int board[9];   // 3x3の盤面を1次元配列で表現
    int blank_pos;  // 空白(0)の位置
    int depth;      // 初期状態からの手数
} State;

// キューの実装
State queue[362881];
int head = 0, tail = 0;

void enqueue(State s) {
    queue[tail++] = s;
}

State dequeue() {
    return queue[head++];
}

// 階乗を事前計算
int fact[] = {1, 1, 2, 6, 24, 120, 720, 5040, 40320};

// 盤面の順列をユニークなIDに変換する関数
int get_id(int board[]) {
    int id = 0;
    for (int i = 0; i < 9; i++) {
        int count = 0;
        for (int j = i + 1; j < 9; j++) {
            if (board[i] > board[j]) {
                count++;
            }
        }
        id += count * fact[8 - i];
    }
    return id;
}

int main() {
    State initial_state;
    initial_state.depth = 0;

    for (int i = 0; i < 9; i++) {
        scanf("%d", &initial_state.board[i]);
        if (initial_state.board[i] == 0) {
            initial_state.blank_pos = i;
        }
    }

    // 訪問済み状態を管理する配列 (0で初期化される)
    char visited[362881] = {0};

    // ゴール状態
    int goal[] = {1, 2, 3, 4, 5, 6, 7, 8, 0};
    int goal_id = get_id(goal);
    
    head = tail = 0;
    enqueue(initial_state);
    visited[get_id(initial_state.board)] = 1;

    // 移動方向 (上、下、左、右)
    int dr[] = {-3, 3, -1, 1};

    while (head < tail) {
        State current = dequeue();

        if (get_id(current.board) == goal_id) {
            printf("%d\n", current.depth);
            return 0;
        }

        // 4方向への移動を試す
        for (int i = 0; i < 4; i++) {
            int next_blank_pos = current.blank_pos + dr[i];
            
            // 盤面の左右の端をまたぐ無効な移動をチェック
            if ((i == 2 && current.blank_pos % 3 == 0) || (i == 3 && current.blank_pos % 3 == 2)) {
                continue;
            }
            
            // 盤面外への移動でないかチェック
            if (next_blank_pos >= 0 && next_blank_pos < 9) {
                State next = current;
                // パネルを交換
                next.board[current.blank_pos] = next.board[next_blank_pos];
                next.board[next_blank_pos] = 0;
                
                int next_id = get_id(next.board);
                if (!visited[next_id]) {
                    visited[next_id] = 1;
                    next.blank_pos = next_blank_pos;
                    next.depth = current.depth + 1;
                    enqueue(next);
                }
            }
        }
    }

    return 0;
}
accuracy: 1
time_ms: 80
memory_kb: 11460
final_rank: 4

code_snippet: #include <iostream>
#include <cmath>
#include <string>
#include <map>
#include <queue>
using namespace std;

#define N 3
#define N2 9

// パズルの状態を表す構造体
struct Puzzle {
    int f[N2];      // パネルの状態（9マス）
    int space;      // 空白の位置
    string path;    // ゴールまでの移動履歴

    // 状態の比較演算子（mapのキー用）
    bool operator < (const Puzzle &p) const {
        for (int i = 0; i < N2; i++) {
            if (f[i] == p.f[i]) continue;
            return f[i] > p.f[i];
        }
        return false;
    }
};

// 移動方向（上下左右）とその表現
static const int dx[4] = {-1, 0, 1, 0};
static const int dy[4] = {0, -1, 0, 1};
static const char dir[4] = {'u', 'l', 'd', 'r'};

// ゴール判定
bool isTarget(Puzzle p) {
    for (int i = 0; i < N2; i++) {
        if (p.f[i] != (i + 1)) return false;
    }
    return true;
}

// 幅優先探索で最短手数を求める
string bfs(Puzzle s) {
    queue<Puzzle> Q;
    map<Puzzle, bool> V;
    Puzzle u, v;

    s.path = "";
    Q.push(s);
    V[s] = true;

    while (!Q.empty()) {
        u = Q.front(); Q.pop();
        if (isTarget(u)) return u.path;

        int sx = u.space / N;
        int sy = u.space % N;

        for (int r = 0; r < 4; r++) {
            int tx = sx + dx[r];
            int ty = sy + dy[r];

            if (tx < 0 || ty < 0 || tx >= N || ty >= N) continue;

            v = u;
            swap(v.f[u.space], v.f[tx * N + ty]);
            v.space = tx * N + ty;
            v.path += dir[r];

            if (!V[v]) {
                V[v] = true;
                Q.push(v);
            }
        }
    }

    return "unsolvable";
}

int main() {
    Puzzle in;

    for (int i = 0; i < N2; i++) {
        cin >> in.f[i];
        if (in.f[i] == 0) {
            in.f[i] = N2;      // 空白を 9 として扱う
            in.space = i;
        }
    }

    string ans = bfs(in);
    cout << ans.size() << endl;
    return 0;
}
accuracy: 1
time_ms: 190
memory_kb: 33564
final_rank: 10

code_snippet: #include<bits/stdc++.h>
using namespace std;
// #define int long long
#define endl '\n'
typedef pair<int,int> PII;
int go[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
bool in(int x,int y){
    return x>=0&&x<3&&y>=0&&y<3;
}
int a[3][3];
void build(int x){
    for(int i=2;i>=0;i--){
        for(int j=2;j>=0;j--){
            a[i][j]=x%10;
            x/=10;
        }
    }
}
int get(){
    int res=0;
    for(int i=0;i<3;i++){
        for(int j=0;j<3;j++){
            res=res*10+a[i][j];
        }
    }
    return res;
}
unordered_set<int>st;
void solve(){
    for(int i=0;i<3;i++){
        for(int j=0;j<3;j++){
            cin>>a[i][j];
        }
    }    
    queue<PII>q;
    int x=get(),tag=123456780;
    if(x==tag){
        cout<<0<<endl;
        return;
    }
    q.push({x,0});
    st.insert(x);
    while(!q.empty()){
        PII p=q.front();q.pop();
        int x=p.first,d=p.second;
        build(x);
        for(int i=0;i<3;i++){
            for(int j=0;j<3;j++){
                if(a[i][j]==0){
                    for(int k=0;k<4;k++){
                        int x1=i+go[k][0],y1=j+go[k][1];
                        if(!in(x1,y1)) continue;
                        swap(a[x1][y1],a[i][j]);
                        int y=get();
                        if(y==tag){
                            cout<<d+1<<endl;
                            return;
                        }
                        if(!st.count(y)){
                            st.insert(y);
                            q.push({y,d+1});
                        }
                        swap(a[x1][y1],a[i][j]);
                    }
                }
            }
        }
    }
}
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    int T=1;
    // cin>>T;
    for(int t=1;t<=T;t++){
        solve();
    }
    return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 10592
final_rank: 1

code_snippet: #include <iostream>
#include <cmath>
#include <string>
#include <map>
#include <queue>
using namespace std;

#define N 3
#define N2 9

// パズルの状態を表す構造体
struct Puzzle {
    int f[N2];      // パネルの状態（9マス）
    int space;      // 空白の位置
    string path;    // ゴールまでの移動履歴

    // 状態の比較演算子（mapのキー用）
    bool operator < (const Puzzle &p) const {
        for (int i = 0; i < N2; i++) {
            if (f[i] == p.f[i]) continue;
            return f[i] > p.f[i];
        }
        return false;
    }
};

// 移動方向（上下左右）とその表現
static const int dx[4] = {-1, 0, 1, 0};
static const int dy[4] = {0, -1, 0, 1};
static const char dir[4] = {'u', 'l', 'd', 'r'};

// ゴール判定
bool isTarget(Puzzle p) {
    for (int i = 0; i < N2; i++) {
        if (p.f[i] != (i + 1)) return false;
    }
    return true;
}

// 幅優先探索で最短手数を求める
string bfs(Puzzle s) {
    queue<Puzzle> Q;
    map<Puzzle, bool> V;
    Puzzle u, v;

    s.path = "";
    Q.push(s);
    V[s] = true;

    while (!Q.empty()) {
        u = Q.front(); Q.pop();
        if (isTarget(u)) return u.path;

        int sx = u.space / N;
        int sy = u.space % N;

        for (int r = 0; r < 4; r++) {
            int tx = sx + dx[r];
            int ty = sy + dy[r];

            if (tx < 0 || ty < 0 || tx >= N || ty >= N) continue;

            v = u;
            swap(v.f[u.space], v.f[tx * N + ty]);
            v.space = tx * N + ty;
            v.path += dir[r];

            if (!V[v]) {
                V[v] = true;
                Q.push(v);
            }
        }
    }

    return "unsolvable";
}

int main() {
    Puzzle in;

    for (int i = 0; i < N2; i++) {
        cin >> in.f[i];
        if (in.f[i] == 0) {
            in.f[i] = N2;      // 空白を 9 として扱う
            in.space = i;
        }
    }

    string ans = bfs(in);
    cout << ans.size() << endl;
    return 0;
}
accuracy: 1
time_ms: 190
memory_kb: 33576
final_rank: 11

code_snippet: #include <iostream>
#include <cmath>
#include <string>
#include <map>
#include <queue>

constexpr int N = 3;
constexpr int N2 = 9;

struct Puzzle {
    int f[N2];
    int space;
    std::string path;

    bool operator<(const Puzzle& p) const {
        for (int i = 0; i < N2; ++i) {
            if (f[i] == p.f[i]) {
                continue;
            }
            return f[i] > p.f[i];
        }
        return false;
    }
};

static const int dx[4] = {-1, 0, 1, 0};
static const int dy[4] = {0, -1, 0, 1};
static const char dir[4] = {'u', 'l', 'd', 'r'};

bool is_target(Puzzle p) {
    for (int i = 0; i < N2; ++i) {
        if (p.f[i] != (i + 1)) {
            return false;
        }
    }
    return true;
}

std::string bfs(Puzzle s) {
    std::queue<Puzzle> Q;
    std::map<Puzzle, bool> V;
    Puzzle u, v;
    s.path = "";
    Q.push(s);
    V[s] = true;

    while (!Q.empty()) {
        u = Q.front();
        Q.pop();
        if (is_target(u)) {
            return u.path;
        }
        int sx = u.space / N;
        int sy = u.space % N;
        for (int r = 0; r < 4; ++r) {
            int tx = sx + dx[r];
            int ty = sy + dy[r];
            if (tx < 0 || ty < 0 || tx >= N || ty >= N) {
                continue;
            }
            v = u;
            std::swap(v.f[u.space], v.f[tx * N + ty]);
            v.space = tx * N + ty;
            if (!V[v]) {
                V[v] = true;
                v.path += dir[r];
                Q.push(v);
            }
        }
    }
    return "unsolvable";
}

int main() {
    Puzzle in;

    for (int i = 0; i < N2; ++i) {
        std::cin >> in.f[i];
        if (in.f[i] == 0) {
            in.f[i] = N2; // set space
            in.space = i;
        }
    }
    std::string ans = bfs(in);
    std::cout << ans.size() << std::endl;

    return 0;
}
accuracy: 1
time_ms: 170
memory_kb: 32768
final_rank: 7

code_snippet: #include <bits/stdc++.h>

using namespace std;

int encode(const vector<vector<int>> &b) {
    int res = 0;
    for (auto &row: b)
        for (auto cell: row)
            res = res * 10 + cell;
    return res;
}

vector<vector<int>> decode(int code) {
    vector<vector<int>> res(3, vector<int>(3));
    for (int i = 2; i >= 0; --i) {
        for (int j = 2; j >= 0; --j) {
            res[i][j] = code % 10;
            code /= 10;
        }
    }
    return res;
}

pair<int, int> findZero(const vector<vector<int>> &b) {
    for (int i = 0; i < 3; ++i)
        for (int j = 0; j < 3; ++j)
            if (b[i][j] == 0)
                return {i, j};
    return {-1, -1}; // Should never happen
}

vector<int> getNeig(int code) {
    auto b = decode(code);
    vector<int> res;
    auto [i, j] = findZero(b);
    for (int di = 0, dj = -1, k = 0; k < 4; ++k) {
        int ni = i + di;
        int nj = j + dj;
        if (ni >= 0 && ni < 3 && nj >= 0 && nj < 3) {
            swap(b[i][j], b[ni][nj]);
            res.push_back(encode(b));
            swap(b[i][j], b[ni][nj]);
        }
        swap(di, dj);
        if (k == 1)dj = 1;
    }
    return res;
}

/*
1 3 0
4 2 5
7 8 6

130425786

1 3 5
4 2 0
7 8 6
135420786

1 0 3
4 2 5
7 8 6
103425786
 */
int main() {
    vector<vector<int>> goal = {{1, 2, 3},
                                {4, 5, 6},
                                {7, 8, 0}}, start(3, vector<int>(3));
    for (int i = 0; i < 3; ++i)
        for (int j = 0; j < 3; ++j)
            cin >> start[i][j];
    int startCode = encode(start);
    int goalCode = encode(goal);
    if (startCode == goalCode) {
        cout << 0 << endl;
        return 0;
    }
    queue<int> q;
    q.push(startCode);
    unordered_set<int> vis = {startCode};
    int steps = 0;
    while(q.size()){
        int sz=q.size();
        ++steps;
        while (sz--){
            int u=q.front();
            q.pop();
            for(auto v: getNeig(u))
                if(vis.insert(v).second)
                    if (v == goalCode) {
                        cout << steps << endl;
                        return 0;
                    } else
                        q.push(v);
        }
    }
}
accuracy: 1
time_ms: 50
memory_kb: 10864
final_rank: 2

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (i = 0; i < (int)(n); i++)
using ll = long long;

static const int N = 3;
static const vector<int> dx = {-1, 0, 1, 0};
static const vector<int> dy = {0, -1, 0, 1};
static const vector<char> dir = {'l', 'd', 'r', 'u'};

struct Puzzle
{
    vector<vector<int>> f = vector<vector<int>>(N, vector<int>(N));
    int sx;
    int sy;
    string path;

    bool operator<(const Puzzle &p) const
    {
        int i;
        int j;
        rep(i, N)
        {
            rep(j, N)
            {
                if (f.at(i).at(j) == p.f.at(i).at(j))
                {
                    continue;
                }
                return f.at(i).at(j) > p.f.at(i).at(j);
            }
        }
        return false;
    }
};

bool isTgt(Puzzle s, Puzzle t)
{
    int i;
    int j;

    rep(i, N)
    {
        rep(j, N)
        {
            if (s.f.at(i).at(j) != t.f.at(i).at(j))
            {
                return false;
            }
        }
    }
    return true;
}

string search(Puzzle s, Puzzle t)
{
    queue<Puzzle> Q;
    set<Puzzle> V;
    Puzzle u;
    Puzzle v;
    int i;
    int tx;
    int ty;

    Q.push(s);
    V.insert(s);

    while (!Q.empty())
    {
        u = Q.front();
        Q.pop();
        if (isTgt(u, t))
        {
            return u.path;
        }
        rep(i, 4)
        {
            tx = u.sx + dx.at(i);
            ty = u.sy + dy.at(i);
            if (tx < 0 || tx >= N || ty < 0 || ty >= N)
            {
                continue;
            }
            v = u;
            swap(v.f.at(u.sy).at(u.sx), v.f.at(ty).at(tx));
            v.sx = tx;
            v.sy = ty;
            if (V.count(v) == 0)
            {
                V.insert(v);
                v.path += dir.at(i);
                Q.push(v);
            }
        }
    }
    return "";
}

int main()
{
    int i;
    int j;
    Puzzle s;
    Puzzle t;
    string ans;

    rep(i, N)
    {
        rep(j, N)
        {
            cin >> s.f.at(i).at(j);
            if (s.f.at(i).at(j) == 0)
            {
                s.sx = j;
                s.sy = i;
            }
            t.f.at(i).at(j) = (i * 3 + j + 1) % 9;
        }
    }

    s.path = "";
    ans = search(s, t);
    cout << ans.size() << endl;
    return 0;
}
accuracy: 1
time_ms: 430
memory_kb: 61568
final_rank: 14

code_snippet: #include <bits/stdc++.h>
using namespace std;

const string target = "123456780";
int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, -1, 1};

int bfs(string start) {
	queue<pair<string, int>> q;
	unordered_set<string> visited;
	
	q.push({start, 0});
	visited.insert(start);
	
	while (!q.empty()) {
		auto [current, steps] = q.front(); q.pop();
		
		if (current == target) return steps;
		
		int zeroPos = current.find('0');
		int row = zeroPos / 3;
		int col = zeroPos % 3;
		
		for (int i = 0; i < 4; ++i) {
			int newRow = row + dx[i];
			int newCol = col + dy[i];
			
			if (newRow >= 0 && newRow < 3 && newCol >= 0 && newCol < 3) {
				int newPos = newRow * 3 + newCol;
				string nextState = current;
				swap(nextState[zeroPos], nextState[newPos]);
				
				if (visited.find(nextState) == visited.end()) {
					q.push({nextState, steps + 1});
					visited.insert(nextState);
				}
			}
		}
	}
	return -1;
}

int main() {
	string start;
	for (int i = 0; i < 9; ++i) {
		char c; cin >> c;
		start += c;
	}
	
	cout << bfs(start) << endl;
	
	return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 16316
final_rank: 6