php编写拉丁矩阵问题 跪谢要求如下 php,web应用开发,编程 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 这个应该就是八皇后问题,姑且先用下面这个不完善的吧<?phpfunction judge($row, $column, $number, $array){ for ($i = 0; $i < $row; $i++) { if ($array[$i][$column] == $number) { return false; } } for ($i = 0; $i < $column; $i++) { if ($array[$row][$i] == $number) { return false; } } return true;}function run($input){ $array = array_pad(array(), $input, array_pad(array(), $input, 0)); $nowRow = 0; $nowColumn = 0; $success = 0; for (; $nowRow < $input; $nowRow++) { for (; $nowColumn < $input; $nowColumn++) { $success = 0; $now = $array[$nowRow][$nowColumn] + 1; while ($now <= $input) { if (judge($nowRow, $nowColumn, $now, $array)) { $success = $array[$nowRow][$nowColumn] = $now; break; } else { $now++; } } if (!$success) { $array[$nowRow][$nowColumn] = 0; $nowColumn = $nowColumn - 2; } if ($nowColumn < 0) { $success = 0; break; } } if (!$success) { $array[$nowRow][0] = 0; $nowRow = $nowRow - 2; $nowColumn = $input - 1; } else { $nowColumn = 0; } if ($nowRow < 0) { $success = 0; break; } } if (empty($success)) { return false; } return $array;}function p($array){ foreach ($array as $item) { foreach ($item as $data) { echo $data . ' '; } echo PHP_EOL; }}###########################################################$input = 6;$ret = run($input);p($ret);1 2 3 4 5 6 2 1 4 3 6 5 3 4 5 6 1 2 4 3 6 5 2 1 5 6 1 2 3 4 6 5 2 1 4 3 拉丁方是有其规律的,可以简单的生成Latin(4);function Latin($n) { //int i,j,k,t; //printf("The possble Latin Squares of order %d are:\n",N); for($j=0; $j<$n; $j++) /*构造N个不同的拉丁方阵*/ { for($i=0; $i<$n; $i++) { $t = ($i + $j) % $n; /*确定该拉丁方阵第i 行的第一个元素的值*/ for($k=0; $k<$n; $k++) /*按照环的形式输出该行中的各个元素*/ printf("%2d",($k + $t) % $n + 1); printf("\n"); } printf("\n"); }} 本帖最后由 fdipzone 于 2015-01-03 17:34:29 编辑 define("N", 5);function latinFounder(){ if(N < 1) return false; $flag = array(); for($i=0; $i<N; $i++) { for($j=0; $j<N; $j++) { $value = ($j==0) ? ($i+1)%N : ($flag[$i][$j-1] + 1)%N; $flag[$i][$j] = $value ? $value : N; } } return $flag;}$founder = latinFounder();if(!empty($founder) && is_array($founder)){ foreach($founder as $value) { if(is_array($value)) { foreach($value as $val) { echo $val . " "; } } echo "<br>"; }} 关于form表单action后取值问题 php smarty 引用css文件 css没能完全加载 smarty 关于truncate使用的问题 建网站 请问下gettimeofday()的问题 关于静态化的一些问题 php如何调用mysql存储过程并获得记录集 动态加载php_gd2.dll、php_iconv.dll出错 PHP怎么处理文件中的回车啊? 高分求助,对于rpm安装的Apache+MySQL+PHP怎么设置? 我对ide编码的困惑 __set和__get的属性有办法代码提示么?
<?phpfunction judge($row, $column, $number, $array)
{ for ($i = 0; $i < $row; $i++) { if ($array[$i][$column] == $number) {
return false;
} } for ($i = 0; $i < $column; $i++) { if ($array[$row][$i] == $number) {
return false;
}
} return true;
}
function run($input)
{
$array = array_pad(array(), $input, array_pad(array(), $input, 0)); $nowRow = 0; $nowColumn = 0;
$success = 0; for (; $nowRow < $input; $nowRow++) { for (; $nowColumn < $input; $nowColumn++) { $success = 0; $now = $array[$nowRow][$nowColumn] + 1; while ($now <= $input) { if (judge($nowRow, $nowColumn, $now, $array)) { $success = $array[$nowRow][$nowColumn] = $now; break;
}
else { $now++;
}
} if (!$success) { $array[$nowRow][$nowColumn] = 0; $nowColumn = $nowColumn - 2;
} if ($nowColumn < 0) { $success = 0; break;
}
} if (!$success) { $array[$nowRow][0] = 0; $nowRow = $nowRow - 2; $nowColumn = $input - 1; }
else { $nowColumn = 0;
} if ($nowRow < 0) { $success = 0; break;
} }
if (empty($success)) {
return false;
} return $array;
}
function p($array)
{ foreach ($array as $item) { foreach ($item as $data) { echo $data . ' '; } echo PHP_EOL; }
}###########################################################$input = 6;$ret = run($input);p($ret);
1 2 3 4 5 6
2 1 4 3 6 5
3 4 5 6 1 2
4 3 6 5 2 1
5 6 1 2 3 4
6 5 2 1 4 3
//int i,j,k,t;
//printf("The possble Latin Squares of order %d are:\n",N);
for($j=0; $j<$n; $j++) /*构造N个不同的拉丁方阵*/
{
for($i=0; $i<$n; $i++)
{
$t = ($i + $j) % $n; /*确定该拉丁方阵第i 行的第一个元素的值*/
for($k=0; $k<$n; $k++) /*按照环的形式输出该行中的各个元素*/
printf("%2d",($k + $t) % $n + 1);
printf("\n");
}
printf("\n");
}
}
define("N", 5);function latinFounder()
{
if(N < 1) return false;
$flag = array();
for($i=0; $i<N; $i++)
{
for($j=0; $j<N; $j++)
{
$value = ($j==0) ? ($i+1)%N : ($flag[$i][$j-1] + 1)%N;
$flag[$i][$j] = $value ? $value : N;
}
}
return $flag;
}$founder = latinFounder();if(!empty($founder) && is_array($founder))
{
foreach($founder as $value)
{
if(is_array($value))
{
foreach($value as $val)
{
echo $val . " ";
}
}
echo "<br>";
}
}