Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.io.*;
- import java.math.*;
- import java.awt.geom.*;
- import static java.lang.Math.*;
- public class Solution implements Runnable {
- public void solve() throws Exception {
- int n = sc.nextInt(), m = sc.nextInt();
- char[][] field = new char[n][];
- for (int i = 0; i < n; i++) {
- field[i] = in.readLine().toCharArray();
- }
- int[][] ans = new int[n][m];
- for (int i = 0; i < n; i++) {
- Arrays.fill(ans[i], Integer.MAX_VALUE / 4);
- }
- int[] last = new int[m];
- Arrays.fill(last, -1);
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < m; j++) {
- if (field[i][j] == '1') {
- last[j] = i;
- }
- }
- for (int j = 0; j < m; j++) {
- for (int k = 0; k < m; k++) {
- if (last[k] == -1) {
- continue;
- }
- ans[i][j] = min(ans[i][j], (j - k) * (j - k) + (i - last[k]) * (i - last[k]));
- }
- }
- }
- Arrays.fill(last, -1);
- for (int i = n - 1; i >= 0; i--) {
- for (int j = 0; j < m; j++) {
- if (field[i][j] == '1') {
- last[j] = i;
- }
- }
- for (int j = 0; j < m; j++) {
- for (int k = 0; k < m; k++) {
- if (last[k] == -1) {
- continue;
- }
- ans[i][j] = min(ans[i][j], (j - k) * (j - k) + (i - last[k]) * (i - last[k]));
- }
- }
- }
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < m; j++) {
- out.print(ans[i][j] + " ");
- }
- out.println();
- }
- }
- static Throwable uncaught;
- BufferedReader in;
- FastScanner sc;
- PrintWriter out;
- @Override
- public void run() {
- try {
- in = new BufferedReader(new InputStreamReader(System.in));
- out = new PrintWriter(System.out);
- sc = new FastScanner(in);
- solve();
- } catch (Throwable uncaught) {
- Solution.uncaught = uncaught;
- } finally {
- out.close();
- }
- }
- public static void main(String[] args) throws Throwable {
- Thread thread = new Thread(null, new Solution(), "", (1 << 26));
- thread.start();
- thread.join();
- if (Solution.uncaught != null) {
- throw Solution.uncaught;
- }
- }
- }
- class FastScanner {
- BufferedReader in;
- StringTokenizer st;
- public FastScanner(BufferedReader in) {
- this.in = in;
- }
- public String nextToken() throws Exception {
- while (st == null || !st.hasMoreTokens()) {
- st = new StringTokenizer(in.readLine());
- }
- return st.nextToken();
- }
- public int nextInt() throws Exception {
- return Integer.parseInt(nextToken());
- }
- public long nextLong() throws Exception {
- return Long.parseLong(nextToken());
- }
- public double nextDouble() throws Exception {
- return Double.parseDouble(nextToken());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement