21,886
社区成员
发帖
与我相关
我的任务
分享
public function test($width = 8, $height = 15)
{
$this->width = $width;
$this->height = $height;
// $this->warehouse = array_fill(0, $this->height, array_fill(0, $this->width, 0));
$this->warehouse = [
0 => [1,0,1,1,0,0,1,1],
1 => [1,1,1,1,0,1,1,1],
2 => [0,0,0,0,0,1,1,1],
3 => [1,1,1,1,1,1,1,1],
4 => [1,1,1,0,1,1,0,0],
5 => [0,1,1,0,0,0,0,0],
6 => [0,0,0,0,0,0,0,0],
7 => [0,0,0,0,0,0,0,0],
8 => [0,0,0,0,0,0,0,0],
9 => [0,0,0,0,0,0,0,0],
10 => [0,0,0,0,0,0,0,0],
11 => [0,0,0,0,0,0,0,0],
12 => [0,0,0,0,0,0,0,0],
13 => [0,0,0,0,0,0,0,0],
14 => [0,0,0,0,0,0,0,0],
];
debug($this->warehouse);
}
/**
* 打印当前仓库存储状态
*/
public function dumpWarehouse()
{
$linMax = strlen($this->height);
echo str_pad(' ', $linMax + 1, ' ') . implode(' ', range(1, $this->width)) . '<br>';
foreach ($this->warehouse as $key=>$item) {
echo str_pad($key + 1, $linMax, ' ') . ':' . implode(',', $item) . '<br>';
}
}
/**
* 写入仓库
* @param $width
* @param $height
* @return array|bool
*/
public function put($width, $height)
{
for ($i = 0; $i <= $this->height - $height; $i++) {
for ($j = 0; $j <= $this->width - $width; $j++) {
$w = false;
if (!$this->warehouse[$i][$j]) {
//判断是否满足条件
$w = true;
for ($m = 0; $m < $height; $m++) {
for ($n = 0; $n < $width; $n++) {
if ($this->warehouse[$i + $m][$j + $n]) {
continue 2;
}
}
}
}
if ($w == true) {
$this->edit([$i, $j], $width, $height);
return [$i + 1, $j + 1];
}
}
}
return false;
}
class Warehouse
{
/**
* @var array
*/
private $warehouse;
/**
* @var int
*/
private $width;
/**
* @var int
*/
private $height;
/**
* Warehouse constructor.
* @param int $width
* @param int $height
*/
public function __construct($width = 8, $height = 8)
{
$this->width = $width;
$this->height = $height;
$this->warehouse = array_fill(0, $this->height, 0);
}
/**
* 打印当前仓库存储状态
*/
public function dumpWarehouse()
{
$linMax = strlen($this->height);
echo str_pad(' ', $linMax + 1, ' ') . implode(' ', range(1, $this->width)) . PHP_EOL;
foreach ($this->warehouse as $key=>$item) {
echo str_pad($key + 1, $linMax, ' ') . ':' . implode(',', str_split( substr(strrev(decbin($item + pow(2, $this->width))), 0 , -1))) . PHP_EOL;
}
}
/**
* 写入仓库
* @param $width
* @param $height
* @return array|bool
*/
public function put($width, $height)
{
$bit_sum = [];
for ($i = 0; $i <= $this->width - $width; $i++) {
$w = pow(2, $i);
for ($k = 1; $k < $width; $k++) {
$w += pow(2, $i + $k);
}
$bit_sum[$i] = $w;
}
for ($i = 0; $i <= $this->height - $height; $i++) {
$yu = 0;
for ($j = 0; $j < $height; $j++) {
$yu = $this->warehouse[$i + $j] | $yu;
}
foreach ($bit_sum as $key=>$value) {
if (($yu & $value) == 0) {
for ($j = 0; $j < $height; $j++) {
$this->warehouse[$i + $j] += $value;
}
return [$key + 1, $i + 1];
}
}
}
return false;
}
}
$warehouse = new Warehouse();
var_dump($warehouse->put(1, 2));
$warehouse->dumpWarehouse();
var_dump($warehouse->put(7, 2));
var_dump($warehouse->put(1, 5));
var_dump($warehouse->put(2, 1));
var_dump($warehouse->put(7, 5));
var_dump($warehouse->put(5, 1));
$warehouse->dumpWarehouse();
通过位运算实现的,但是宽度有限制,你也可以试一试
class Warehouse
{
/**
* @var array
*/
private $warehouse;
/**
* @var int
*/
private $width;
/**
* @var int
*/
private $height;
/**
* Warehouse constructor.
* @param int $width
* @param int $height
*/
public function __construct($width = 8, $height = 8)
{
$this->width = $width;
$this->height = $height;
$this->warehouse = array_fill(0, $this->height, array_fill(0, $this->width, 0));
}
/**
* 打印当前仓库存储状态
*/
public function dumpWarehouse()
{
$linMax = strlen($this->height);
echo str_pad(' ', $linMax + 1, ' ') . implode(' ', range(1, $this->width)) . PHP_EOL;
foreach ($this->warehouse as $key=>$item) {
echo str_pad($key + 1, $linMax, ' ') . ':' . implode(',', $item) . PHP_EOL;
}
}
/**
* 写入仓库
* @param $width
* @param $height
* @return array|bool
*/
public function put($width, $height)
{
for ($i = 0; $i <= $this->height - $height; $i++) {
for ($j = 0; $j <= $this->width - $width; $j++) {
$w = false;
if (!$this->warehouse[$i][$j]) {
//判断是否满足条件
$w = true;
for ($m = 0; $m < $height; $m++) {
for ($n = 0; $n < $width; $n++) {
if ($this->warehouse[$i + $m][$j + $n]) {
continue 2;
}
}
}
}
if ($w == true) {
$this->edit([$i, $j], $width, $height);
return [$i + 1, $j + 1];
}
}
}
return false;
}
/**
* 将满足条件的置为1
* @param array $start
* @param $width
* @param $height
*/
private function edit(array $start, $width, $height)
{
for ($i = 0; $i < $width; $i++) {
for ($j = 0; $j < $height; $j++) {
$this->warehouse[$start[0] + $j][$start[1] + $i] = 1;
}
}
}
}
$warehouse = new Warehouse();
var_dump($warehouse->put(1, 2));
$warehouse->dumpWarehouse();
var_dump($warehouse->put(8, 5));
$warehouse->dumpWarehouse();
var_dump($warehouse->put(7, 2));
$warehouse->dumpWarehouse();
试一试<?php
$arrs =
[
0 => [1, 0, 1, 1, 0, 0, 1, 1],
1 => [1, 1, 1, 1, 0, 1, 1, 1],
2 => [0, 0, 0, 0, 0, 1, 1, 1],
3 => [1, 1, 1, 1, 1, 1, 1, 1],
4 => [1, 1, 1, 0, 1, 1, 0, 0],
5 => [0, 1, 1, 0, 0, 0, 0, 0],
6 => [0, 0, 0, 0, 0, 0, 0, 0],
7 => [0, 0, 0, 0, 0, 0, 0, 0],
8 => [0, 0, 0, 0, 0, 0, 0, 0],
9 => [0, 0, 0, 0, 0, 0, 0, 0]
];
//2X2 坐标
$a =[1,1];
$w = count($arrs[0]) - $a[0];
$h = count($arrs) - $a[1];
foreach ($arrs as $key => $arr) {
if ($key >= $h) {
break;
}
foreach ($arr as $k => $i) {
if ($k >= $w) {
break;
}
if ($i==1){
continue;
}
$sum =[];
for ($s=0;$s<=$a[1];$s++){
for ($d=0;$d<=$a[0];$d++){
$sum =array_merge($sum,[$arrs[$s+$key][$d+$k]]);
}
}
$site=[];
if (count($sum)>0){
if (array_sum($sum)<1){
$site=[$key,$k];
break;
}
}
}
if (count($site)>0){
var_dump($site);//坐标
break;
}
}