spoj16935 Straight Line Spiral Pattern (Act 3)
来源:互联网 发布:软件开发技术特点 编辑:程序博客网 时间:2024/06/05 03:19
题目:http://www.spoj.com/problems/SPTTRN3/
题意:输出螺旋的图案,图案规则是先顺时针画一圈星号,但是首尾不能相连,然后在第二层中隔两点,接着是逆时针画圈
具体代码如下:
<?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);$clockdx = array(0, 1, 0, -1);$clockdy = array(1, 0, -1, 0);$arr[] = array($clockdx, $clockdy);$anticlockdx = array(1, 0, -1, 0);$anticlockdy = array(0, 1, 0, -1);$arr[] = array($anticlockdx, $anticlockdy);$cur = 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 + $arr[$cur][0][$curdir]; $newy = $cury + $arr[$cur][1][$curdir];//echo 'curx:', $curx, ' cury:', $cury, ' newx:', $newx, ' newy:', $newy, PHP_EOL;if ($newx == $x && $newy == $y){$matrix[$curx][$cury] = '.';$curx -= $arr[$cur][0][$curdir];$cury -= $arr[$cur][1][$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 + $arr[$cur][0][$curdir]; $cury = $cury + $arr[$cur][1][$curdir];$matrix[$curx][$cury] = '*';$x = $curx + $arr[$cur][0][$curdir]; $y = $cury + $arr[$cur][1][$curdir];$cur = ($cur + 1) % 2;}}//echo 'remain:', $remain, PHP_EOL;if (0 !== $remain){$curdir = ($curdir + 1) % 4;$curx = $curx + $arr[$cur][0][$curdir]; $cury = $cury + $arr[$cur][1][$curdir];$matrix[$curx][$cury] = '*';$x = $curx + $arr[$cur][0][$curdir]; $y = $cury + $arr[$cur][1][$curdir];$cur = ($cur + 1) % 2;$curx = $x; $cury = $y;while ($remain--){while (true){$matrix[$curx][$cury] = '*';$newx = $curx + $arr[$cur][0][$curdir]; $newy = $cury + $arr[$cur][1][$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;}?>
0 0
- spoj16935 Straight Line Spiral Pattern (Act 3)
- spoj 16892 Straight Line Spiral Pattern (Act 1)
- ArcGIS的直线距离Straight line工具
- [线性常微分方程][2] STRAIGHT-LINE SOLUTIONS
- Code Complete 读书笔记(Chapter 14 Organizing Straight-line Code)
- ACT
- spiral search pattern for block matching
- 代码大全学习-17-组织顺序排列的代码(Organizing Straight-Line Code)
- 在xml中定义直线路径Custom straight line path in xml
- [LeetCode]Maximum number of points on a straight line in 2d plane
- spiral
- spoj CPTTRN3 - Character Patterns (Act 3)
- Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the other.
- Pattern 3
- Design Pattern(3)-State Pattern
- Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.
- act.exe
- Balancing Act
- 最短路:带限制的最短路,邻接表建图,dijkstra ZOJ 3946+HDU 2680 (简单点的)
- Java编程中“为了性能”需做的26件事
- KDDCUP历年主题
- 隐马尔科夫模型
- Vmware虚拟机网络设置
- spoj16935 Straight Line Spiral Pattern (Act 3)
- 函数名 和 自己定义的普通函数指针 辨析
- LeetCode——048
- php通过curl post和get发送json数据实例 curl命令转为php源码
- Ubuntu 14.04 配置 Nginx + uWSGI 托管 virtualenv 下 Flask 应用的一点注意事项
- 阮一峰:美丽心灵的永恒阳光
- php设计模式之装饰模式
- 21-Add Two Numbers-Leetcode
- javascript中apply()和call()方法的区别