SuitNdtie

Mr Robot

May 27th, 2019
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.04 KB | None | 0 0
  1. #include<stdio.h>
  2.  
  3. int Xs,Ys;
  4.  
  5. struct pos{
  6.     int x,y;
  7. };
  8.  
  9. int const N = 12;
  10. int n;
  11. int dist[N][N];
  12.  
  13. int abs(int x){
  14.     return (x < 0 ? x*-1 : x);
  15. }
  16.  
  17. int cd(pos a,pos b){
  18.     return abs(a.x - b.x) + abs(a.y - b.y);
  19. }
  20.  
  21. int min(int a,int b){
  22.     return (a < b ? a : b);
  23. }
  24.  
  25. int dp[N][(1<<N)];
  26.  
  27. int cal(int now , int bit){
  28.     if(bit == (1<<(n+1))-1){
  29.         return dist[now][0];
  30.     }
  31.     if(dp[now][bit] != -1)return dp[now][bit];
  32.     int ans = 1e9;
  33.     for(int nxt = 0 ; nxt <= n ; nxt ++){
  34.         if(!(bit&(1<<nxt)) && nxt != now){
  35.             ans = min(ans , cal(nxt , (bit|(1<<nxt))) + dist[now][nxt]);
  36.         }
  37.     }
  38.     return dp[now][bit] = ans;
  39. }
  40.  
  41. int main()
  42. {
  43.     for(int i = 0 ; i < N ; i ++)for(int j = 0 ; j < (1<<N) ; j ++)dp[i][j] = -1;
  44.     scanf("%d %d",&Xs,&Ys);
  45.     pos all[15];
  46.     scanf("%d %d",&all[0].x ,&all[0].y);
  47.    
  48.     scanf("%d",&n);
  49.     for(int i = 1 ; i <= n ; i++){
  50.         scanf("%d %d",&all[i].x , &all[i].y);
  51.     }
  52.     for(int i = 0 ; i <= n ; i ++){
  53.         for(int j = 0 ; j <= n ; j ++){
  54.             dist[i][j] = cd(all[i],all[j]);
  55.         }
  56.     }
  57.     printf("%d",cal(0,1));
  58.     return 0;
  59. }
Add Comment
Please, Sign In to add comment