Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "StdAfx.h"
- #include <iostream>
- #include <cmath>
- #include <string>
- #include <cstdlib>
- int grid[183][183];
- int height;
- int width;
- int distance(int i1, int j1, int i2, int j2)
- {
- int result = std::fabs(float(i1 - i2)) + std::fabs(float(j1 - j2));
- return result;
- }
- int nearest(int tx, int ty)
- {
- int closest = 183;
- for (int y = 0; y != height; ++y)
- {
- for (int x = 0; x != width; ++x)
- {
- if (grid[x][y] == 1){
- int d = distance(tx, ty, x, y);
- if (d < closest){
- closest = d;
- }
- }
- }
- }
- return closest;
- }
- int bitmap()
- {
- std::cin >> height;
- std::cin >> width;
- int solution[183][183];
- // get input
- for(int y = 0; y != height; ++y)
- {
- std::string s;
- std::cin >> s;
- for(int x = 0; x != width; ++x)
- {
- std::string c = s.substr(x, 1);
- grid[x][y] = std::atoi(c.c_str());
- }
- }
- // get solutions
- for(int y = 0; y != height; ++y)
- {
- for(int x = 0; x != width; ++x)
- {
- solution[x][y] = nearest(x, y);
- }
- }
- // print solution
- for(int y = 0; y != height; ++y)
- {
- for(int x = 0; x != width; ++x)
- {
- std::cout << solution[x][y] << ' ';
- }
- std::cout << std::endl;
- }
- return 0;
- }
- int main()
- {
- int tests;
- std::cin >> tests;
- for(int i = 0; i != tests; ++i)
- {
- bitmap();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement