Guest User

Untitled

a guest
Oct 30th, 2016
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.89 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <assert.h>
  4. #include <limits.h>
  5. #include <iostream>
  6. using namespace std;
  7. #include <queue>
  8. #include <math.h>
  9.  
  10. typedef struct t_cell {
  11.     int x;
  12.     int y;
  13.     double distance;
  14.  
  15.     bool operator>(const t_cell &rhs) const {
  16.         return (distance > rhs.distance);
  17.     }
  18. } cell;
  19.  
  20. typedef priority_queue<cell, vector<cell>, greater<cell>> workqueue;
  21.  
  22. char **G;
  23. int H, W;
  24. int **distances;
  25.  
  26. double heur(int x1, int y1, int x2, int y2) {
  27.     return (
  28.         sqrt((x1 - x2)*(x1 - x2) + (y1 - y2) * (y1 - y2))
  29.     );
  30. }
  31.  
  32. bool is_free(int x, int y) {
  33.     if (x < 0 || y < 0) return false;
  34.     if (x >= W || y >= H) return false;
  35.     if (G[y][x] == '#') return false;
  36.  
  37.     //cout << "is_free (" << x << ", " << y << "): true" << endl;
  38.     return true;
  39. }
  40.  
  41. void check_n_add(workqueue &q, int x, int y) {
  42.     if (is_free(x, y)) {
  43.         double dist = heur(W-1, H-1, x, y);
  44.         if (distances[y][x] > dist) {
  45.             //cout << "Updating distance" << endl;
  46.             distances[y][x] = dist;
  47.         }
  48.         else {
  49.             return;
  50.         }
  51.         //cout << "Added: " << x << ", " << y << ", " << dist << endl;
  52.         q.push({x, y, dist});
  53.     }
  54. }
  55.  
  56.  
  57.  
  58. int jump(int H, int W, char** G) {
  59.     ::H = H;
  60.     ::W = W;
  61.     ::G = G;
  62.  
  63.     distances = new int*[H];
  64.     for (int i = 0; i < H; i++) distances[i] = new int[W];
  65.  
  66.     for (int i = 0; i < H; i++)
  67.     for (int j = 0; j < W; j++) {
  68.         distances[i][j] = INT_MAX;
  69.     }
  70.  
  71.     int px, py;
  72.     px = py = 0;
  73.  
  74.     int steps = 0;
  75.     workqueue q;
  76.     q.push({0, 0, INT_MAX});
  77.  
  78.     while (q.size() > 0) {
  79.         cell nearest = q.top(); q.pop();
  80.         px = nearest.x;
  81.         py = nearest.y;
  82.         //cout << "moving to: " << px << ", " << py << ", " << nearest.distance << endl;
  83.  
  84.         if (px == W-1 && py == H-1) {
  85.             //cout << "Found" << endl;
  86.             break;
  87.         }
  88.  
  89.         steps++;
  90.         check_n_add(q, px+1, py);
  91.         check_n_add(q, px+2, py);
  92.  
  93.         check_n_add(q, px, py+1);
  94.         check_n_add(q, px, py+2);
  95.  
  96.         check_n_add(q, px-1, py);
  97.         check_n_add(q, px-2, py);
  98.  
  99.         check_n_add(q, px, py-1);
  100.         check_n_add(q, px, py-2);
  101.  
  102.         check_n_add(q, px+1, py+1);
  103.         check_n_add(q, px-1, py-1);
  104.         check_n_add(q, px-1, py+1);
  105.         check_n_add(q, px+1, py-1);
  106.     }
  107.  
  108.     //cout << "Steps: " << steps << endl;
  109.     return steps;
  110. }
  111.  
  112.  
  113. int main() {
  114.     FILE *fr, *fw;
  115.     int H, W, i;
  116.     char** G;
  117.  
  118.     fr = fopen("input.txt", "r");
  119.     fw = fopen("output.txt", "w");
  120.     assert(2 == fscanf(fr, "%d%d", &H, &W));
  121.     G = (char**) malloc(H * sizeof(char*));
  122.     for(i=0; i<H; i++) {
  123.         G[i] = (char*) malloc(W * sizeof(char*));
  124.         assert(1 == fscanf(fr, "%s", G[i]));
  125.     }
  126.  
  127.     fprintf(fw, "%d\n", jump(H, W, G));
  128.     fclose(fr);
  129.     fclose(fw);
  130.     return 0;
  131. }
Advertisement
Add Comment
Please, Sign In to add comment