Php 从一维数组访问行和列
我记得很久以前在一个算法课上就可以做这样的事情,但我想我可能只是在想象而已 假设我们有下面的3x3表Php 从一维数组访问行和列,php,c,arrays,optimization,Php,C,Arrays,Optimization,我记得很久以前在一个算法课上就可以做这样的事情,但我想我可能只是在想象而已 假设我们有下面的3x3表 1 2 3 4 5 6 7 8 9 可以存储为数组(0=>1,1=>2,…,8=>9) 我可以使用一些数学方法查找第1行第1列的值(0索引,因此值为5)。假设我们知道桌子的宽度 0 1 2 3 4 5 6 7 8 数组(宽度*行值查找+列值查找) 因为width=3,我们需要第1行和第1列,这就是数组(3*1+1=4),这是正确的。我想是吧 我试图对此进行扩展,以查找
1 2 3
4 5 6
7 8 9
可以存储为数组(0=>1,1=>2,…,8=>9)
我可以使用一些数学方法查找第1行第1列的值(0索引,因此值为5)。假设我们知道桌子的宽度
0 1 2
3 4 5
6 7 8
数组(宽度*行值查找+列值查找)
因为width=3,我们需要第1行和第1列,这就是数组(3*1+1=4)
,这是正确的。我想是吧
我试图对此进行扩展,以查找也属于子组的表
+----------+----------+
| 0 1 2 | 3 4 5 |
| 6 7 8 | 9 A B |
+----------+----------+
| C D E | F 10 11 |
| 12 13 14 | 15 16 17 |
+----------+----------+
所以我试着想出一个方程,让我看看0区(1,2,3,6,7,8)或1区(3,4,5,9,A,B),2区(C,D,E,12,13,14)或3区(F,10,11,15,16,17)中的所有值
我们知道总宽度是6,我们知道每个区块的宽度是3,每个区块的高度是2
arr = array(0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,10,11,12,13,14,15,16,17);
arr_length = 24; // total length
tw = 6 // total width
bw = 3 // block width
bh = 2 // block height
bpr = tw / bw // blocks per row = 2
bro = tw * bh // block index row offset = 12
我需要能够查看块中的所有值、列中的所有值以及行中的所有值。所以我想我需要3个循环
// check row, left to right
for (i=row_to_check; i<(row_to_check + tw); i++) {
if(check(arr, i) == FALSE)
break; // exit loop if we met a false condition
}
// check col, top to bottom
for (i=col_to_check; i<arr_length;) {
if(check(arr, i) == FALSE)
break; // exit loop if we met a false condition
i += tw; // col + total width
}
// this is where i'm confused (two loops???)
// need to find starting index, then we can just use the tw offset to scoot down in the block
// first row of blocks is just block_number * block_width, but we don't know where blocks fall down to the next row
// so we need to find out what row the block is on
// total_width / block_width = number of blocks per row
// total_width * block_height = index offset per row of blocks (block 2 starts at index C)
// block 0 and block 1 are block_row 0, block 2 and block 3 are block_row 1
// if we use integer division for the blocks...
// 0 \ num_blocks_per_row = 0
// 1 \ num_blocks_per_row = 0
// 2 \ num_blocks_per_row = 1
// 3 \ num_blocks_per_row = 1
// offset from start of block_row = 3 % num_blocks_per_row = 1
// Sooo... block 3's index = (block_number / (total_width / block_width))*(total_width*block_height) + (block_number % (total_width / block_width) ) * block_width = (3/(6/3))*12 + (3%(6/3))*3 = 3/2 + (3%2)*3 = 1*12 + 1*3 = 15
// DAMN that's messy
// using new variables that I set up upstairs
// starting_index = (block_number / bpr) * bro + (block_number % bpr) * bw
// check block (only have bw*bh values to check?)
for(i=0; i<(bw*bh); i++) {
for( ) {
}
}
//检查行,从左到右
对于(i=行到检查;i以下函数将返回一维数组中所需元素的索引:
function getIndices($rows, $columns, $subMatrixRows, $subMatrixColumns, $subMatrixIndex) {
$indices = array();
// LiCol coordinates of submatrix
$subRow = ceil($subMatrixIndex * $subMatrixColumns / $columns);
$subColumn = ($subMatrixIndex -1 ) % ($columns / $subMatrixColumns) + 1;
// LiCol coordinates in global matrix of first sub matrix element
$baseRow = ($subRow -1 ) * $subMatrixRows + 1;
$baseColumn = ($subColumn -1) * $subMatrixColumns + 1;
// Index in zero based one dimention array of first sub matrix element
$baseIndex = ($baseRow - 1) * $columns + $baseColumn - 1;
// Indices in zero based one dimention array
for ($i = 0; $i < $subMatrixRows; $i++) {
for ($j = 0; $j < $subMatrixColumns; $j++) {
$indices[] = $baseIndex + $i * $columns + $j;
}
}
return $indices;
}
函数getIndex($rows、$columns、$subtrixRows、$subtrixColumns、$subtrixIndex){
$index=array();
//子矩阵的LiCol坐标
$subtraxW=ceil($subtraxIndex*$subtraxColumns/$columns);
$subColumn=($subtrixindex-1)%($columns/$subtrixcolumns)+1;
//第一个子矩阵元素全局矩阵中的LiCol坐标
$baseRow=($subRow-1)*$subtrixrows+1;
$baseColumn=($subColumn-1)*$subtrixcolumns+1;
//第一个子矩阵元素的零基一维数组中的索引
$baseIndex=($baseRow-1)*$columns+$baseColumn-1;
//基于零的一维数组中的索引
对于($i=0;$i<$subtrixRows;$i++){
对于($j=0;$j<$subtrixcolumns;$j++){
$index[]=$baseIndex+$i*$columns+$j;
}
}
回报指数;;
}
测试结果如下:
for ($i = 0; $i < 4; $i++) {
print_r(getIndices(4, 6, 2, 3, $i+1));
}
Array
(
[0] => 0
[1] => 1
[2] => 2
[3] => 6
[4] => 7
[5] => 8
)
Array
(
[0] => 3
[1] => 4
[2] => 5
[3] => 9
[4] => 10
[5] => 11
)
Array
(
[0] => 12
[1] => 13
[2] => 14
[3] => 18
[4] => 19
[5] => 20
)
Array
(
[0] => 15
[1] => 16
[2] => 17
[3] => 21
[4] => 22
[5] => 23
)
($i=0;$i<4;$i++)的{
打印(获取索引(4,6,2,3,$i+1));
}
排列
(
[0] => 0
[1] => 1
[2] => 2
[3] => 6
[4] => 7
[5] => 8
)
排列
(
[0] => 3
[1] => 4
[2] => 5
[3] => 9
[4] => 10
[5] => 11
)
排列
(
[0] => 12
[1] => 13
[2] => 14
[3] => 18
[4] => 19
[5] => 20
)
排列
(
[0] => 15
[1] => 16
[2] => 17
[3] => 21
[4] => 22
[5] => 23
)
是的,两个嵌套循环。另外,如果是php,您的变量名称中缺少了$
。好问题。这让我思考。哈哈,谢谢你的工作,我刚刚为这些东西编写了伪代码(最接近php/C)您确定块0(1,2,3,7,8,9)
是正确的吗?应该是块0(0,1,2,6,7,8)
?这似乎非常接近我需要它的目的。谢谢!我将不得不花一段时间来处理它。好的,我看到这得到了给定块中的索引,这就是我一直坚持的。我想我可以格式化我的其他函数,以像你一样返回数组,并推广检查过程。谢谢!