Advertisement
Guest User

Untitled

a guest
Aug 19th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.32 KB | None | 0 0
  1. #include "StdAfx.h"
  2. #include <iostream>
  3. #include <cmath>
  4. #include <string>
  5. #include <cstdlib>
  6.  
  7. int grid[183][183];
  8. int height;
  9. int width;
  10.  
  11. int distance(int i1, int j1, int i2, int j2)
  12. {
  13.     int result = std::fabs(float(i1 - i2)) + std::fabs(float(j1 - j2));
  14.  
  15.     return result;
  16. }
  17.  
  18. int nearest(int tx, int ty)
  19. {
  20.     int closest = 183;
  21.    
  22.     for (int y = 0; y != height; ++y)
  23.     {
  24.         for (int x = 0; x != width; ++x)
  25.         {
  26.             if (grid[x][y] == 1){
  27.                 int d = distance(tx, ty, x, y);
  28.                 if (d < closest){
  29.                     closest = d;
  30.                 }
  31.             }
  32.         }
  33.     }
  34.    
  35.     return closest;
  36. }
  37.  
  38. int bitmap()
  39. {
  40.     std::cin >> height;
  41.     std::cin >> width;
  42.    
  43.     int solution[183][183];
  44.    
  45.     // get input
  46.     for(int y = 0; y != height; ++y)
  47.     {
  48.         std::string s;
  49.         std::cin >> s;
  50.  
  51.         for(int x = 0; x != width; ++x)
  52.         {
  53.             std::string c = s.substr(x, 1);
  54.             grid[x][y] = std::atoi(c.c_str());
  55.         }
  56.     }
  57.    
  58.     // get solutions
  59.     for(int y = 0; y != height; ++y)
  60.     {
  61.         for(int x = 0; x != width; ++x)
  62.         {
  63.             solution[x][y] = nearest(x, y);
  64.         }
  65.     }
  66.    
  67.     // print solution
  68.     for(int y = 0; y != height; ++y)
  69.     {
  70.         for(int x = 0; x != width; ++x)
  71.         {
  72.             std::cout << solution[x][y] << ' ';
  73.         }
  74.         std::cout << std::endl;
  75.     }
  76.  
  77.     return 0;
  78. }
  79.  
  80. int main()
  81. {
  82.     int tests;
  83.     std::cin >> tests;
  84.     for(int i = 0; i != tests; ++i)
  85.     {
  86.         bitmap();
  87.     }
  88.     return 0;
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement