Showing posts with label algorithms. Show all posts
Showing posts with label algorithms. Show all posts

Sunday, November 4, 2012

Bitwise operators in C, JavaScript, Lua and Killa

So I thought that porting a Game Boy Advance (GBA) console emulator written in JavaScript to Killa would be fun, boy I was wrong... All because I wanted to play with this:


Thursday, November 1, 2012

Dynamic programming

Dynamic Programming (DP) problems are fun, but they can be also difficult to solve. For a better definition than I could give you here I'll redirect you to the Wikipedia article, and there are some nice tutorials and sample problems in TopCoder and CodeChef. In this post I'm just going to add commentaries to the DP problem I solved previously without giving much explanation. This problem comes from the Project Euler site:

An n×n grid of squares contains n^2 ants, one ant per square.
All ants decide to move simultaneously to an adjacent square
(usually 4 possibilities, except for ants on the edge of the grid or at the corners). 
We define f(n) to be the number of ways this can happen without any ants ending on 
the same square and without any two ants crossing the same edge between two squares.
You are given that f(4) = 88. Find f(10).

Sunday, September 16, 2012

Combinatorial explosion and Project Euler 393

Last week I found a nice video about algorithmic combinatorial explosion. It was very interesting but it's a bit sad to see the big sister throwing away her life in order to teach her little students the perils of trying to compute combinatorial problems that grow very fast. The problem they studied didn't seem to be hard, just enumerate all the ways you can go from one corner to the opposite corner in a rectangular grid without passing the same node twice. Deceptively simple, for a 1x1 grid there are only 2 ways, for a 2x2 grid there are 12 ways:

Wednesday, February 9, 2011

Solving the Tic-Tac-Toe game, finally...

The simple game that has been always after me laughing at my programming incompetence.

The very first time I tried to make a program that could play perfectly the game was in the university, after taking my very first course in Programming Languages (I learned Pascal). It was holidays and I was visiting my cousin and got the nice surprise of him getting a personal computer. It was a powerful 286 with a monochrome amber monitor. I couldn't find anything similar but for you to get the idea, games were played like this in those computers:

Sunday, August 8, 2010

Solving the Princess in a Box Puzzle

We can solve the puzzle of my last post using a Breadth-first search (BFS) algorithm. The key idea is to realize than the problem is just a Shortest Path problem where the graph, even when it is not completely known at the beginning (because we don't have the graph's adjacency list or adjacency matrix), can be built joining the states the board takes after every movement. The image below can help to understand this. It shows some of the states the board cab take after some movements starting from the beginning.