Print a straight line spiral pattern as shown in the example below. Please use the * (asterisk) and . (dot) characters.
Input
You are given t - the number of test casesand for each of the test cases one integer: s, 3<s<100 - the size of the shape.
Output
For each of the test cases output the requested pattern.Use one line break in between successive patterns.
Example
Input: 5 4 5 11 13 78 Output: **** ...* *..* **** ***** ....* ***.* *...* ***** *********** ..........* *********.* *.......*.* *.*****.*.* *.*...*.*.* *.*.***.*.* *.*.....*.* *.*******.* *.........* *********** ************* ............* ***********.* *.........*.* *.*******.*.* *.*.....*.*.* *.*.***.*.*.* *.*.*...*.*.* *.*.*****.*.* *.*.......*.* *.*********.* *...........* ************* ****************************************************************************** .............................................................................* ****************************************************************************.* *..........................................................................*.* *.************************************************************************.*.* *.*......................................................................*.*.* *.*.********************************************************************.*.*.* *.*.*..................................................................*.*.*.* *.*.*.****************************************************************.*.*.*.* *.*.*.*..............................................................*.*.*.*.* *.*.*.*.************************************************************.*.*.*.*.* *.*.*.*.*..........................................................*.*.*.*.*.* *.*.*.*.*.********************************************************.*.*.*.*.*.* *.*.*.*.*.*......................................................*.*.*.*.*.*.* *.*.*.*.*.*.****************************************************.*.*.*.*.*.*.* *.*.*.*.*.*.*..................................................*.*.*.*.*.*.*.* *.*.*.*.*.*.*.************************************************.*.*.*.*.*.*.*.* *.*.*.*.*.*.*.*..............................................*.*.*.*.*.*.*.*.* *.*.*.*.*.*.*.*.********************************************.*.*.*.*.*.*.*.*.* *.*.*.*.*.*.*.*.*..........................................*.*.*.*.*.*.*.*.*.* *.*.*.*.*.*.*.*.*.****************************************.*.*.*.*.*.*.*.*.*.* *.*.*.*.*.*.*.*.*.*......................................*.*.*.*.*.*.*.*.*.*.* *.*.*.*.*.*.*.*.*.*.************************************.*.*.*.*.*.*.*.*.*.*.* *.*.*.*.*.*.*.*.*.*.*..................................*.*.*.*.*.*.*.*.*.*.*.* *.*.*.*.*.*.*.*.*.*.*.********************************.*.*.*.*.*.*.*.*.*.*.*.* *.*.*.*.*.*.*.*.*.*.*.*..............................*.*.*.*.*.*.*.*.*.*.*.*.* *.*.*.*.*.*.*.*.*.*.*.*.****************************.*.*.*.*.*.*.*.*.*.*.*.*.* *.*.*.*.*.*.*.*.*.*.*.*.*..........................*.*.*.*.*.*.*.*.*.*.*.*.*.* *.*.*.*.*.*.*.*.*.*.*.*.*.************************.*.*.*.*.*.*.*.*.*.*.*.*.*.* *.*.*.*.*.*.*.*.*.*.*.*.*.*......................*.*.*.*.*.*.*.*.*.*.*.*.*.*.* *.*.*.*.*.*.*.*.*.*.*.*.*.*.********************.*.*.*.*.*.*.*.*.*.*.*.*.*.*.* *.*.*.*.*.*.*.*.*.*.*.*.*.*.*..................*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.* *.*.*.*.*.*.*.*.*.*.*.*.*.*.*.****************.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.* *.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*..............*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.* *.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.************.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.* *.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*..........*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.* *.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.********.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.* *.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*......*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.* *.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.****.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.* *.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*..*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.* *.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*....*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.* *.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.******.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.* *.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*........*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.* *.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.**********.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.* *.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*............*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.* *.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.**************.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.* *.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*................*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.* *.*.*.*.*.*.*.*.*.*.*.*.*.*.*.******************.*.*.*.*.*.*.*.*.*.*.*.*.*.*.* *.*.*.*.*.*.*.*.*.*.*.*.*.*.*....................*.*.*.*.*.*.*.*.*.*.*.*.*.*.* *.*.*.*.*.*.*.*.*.*.*.*.*.*.**********************.*.*.*.*.*.*.*.*.*.*.*.*.*.* *.*.*.*.*.*.*.*.*.*.*.*.*.*........................*.*.*.*.*.*.*.*.*.*.*.*.*.* *.*.*.*.*.*.*.*.*.*.*.*.*.**************************.*.*.*.*.*.*.*.*.*.*.*.*.* *.*.*.*.*.*.*.*.*.*.*.*.*............................*.*.*.*.*.*.*.*.*.*.*.*.* *.*.*.*.*.*.*.*.*.*.*.*.******************************.*.*.*.*.*.*.*.*.*.*.*.* *.*.*.*.*.*.*.*.*.*.*.*................................*.*.*.*.*.*.*.*.*.*.*.* *.*.*.*.*.*.*.*.*.*.*.**********************************.*.*.*.*.*.*.*.*.*.*.* *.*.*.*.*.*.*.*.*.*.*....................................*.*.*.*.*.*.*.*.*.*.* *.*.*.*.*.*.*.*.*.*.**************************************.*.*.*.*.*.*.*.*.*.* *.*.*.*.*.*.*.*.*.*........................................*.*.*.*.*.*.*.*.*.* *.*.*.*.*.*.*.*.*.******************************************.*.*.*.*.*.*.*.*.* *.*.*.*.*.*.*.*.*............................................*.*.*.*.*.*.*.*.* *.*.*.*.*.*.*.*.**********************************************.*.*.*.*.*.*.*.* *.*.*.*.*.*.*.*................................................*.*.*.*.*.*.*.* *.*.*.*.*.*.*.**************************************************.*.*.*.*.*.*.* *.*.*.*.*.*.*....................................................*.*.*.*.*.*.* *.*.*.*.*.*.******************************************************.*.*.*.*.*.* *.*.*.*.*.*........................................................*.*.*.*.*.* *.*.*.*.*.**********************************************************.*.*.*.*.* *.*.*.*.*............................................................*.*.*.*.* *.*.*.*.**************************************************************.*.*.*.* *.*.*.*................................................................*.*.*.* *.*.*.******************************************************************.*.*.* *.*.*....................................................................*.*.* *.*.**********************************************************************.*.* *.*........................................................................*.* *.**************************************************************************.* *............................................................................* ******************************************************************************
题意:输出螺旋状的图案
思路:完成一圈,范围减小4,并且每圈的最后一段与首结点相隔一个点符号。所以根据所给的数,能够知道有几个完整的圈,后面就填充剩下的余数段就可以了
代码如下
<?php
$debug = false;
$file = STDIN;
if ($debug)
{
$file = fopen("./spoj.txt", "r");
}
$t = trim(fgets($file));
while ($t--)
{
$line = trim(fgets($file));
$n = intval($line);
solve($n);
if (0 != $t) echo PHP_EOL;
}
if ($debug) fclose($file);
function solve($n)
{
$matrix = array();
$tmp = array();
$tmp = array_pad($tmp, $n, '.');
$matrix = array_pad($matrix, $n, $tmp);
$dx = array(0, 1, 0, -1);
$dy = array(1, 0, -1, 0);
$curdir = 0;
$x = 0; $y = 0;
$loop = (int)($n / 4);
$remain = $n % 4;
for ($i = 0; $i < $loop; $i++)
{
$curx = $x; $cury = $y;
while (true)
{
$matrix[$curx][$cury] = '*';
$newx = $curx + $dx[$curdir]; $newy = $cury + $dy[$curdir];
//echo 'curx:', $curx, ' cury:', $cury, ' newx:', $newx, ' newy:', $newy, PHP_EOL;
if ($newx == $x && $newy == $y)
{
$matrix[$curx][$cury] = '.';
$curx -= $dx[$curdir];
$cury -= $dy[$curdir];
break;
}
if ($newx < 0 + $i * 2 || $newx >= $n - $i * 2 || $newy < 0 + $i * 2 || $newy >= $n - $i * 2)
{
$curdir = ($curdir + 1) % 4;
continue;
}
$curx = $newx; $cury = $newy;
}
if ($loop - 1 != $i)
{
$curdir = ($curdir + 1) % 4;
$curx = $curx + $dx[$curdir]; $cury = $cury + $dy[$curdir];
$matrix[$curx][$cury] = '*';
$x = $curx + $dx[$curdir]; $y = $cury + $dy[$curdir];
}
}
//echo 'remain:', $remain, PHP_EOL;
if (0 !== $remain)
{
$curdir = ($curdir + 1) % 4;
$curx = $curx + $dx[$curdir]; $cury = $cury + $dy[$curdir];
$matrix[$curx][$cury] = '*';
$x = $curx + $dx[$curdir]; $y = $cury + $dy[$curdir];
$curx = $x; $cury = $y;
while ($remain--)
{
while (true)
{
$matrix[$curx][$cury] = '*';
$newx = $curx + $dx[$curdir]; $newy = $cury + $dy[$curdir];
//echo 'curx:', $curx, ' cury:', $cury, ' newx:', $newx, ' newy:', $newy, PHP_EOL;
if ($newx < 0 + $i * 2 || $newx >= $n - $i * 2 || $newy < 0 + $i * 2 || $newy >= $n - $i * 2)
{
$curdir = ($curdir + 1) % 4;
break;
}
$curx = $newx; $cury = $newy;
}
}
}
//var_dump($matrix);
output($matrix);
}
function output($matrix)
{
$ans = '';
$size = count($matrix);
for ($i = 0; $i < $size; $i++)
{
for ($j = 0; $j < $size; $j++)
{
$ans .= $matrix[$i][$j];
}
$ans .= PHP_EOL;
}
echo $ans;
}
?>