puzzle

Fastest algorithm for circle shift N sized array for M position

﹥>﹥吖頭↗ 提交于 2019-12-17 06:30:10
问题 What is the fastest algorithm for circle shifting array for M positions? For example, [3 4 5 2 3 1 4] shift M = 2 positions should be [1 4 3 4 5 2 3] . Thanks a lot. 回答1: If you want O(n) time and no extra memory usage (since array was specified), use the algorithm from Jon Bentley's book, "Programming Pearls 2nd Edition". It swaps all the elements twice. Not as fast as using linked lists but uses less memory and is conceptually simple. shiftArray( theArray, M ): size = len( theArray ) assert

What is a good non-recursive algorithm for deciding whether a passed in amount can be built additively from a set of numbers?

依然范特西╮ 提交于 2019-12-13 09:57:24
问题 What is a non recursive algorithm for deciding whether a passed in amount can be built additively from a set of numbers. In my case I'm determining whether a certain currency amount (such as $40) can be met by adding up some combination of a set of bills (such as $5, $10 and $20 bills). That is a simple example, but the algorithm needs to work for any currency set (some currencies use funky bill amounts and some bills may not be available at a given time). So $50 can be met with a set of ($20

Finding out same colored block in a 2D matrix

旧城冷巷雨未停 提交于 2019-12-13 01:15:03
问题 I am trying to find out a block of same colored region starting from the top left corner in a 2D matrix. For example: I have the following Matrix: 1 1 1 2 2 3 1 1 2 3 4 5 1 1 1 1 3 4 1 4 3 2 1 5 2 3 4 5 1 2 say, The initial top left corner is 1 and I want to find out the adjacent area containing 1's (I will only consider starting from the top left corner). In the above matrix the digits 1,2,3,4,5 represent different colors. I have tried using the following code segment for finding out the

A Better Frog Crossing Algorithm

自作多情 提交于 2019-12-12 11:59:42
问题 I am solving the following problem from Codility: A small frog wants to get to the other side of a river. The frog is currently located at position 0, and wants to get to position X. Leaves fall from a tree onto the surface of the river. You are given a non-empty zero-indexed array A consisting of N integers representing the falling leaves. A[K] represents the position where one leaf falls at time K, measured in minutes. The goal is to find the earliest time when the frog can jump to the

Jigsaw Puzzle Solver Method

白昼怎懂夜的黑 提交于 2019-12-12 10:26:35
问题 Ok, so I have a 3 x 3 jig saw puzzle game that I am writing and I am stuck on the solution method. public Piece[][] solve(int r, int c) { if (isSolved()) return board; board[r][c] = null; for (Piece p : pieces) { if (tryInsert(p, r, c)) { pieces.remove(p); break; } } if (getPieceAt(r, c) != null) return solve(nextLoc(r, c).x, nextLoc(r, c).y); else { pieces.add(getPieceAt(prevLoc(r, c).x, prevLoc(r, c).y)); return solve(prevLoc(r, c).x, prevLoc(r, c).y); } } I know I haven't provided much

Trying to solve 15 Puzzle, OutOfMemoryError [closed]

痴心易碎 提交于 2019-12-12 10:12:51
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 9 years ago . Is there any way that I can optimize this code as to not run out of memory? import java.util.HashMap; import java.util.Map; import java.util.PriorityQueue; import java.util.Random; import java.util.Stack; public

Set Image to Button and process ActionListener in Puzzle Game using Java

我怕爱的太早我们不能终老 提交于 2019-12-12 01:28:57
问题 I only learn Java Swing 1 week so I tried complete some exercises. This is my code. I set 9 icon to 9 button but it doesn't show on button. package mypack; import java.awt.Color; import javax.swing.*; public class PuzzleGame extends JFrame{ static JButton bt1,bt2,bt3,bt4,bt5,bt6,bt7,bt8,bt9,btNew,btExit; static JLabel move, moveNum, time, timeNum; public PuzzleGame(){ createMyGUI(); } public static void createMyGUI(){ JFrame jf = new JFrame("Game Puzzle Java"); JPanel jpl = new JPanel(); Icon

What does this mysterious Color Method do? What does it return?

孤街浪徒 提交于 2019-12-11 07:47:23
问题 Maybe I've had too much coffee, maybe I've been working too long, regardless, I'm at a loss as to what this method does, or rather, why and how it does it, could anyone shed some light upon me? What is the nextColor? public Color nextColor() { int max = 0, min = 1000000000, cr = 0, cg = 0, cb = 0; for (int r = 0; r < 256; r += 4) { for (int g = 0; g < 256; g += 4) { for (int b = 0; b < 256; b += 4) { if (r + g + b < 256 || r + g + b > 512) { continue; } min = 1000000000; for (Color c :

How to find “the first survivor” after a given position in the Josephus puzzle?

只愿长相守 提交于 2019-12-11 02:53:56
问题 I want to find the next survivor after a given position and number of people. (define renumber (lambda (position n) (if (< position 3) (+ position (- n 3)) (- position 3)))) (define survives? (lambda (position n) (if (< n 3) #t (if (= position 3) #f (survives? (renumber position n) (- n 1)))))) (define first-survivor-after (lambda (position n) (cond ((and (<= n 3)(<= position 3)) null) ((or (>= n 3)(>= position 3))(survives? position n) (if = #f survives?) (survives? (+ 1 position) n)

Prolog restrictions - puzzle

牧云@^-^@ 提交于 2019-12-11 01:47:43
问题 I'm developing a game named woodntDie. This game consists in 9 pieces which should look like this: The aim is to arrange all those pieces and make it look like a dice: This is the four final possible solutions with all those 9 pieces. I'm trying to implement some restrictions but those arent enough: sum([V5, V6, V11, V12, V17, V18, V23, V24, V29, V30, V35, V36, V41, V42, V47, V48, V53, V54], #=, 7), The sum of the opposite faces should always be 7. In this restriction I'm telling that the sum