[ create a new paste ] login | about

Link: http://codepad.org/hg051cu0    [ raw code | output | fork ]

pauld - PHP, pasted on Jul 16:
<?php

// inputs
$w=3;
$h=3;

// start 
$last = -1;
$steps = $w * $h;

// steps counters
$wstep = $w;
$hstep = ($h - 1);

// function to output details
function c($limit, $add) {
	global $last, $steps;
	for( 
		$i=0; 
		$i < $limit;
		$last=$last+$add, ++$i, --$steps, print $last.' '
	);
}

// main loop
while ($wstep > 1) {
	// accross
	$j = 1;
	$limit = $wstep;
	c($limit, $j);
	--$wstep;
	
	if ($hstep > 0) {
		// up
		$j = $w;
		$limit = $hstep;
		c($limit, $j);
		--$hstep;

		
		if ($wstep > 0) {
			// back accross 
			$j = -1;
			$limit = $wstep;
			c($limit, $j);
			--$wstep;

			
			if ($hstep > 0) {	
				// down
				$j = -$w;
				$limit = $hstep;
				c($limit, $j);
				--$hstep;
			}
		}
	}
}

?>


Output:
1
0 1 2 5 8 7 6 3 


Create a new paste based on this one


Comments: