Advertisement
Guest User

Untitled

a guest
Sep 19th, 2017
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.12 KB | None | 0 0
  1. #include <vector>
  2. #include <queue>
  3. #include <utility>
  4. #include <tuple>
  5. #include <algorithm>
  6. using namespace std;
  7.  
  8. using queue_entry = tuple<int, long long, int, int>;
  9. struct comp_func_queue
  10. {
  11. public:
  12.     bool operator()(queue_entry &lhs, queue_entry &rhs) const
  13.     {
  14.         // 아무리 적게 이동해도 ((m-1) - y) + ((n-1) - x)만큼을 더 가야 하므로 지금까지의 이동거리에 더해준다
  15.         // m-1과 n-1은 모든 식에 동일하게 등장하므로 제거해서 비교하여도 상관이 없다.
  16.         const int dist_l = get<0>(lhs) - get<2>(lhs) - get<3>(lhs);
  17.         const int dist_r = get<0>(rhs) - get<2>(rhs) - get<3>(rhs);
  18.  
  19.         if (dist_l == dist_r)
  20.         {
  21.             return get<1>(lhs) > get<1>(rhs);
  22.         }
  23.         return dist_l > dist_r;
  24.     }
  25. };
  26.  
  27.  
  28. int cut[50][50];
  29. // 전역 변수를 정의할 경우 함수 내에 초기화 코드를 꼭 작성해주세요.
  30. vector<int> solution(int m, int n, int s, vector<vector<int>> time_map) {
  31.     int move_distance = 0;
  32.     int sum_of_talk_time = 0;
  33.  
  34.     int dy[4] = { 0,1,0,-1 };
  35.     int dx[4] = { 1,0,-1,0 };
  36.  
  37.     for(int i=0;i<m;i++)
  38.     {
  39.         for(int j=0;j<n;j++)
  40.         {
  41.             cut[i][j] = 0x7fffffff;
  42.         }
  43.     }
  44.  
  45.     priority_queue<queue_entry, vector<queue_entry>, comp_func_queue> pq;
  46.     pq.emplace(0, 0, 0, 0);
  47.     vector<int> answer(2);
  48.     while (true)
  49.     {
  50.         auto top = pq.top();
  51.         pq.pop();
  52.  
  53.         int y = get<2>(top);
  54.         int x = get<3>(top);
  55.        
  56.         if (cut[y][x] <= get<1>(top)) continue;
  57.         cut[y][x] = get<1>(top);       
  58.  
  59.         if (y == m - 1 && x == n - 1)
  60.         {
  61.             answer[0] = get<0>(top);
  62.             answer[1] = get<1>(top);
  63.             break;
  64.         }
  65.  
  66.         for (int dir = 0; dir<4; dir++)
  67.         {
  68.             const int ny = y + dy[dir];
  69.             const int nx = x + dx[dir];
  70.             if (!(ny >= 0 && ny < m && nx >= 0 && nx < n)) continue;
  71.             if (time_map[ny][nx] == -1) continue;
  72.  
  73.             queue_entry entry(get<0>(top) + 1, get<1>(top) + time_map[ny][nx], ny, nx);
  74.             if (get<1>(entry) > s) continue; // 이동함으로써 t의 합이 s를 초과하게 되는 경우
  75.             if (get<1>(entry) >= cut[ny][nx]) continue; // 더 적은 이동으로 더 적은 대화시간에 도달하는게 가능한 경우 고려X
  76.  
  77.             pq.emplace(entry);
  78.         }
  79.     }
  80.  
  81.     return answer;
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement