Here you can find the source of computeDistanceMap(int[][] image)
public static int[][] computeDistanceMap(int[][] image)
//package com.java2s; /***************************************************************************** ** ANGRYBIRDS AI AGENT FRAMEWORK//from www. j a va 2 s . c om ** Copyright (c) 2013,XiaoYu (Gary) Ge, Stephen Gould,Jochen Renz ** Sahan Abeyasinghe, Jim Keys, Kar-Wai Lim, Zain Mubashir, Andrew Wang, Peng Zhang ** All rights reserved. **This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License. **To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/3.0/ *or send a letter to Creative Commons, 444 Castro Street, Suite 900, Mountain View, California, 94041, USA. *****************************************************************************/ public class Main { public static int[][] computeDistanceMap(int[][] image) { // first pass: top-left to bottom-right for (int y = 0; y < image.length; y++) { for (int x = 0; x < image[y].length; x++) { if (image[y][x] != 0) { image[y][x] = 0; } else { image[y][x] = image.length + image[y].length; if (y > 0) image[y][x] = Math.min(image[y][x], image[y - 1][x] + 1); if (x > 0) image[y][x] = Math.min(image[y][x], image[y][x - 1] + 1); } } } // second pass: bottom-right to top-left for (int y = image.length - 1; y >= 0; y--) { for (int x = image[y].length - 1; x >= 0; x--) { if (y < image.length - 1) image[y][x] = Math .min(image[y][x], image[y + 1][x] + 1); if (x < image[y].length - 1) image[y][x] = Math .min(image[y][x], image[y][x + 1] + 1); } } return image; } }