1067 Gap
bfs,注意输入的数据是short可以转化为char再转化为string来存储
打表不可行,因为情况实在是太多了
#pragma warning(disable:4996)
#include<cstdio>
#include<map>
#include<cmath>
#include<vector>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cstdlib>
#include<stack>
#include<queue>
#include<set>
using namespace std;
char temp[35];
const char mp[35] = {11,12,13,14,15,16,17,1,
21,22,23,24,25,26,27,1,
31,32,33,34,35,36,37,1,
41,42,43,44,45,46,47,1};
void showmap()
{
int k;
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 8; j++)
{
printf("%hhd ", temp[8 * i + j]);
}
printf("\n");
}
scanf("%d", &k);
printf("\n");
}
int bfs()
{
if (!strcmp(temp, mp))return 0;
pair<string, int> p, tp;
queue< pair<string, int>> q; q.push({ temp,0 });
set<string> s; s.insert(temp);
while (!q.empty())
{
tp = q.front(); q.pop();
strcpy(temp, tp.first.c_str());
//showmap();
for (int i = 0; i < 32; i++)
{
if (temp[i] == 1 && temp[i - 1] != 1 && temp[i - 1] % 10 != 7)
{
char ttemp[35]; strcpy(ttemp, temp);
for (int j = 0; j < 32; j++) if (ttemp[j] == ttemp[i - 1] + 1) ttemp[j] = 1;
ttemp[i] = ttemp[i - 1] + 1;
if (!strcmp(ttemp, mp))return tp.second + 1;
if (s.find(ttemp) == s.end())
{
q.push({ ttemp,tp.second + 1 });
s.insert(ttemp);
}
}
}
}
return -1;
}
int main()
{
int n; scanf("%d", &n);
while (n--)
{
for (int i = 0; i < 4; i++)
{
temp[8 * i] = 10 * i + 11;
for (int j = 0; j < 7; j++)
{
scanf("%hhd", temp + 8 * i + j + 1);
if (temp[8 * i + j + 1] % 10 == 1)temp[8 * i + j + 1] = 1;
}
}
printf("%d\n", bfs());
}
}