【发布时间】:2014-09-18 05:38:41
【问题描述】:
(一)。我有一个包含命令“矩形(x1,x2,y1,y2)”的软件,它可以在xy上生成一个左下角坐标(x1,y1)和右上角坐标(x2,y2)的矩形飞机
(二)。我想用这个软件在 x-y 平面上创建所有带有堆叠矩形的字母数字,基本上我想通过 perl 打印出这些命令
(三)。我的想法是通过用矩形填充此表来创建一个 5x7 表,然后将其放置到我想要的坐标如下:
#!/usr/bin/perl
use strict;
use warnings;
###=================================================================================###
###=================================================================================###
###========= This script will generate alphanumeric by filling polygon in =========###
###==================== the specific position of a 5 x 7 table =====================###
###================= following case represents the alphanumeric:"1"================###
###=================================================================================###
###=== --------------------- ===###
###=== | | | O | | | 6 ===###
###=== --------------------- ===###
###=== | | O | O | | | 5 ===###
###=== --------------------- ===###
###=== | | | O | | | 4 ===###
###=== --------------------- ===###
###=== | | | O | | | 3 row number ===###
###=== --------------------- ===###
###=== | | | O | | | 2 ===###
###=== --------------------- ===###
###=== | | | O | | | 1 ===###
###=== --------------------- ===###
###=== | | O | O | O | | 0 ===###
###=== ----------*---------- "*" stand for (coor_x, coor_y) ===###
###=== -2 -1 0 1 2 --> column number ===###
my grid_x = 1; # the size of a grid along x-direction
my grid_y = 1; # the size of a grid along y-direction
sub alphanum_1 {
my $coor_x = shift; # the x coordinate that I wanna put this "polygon 1"
my $coor_y = shift; # the y coordinate that I wanna put this "polygon 1"
my ($i,$j,$mkrstring);
my @col_neg2 = (""); # the positions which needs to be filled in col -2
my @col_neg1 = (0,5); # the positions which needs to be filled in col -1
my @col_zero = (0,1,2,3,4,5,6); # the positions which needs to be filled in col 0
my @col_pos1 = (0); # the positions which needs to be filled in col +1
my @col_pos2 = (""); # the positions which needs to be filled in col +2
my (@marker,@anchor);
for ($i=0; $i<=$#col_zero-1; $i++) {
$marker[$i] = ($col_zero[$i+1] - $col_zero[$i] == 1)? "m" : "0";
$mkrstring = $mkrstring.$marker[$i];
}
}
&alphanum_1;
问题来了,你可以看到列 0 需要被所有 7 行填充,如果我只想使用“矩形”命令一次而不是 7 次:
假设 coor_x=0, coor_y=0
方法一:
rectangle(-0.5,0.5,0,7)
方法二:
rectangle(-0.5,0.5,0,1)
rectangle(-0.5,0.5,1,2)
rectangle(-0.5,0.5,2,3)
rectangle(-0.5,0.5,3,4)
rectangle(-0.5,0.5,4,5)
rectangle(-0.5,0.5,5,6)
rectangle(-0.5,0.5,6,7)
方法1和2会达到同样的效果,但我更喜欢使用方法1,这意味着我需要检查同一列中的“邻里关系”,但我卡在这部分,基本上,我想坚持矩形,只要它们可以在同一列中粘在一起即可。
我是 perl 的新手,有什么提示吗?我需要你的帮助!
【问题讨论】:
-
这看起来更像是一个算法问题,而不是一些 perl 技巧。在我看来,这就像一个 NP 完成任务,以最小化矩形函数的调用。几个问题:1)两个矩形可以重叠吗? (即您可以先调用
rectangle(-0.5,0.5,0,7),然后再调用rectangle(-1.5,1.5,0,1)) 2)不多次调用矩形函数的原因到底是什么?您想将其称为 最少 次,还是对较小的减少量感到满意? -
(1) 是的,所有矩形都可以重叠,只要能完成正确的形状即可
-
(2)。我需要使用“矩形”函数百万次,所以我想做一些减少,最小值很棒,但没有必要
标签: perl