60分求助
查看原帖
60分求助
516500
hong_liekai楼主2022/7/28 10:31
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <bits/stdc++.h>
#include <string>
// #pragma GCC optimize(2)
// #pragma GCC optimize(3)
// #pragma GCC optimize("Ofast")
#define pb push_back
#define all(a) a.begin(), a.end()
#define SIS std::ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#define X first
#define Y second
#define ls u << 1
#define rs u << 1 | 1
#define Yes cout << "Yes" << endl
#define No cout << "No" << endl
#define YES cout << "YES" << endl
#define NO cout << "NO" << endl
#define inf 0x3f3f3f3f3f3f3f3f
#define endl "\n"
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
typedef pair<ll, ll> Pll;
const ll INF = 1e18;
const int N = 2e6 + 10;
const int mod = 1e9 + 7;
const double pi = acos(-1.0);
inline int read(void)
{
    int num = 0;
    char ch = getchar();
    while (ch < 48 || ch > 57)
        ch = getchar();
    while (ch > 47 && ch < 58)
        num = (num << 3) + (num << 1) + (ch ^ 48), ch = getchar();
    return num;
}

ll n, m;
int d;
ll f[N], dp[N];

char s[5][5];
char t[5][5];
int ed;
int st[N];
int step[N];

string clac(char s[5][5])
{
    string str = "";
    for (int i = 1; i <= 4; i++)
        for (int j = 1; j <= 4; j++)
            str += s[i][j];

    return str;
}

string xx, yy;
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
map<string, int> mp;

struct node
{
    string s;
    int dd;
};

void reclac(string str)
{
    int cnt = 0;
    for (int i = 1; i <= 4; i++)
        for (int j = 1; j <= 4; j++)
        {
            s[i][j] = str[cnt];
            cnt++;
        }
}
struct node2{
    int a,b,c,d;
};
map<string,node2>ans;
map<string,string>fa;
int flag ;
void bfs()
{
    queue<node> q;
    xx = clac(s);
    yy = clac(t);
    q.push({xx, 0});
    mp[xx] = 1;
    while (q.size())
    {
        auto ee = q.front();
        q.pop();
        string x = ee.s;
        int step = ee.dd;
       // cout << x << endl;
        if (x == yy)
        {
            cout << step << endl;
            return;
        }
        for (int i = 1; i <= 4; i++)
        {
            for (int j = 1; j <= 4; j++)
            {
                
                if (s[i][j] == '1')
                {
                    for (int k = 0; k < 4; k++)
                    {
                        reclac(x);
                        int a = i + dx[k];
                        int b = j + dy[k];
                        if (a > 4 || b > 4 || a < 1 || b < 1)
                            continue;
                        if (s[a][b] == '1')
                            continue;
                        swap(s[i][j], s[a][b]);
        
                        string res = clac(s);
                        
                        if (mp[res])
                            continue;
                        ans[res] = {i,j,a,b};
                        fa[res] = x;    
                        mp[res] = 1;
                        q.push({res, step + 1});
                    }
                }
            }
        }
    }
}
char ss[5][5];
int vis[5][5];
int main()
{
    SIS;
    int _;
    _ = 1;
    //  cin >> _;
    int k = 0;
    while (_--)
    {
        for (int i = 1; i <= 4; i++)
            for (int j = 1; j <= 4; j++)
                cin >> s[i][j] , ss[i][j] = s[i][j];

        for (int i = 1; i <= 4; i++)
            for (int j = 1; j <= 4; j++)
                cin >> t[i][j];
        string st = clac(s);
        bfs();
        string ed = clac(t);
          while(ed != st){
             //cout << ed << " " << fa[ed] << endl;
             cout << ans[ed].a << "" << ans[ed].b << "" << ans[ed].c << "" << ans[ed].d << endl;
             ed = fa[ed];
           
        }

    }
    return 0;
}
2022/7/28 10:31
加载中...