spoj 16892 Straight Line Spiral Pattern (Act 1)

本文介绍了一种使用 PHP 实现的算法,该算法能够生成指定大小的直角螺旋图案。图案由星号(*)和点号(.)组成,适用于各种尺寸的正方形网格。通过调整输入参数,可以创建不同规模的螺旋形状。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
	}
?>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kgduu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值