ACM (UVa) : 10102



#include <iostream.h>
#include <stdlib.h>

int m;
char a [1010] [1010];

void reset ()
{
	for ( int i = 0; i < 1010; i++ ) {
		for ( int j = 0; j < 1010; j++ )
			a [i] [j] = '0';
	}
}

int call ( int x, int y )
{
	int min = 9999;

	for ( int i = 0; i < m; i++ ) {
		for ( int j = 0; j < m; j++ ) {
			if ( a [i] [j] == '3' ) {
				int temp = abs ( x - i ) + abs ( y - j );
				if ( temp < min )
					min = temp;
			}
		}
	}

	return min;
}


int main ()
{
	int i;
	while ( cin >> m ) {

		reset ();

		for ( i = 0; i < m; i++ ) {
			for ( int j = 0; j < m; j++ ) 
				cin >> a [i] [j];
		}

		int max = 0;

		for ( i = 0; i < m; i++ ) {
			for ( int j = 0; j < m; j++ ) {
				if ( a [i] [j] == '1' ) {
					int min = call (i, j);
					if ( min > max )
						max = min;
				}
			}
		}

		cout << max << endl;

	}
	return 0;
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s