Xiaopei's DokuWiki

These are the good times in your life,
so put on a smile and it'll be alright

User Tools

Site Tools


it:drawsomething

Draw Something

Draw Something 是移动设备上的一个和朋友画画猜词的游戏.

程序解题法

计算排列

arrange.php
function arrange($l) {
	$nums = array();
 
	for ($i = 1; $i <= $l; $i++) {
		$nums []= $i;
	}
 
	$stack = $nums;
	$queue = array();
	$q = 2;
 
	$results = array(join($stack));
 
	$ori_stack = $stack;
 
	for ($i = 0; $i < $q; $i++) {
		$queue[] = array_pop($stack);
	}
 
	$q_word = join($queue);
 
	// while ($queue != $nums) {
	while (!in_array($q_word, $results)) {
		for ($i = 0; $i < $q; $i++) {
			$stack[] = array_shift($queue);
		}
 
		$word = join($stack);
 
		if (in_array($word, $results)) {
			$stack = $ori_stack;
			$q++;
		}
		else {
			$results[] = $word;
			$q--;
			if ($q < 2) {
				$q = 2;
			}
		}
 
		$ori_stack = $stack;
 
		for ($i = 0; $i < $q; $i++) {
			$queue[] = array_pop($stack);
		}
 
		$q_word = join($queue);
 
	}
 
	return $results;
}

Finding All Permutations of an Array

it/drawsomething.txt · Last modified: 2013/08/19 07:22 (external edit)