-
Notifications
You must be signed in to change notification settings - Fork 0
/
10181.cpp
61 lines (60 loc) · 1.08 KB
/
10181.cpp
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
#include<cstdio>
#include<cstring>
#include<set>
using namespace std;
#define swap(a,b,c,d) {int temp=e[a][b];e[a][b]=e[c][d],e[c][d]=temp;}
int a[5]={0,1,0,-1};
int b[5]={1,0,-1,0};
char s[5]={"URDL"};
char out[20];
unsigned long long f[20];
bool found;
set<unsigned long long> data;
unsigned long long cal(int z[][5])
{
unsigned long long sum=0;
for (int i=1;i<=4;i++)
for (int j=1;j<=4;j++)
sum+=f[(i-1)*4+j]*z[i][j];
return sum;
}
void dfs(int z[][5],int x,int y,int k)
{
int e[5][5];
if (found) return ;
if (k==15) return ;
for (int i=0;i<4;i++)
if (x+a[i]>=1 && x+a[i]<=n && y+b[i]>=1 && y+b[i]<=n)
{
if (found) return ;
memcpy(e,z,sizeof(e));
out[k]=s[i];
swap(x,y,x+a[i],y+b[i]);
unsigned long long hash=cal(e);
if (data.find(hash)==)
dfs(temp,x+a[i],y+b[i],k+1);
}
}
main()
{
int T;
int z[5][5];
scanf("%d",&T);
f[0]=1;
for (int i=1;i<16;i++)
f[i]=f[1]*16;
while (T--)
{
int x,y;
found=0;
for (int i=1;i<=4;i++)
for (int j=1;j<=4;j++)
{
scanf("%d",&z[i][j]);
if (z[i][j]==0)
x=i,y=j;
}
dfs(int *z,x,y,0);
}
return 0;
}