Skip to content

Instantly share code, notes, and snippets.

@ZeronSix
Created November 27, 2015 17:15
Show Gist options
  • Save ZeronSix/ef1bb5666d63e5fa176f to your computer and use it in GitHub Desktop.
Save ZeronSix/ef1bb5666d63e5fa176f to your computer and use it in GitHub Desktop.
621. Ближайшее число
#include <map>
#include <iostream>
#include <vector>
#include <stdlib.h>
struct Point {
int x;
int y;
};
int main() {
int n;
std::cin >> n;
int** matrix = new int*[n];
int** result_matrix = new int*[n];
std::vector<Point> empty;
std::vector<Point> non_empty;
for (int i = 0; i < n; i++) {
matrix[i] = new int[n];
result_matrix[i] = new int[n];
for (int j = 0; j < n; j++) {
Point coord = {i, j};
int m;
std::cin >> m;
matrix[i][j] = m;
result_matrix[i][j] = m;
if (m == 0)
empty.push_back(coord);
else
non_empty.push_back(coord);
}
}
int* a = new int[2 * n];
for (auto v1 : empty) {
for (int i = 0; i < 2 * n; i++)
a[i] = 0;
std::map<int, int> verts;
for (auto v2 : non_empty) {
int l = abs(v2.x - v1.x) + abs(v2.y - v1.y);
a[l] += 1;
verts[l] = matrix[v2.x][v2.y];
}
for (int i = 0; i < 2 * n; i++) {
if (a[i] == 1)
result_matrix[v1.x][v1.y] = verts[i];
if (a[i] >= 1)
break;
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++)
std::cout << result_matrix[i][j] << " ";
std::cout << std::endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment