List all the position that two kings not encounter in Chinese chess

来源:互联网 发布:计算机编程原理书籍 编辑:程序博客网 时间:2024/06/06 03:56

Title like , there are many ways to resolve this question , but if request you can only use one variable , how to do it . 

For use one variable to resolve this question , we can set a eight bit binary variable , every four bit express one kings position . the can solve it . the code not list below .

There are some more convenient solution . just like this 

$i = 81;while ($i--) {if ($i / 9 % 3 == $i % 9 % 3) {continue;}printf("A=%d,B=%d\n",$i / 9 + 1,$i % 9 + 1);}

And we can use php array also:

$array = array(0,0);for ($array[0] = 1;$array[0] <= 9 ; $array[0]++){for ($array[1] = 1;$array[1] <= 9;$array[1]++){if ($array[0] % 3 != $array[1] % 3) {printf("a=%d,b=%d\n",$array[0],$array[1]);}}}


原创粉丝点击