Advertisement
Guest User

Untitled

a guest
Jan 4th, 2018
598
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.46 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define f(i,n) for(int i=0;i<(n);i++)
  5. #define ff(i,n) for(int i=1;i<=(n);i++)
  6. #define m0(X) memset((X), 0, sizeof((X)))
  7. #define m1(X) memset((X), -1, sizeof((X)))
  8. #define pb(x) push_back(x)
  9. #define ay 100005
  10. #define rt return 0
  11. #define sr(x,n) sort(x+1,x+n+1)
  12. #define sv(v) sort(v.begin(),v.end())
  13. #define fs first
  14. #define sc second
  15. #define cs(a) cout<<"Case "<<css<<": "<<a<<"\n";
  16. #define pf(a) printf("%c",a)
  17. #define sf(a) scanf("%d",&a)
  18.  
  19. typedef long long ll;
  20. typedef unsigned long long ull;
  21. const ll mod=1000*1000*1000+7;
  22. const double pi = acos(-1.0);
  23. const double eps= 1e-9;
  24. const ll inf = 1e18;
  25.  
  26. int toint(string s){int sm;stringstream ss(s);ss>>sm;return sm;}
  27. ll tolint(string s){long long sm;stringstream ss(s);ss>>sm;return sm;}
  28. int cnt=0;
  29. struct{
  30. int i;int j;
  31. }goal[20];
  32.  
  33. int puz[5][5],path[55],mx;
  34.  
  35. int inversion(){
  36. int sum=0;
  37. int r,c;
  38. for(int i=1;i<=4;i++)
  39. for(int j=1;j<=4;j++){
  40. if(puz[i][j]==0){
  41. r=i;
  42. continue;
  43. }
  44. ff(p,4)ff(q,4){
  45. if(puz[p][q]==0)continue;
  46. if(p>i||(p==i&&q>j)){
  47. if(puz[i][j]>puz[p][q])sum++;
  48. }
  49. }
  50. }
  51. if(!((r%2==0&&sum%2==0)||(r%2!=0&&sum%2!=0))){
  52. return 0;
  53. }
  54. return 1;
  55. }
  56.  
  57. int calculate(){
  58. //cnt++;
  59. int sum=0,a,m=0,ex=0;
  60. ff(i,4)ff(j,4){
  61.  
  62. a=(abs(goal[puz[i][j]].i-i)+abs(goal[puz[i][j]].j-j));
  63. m=max(a,m);
  64. sum+=a;
  65. }
  66.  
  67. sum+=ex/2;
  68. //cout<<ex<<" "<<sum<<"\n";
  69. return max(sum-m,1);
  70. }
  71.  
  72. int hu(){
  73.  
  74. int p=1;
  75. ff(i,4)ff(j,4){
  76. if(i==4&&j==4){
  77. return 0;
  78. }
  79. if(puz[i][j]!=p)return calculate();
  80. p++;
  81. }
  82. return 0;
  83. }
  84.  
  85. int minval=mod;
  86. bool f=1;
  87. int dfs(int p,int prev,int r,int c){
  88. int h=hu();
  89. int heuristic=p+h-1;
  90.  
  91. //cout<<hu()<<" ";
  92. minval=min(minval,heuristic);
  93. if(heuristic>mx){
  94. return mod;
  95. }
  96. if(h==0){
  97. f=0;return p-1;
  98. }
  99. int mn=mod;
  100.  
  101. if(prev!=1&&r+1<=4&&f){
  102. swap(puz[r][c],puz[r+1][c]);
  103. path[p]=-1;
  104. mn=min(mn,dfs(p+1,-1,r+1,c));
  105. swap(puz[r][c],puz[r+1][c]);
  106. }
  107.  
  108. if(prev!=-2&&c-1>0&&f){
  109. swap(puz[r][c],puz[r][c-1]);
  110. path[p]=2;
  111. mn=min(mn,dfs(p+1,2,r,c-1));
  112. swap(puz[r][c],puz[r][c-1]);
  113. }
  114. if(prev!=2&&c+1<=4&&f){
  115. swap(puz[r][c],puz[r][c+1]);
  116. path[p]=-2;
  117. mn=min(mn,dfs(p+1,-2,r,c+1));
  118. swap(puz[r][c],puz[r][c+1]);
  119. }
  120. if(prev!=-1&&r-1>0&&f){
  121. swap(puz[r][c],puz[r-1][c]);
  122. path[p]=1;
  123. mn=min(mn,dfs(p+1,1,r-1,c));
  124. swap(puz[r][c],puz[r-1][c]);
  125. }
  126. return mn;
  127. }
  128.  
  129.  
  130.  
  131. int e;
  132.  
  133. string s;
  134. int main()
  135. {
  136. int p=1;
  137. ff(i,4)ff(j,4){
  138. goal[p].i=i;
  139. goal[p++].j=j;
  140. }
  141. goal[0].i=goal[0].j=4;
  142.  
  143. int n,r,c;
  144. sf(n);
  145. ff(z,n){
  146.  
  147. ff(i,4)ff(j,4){
  148. sf(puz[i][j]);
  149. if(puz[i][j]==0){
  150. r=i,c=j;
  151. }
  152. }
  153. mx=1;
  154. int ans=mod;
  155. int x=inversion();
  156. if(x==0){
  157. printf("Case %d: This puzzle is not solvable.\n",z);
  158. continue;
  159. }
  160. while(mx<=35){
  161. e=dfs(1,0,r,c);
  162. if(e<mod){
  163. ans=e;break;
  164. }
  165. mx++;
  166. mx=max(mx,minval);
  167. }
  168.  
  169. if(f==1)printf("Case %d: This puzzle is not solvable.\n",z);
  170.  
  171. else {
  172. printf("Case %d: ",z);
  173. for(int i=1;i<=ans;i++){
  174. if(path[i]==1)printf("%c",'U');
  175. else if(path[i]==-1)printf("%c",'D');
  176. else if(path[i]==2)printf("%c",'L');
  177. else if(path[i]==-2)printf("%c",'R');
  178. }
  179. printf("\n");
  180.  
  181. }
  182.  
  183. f=1;
  184. minval=mod;
  185. }
  186. //cout<<cnt;
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement