being able to solve google code jam problem sets [closed] - logic

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
This is not a homework question, but rather my intention to know if this is what it takes to learn programming. I keep loggin into TopCoder not to actually participate but to get the basic understand of how the problems are solved. But to my knowledge I don't understand what the problem is and how to translate the problem into an algorithm that can solve it.
Just now I happen to look at ACM ICPC 2010 World Finals which is being held in china. The teams were given problem sets and one of them is this:
Given at most 100 points on a plan with distinct x-coordinates,
find the shortest cycle that passes through each point exactly once,
goes from the leftmost point always to the right until it reaches the
rightmost point, then goes always to the left until it gets back to the
leftmost point. Additionally, two points are given such that the the path
from left to right contains the first point, and the path from right to
left contains the second point. This seems to be a very simple DP: after
processing the last k points, and with the first path ending in point a
and the second path ending in point b, what is the smallest total length
to achieve that? This is O(n^2) states, transitions in O(n). We deal
with the two special points by forcing the first path to contain the first
one, and the second path contain the second one.
Now I have no idea what I am supposed to solve after reading the problem set.
and there's an other one from google code jam:
Problem
In a big, square room there are two point light sources:
one is red and the other is green. There are also n circular pillars.
Light travels in straight lines and is absorbed by walls and pillars.
The pillars therefore cast shadows: they do not let light through.
There are places in the room where no light reaches (black), where only
one of the two light sources reaches (red or green), and places where
both lights reach (yellow). Compute the total area of each of the four
colors in the room. Do not include the area of the pillars.
Input
* One line containing the number of test cases, T.
Each test case contains, in order:
* One line containing the coordinates x, y of the red light source.
* One line containing the coordinates x, y of the green light source.
* One line containing the number of pillars n.
* n lines describing the pillars. Each contains 3 numbers x, y, r.
The pillar is a disk with the center (x, y) and radius r.
The room is the square described by 0 ≤ x, y ≤ 100. Pillars, room
walls and light sources are all disjoint, they do not overlap or touch.
Output
For each test case, output:
Case #X:
black area
red area
green area
yellow area
Is it required that people who program should be should be able to solve these type of problems?
I would apprecite if anyone can help me interpret the google code jam problem set as I wish to participate in this years Code Jam to see if I can do anthing or not.
Thanks.

It is a big mistake to start from hard problems. Many World Finals problems are too hard for lots of experienced programmers, so it is no surprise that it is also too hard for someone new.
As others have said, start with much easier problems. I am assuming you know the basics of programming and can write code in at least one programming language. Try problems from Division-2 problemsets on TopCoder, and Regional/Qualifying rounds of ACM ICPC. Find out the easy problems from sites like SPOJ, UVa and Project Euler (there are lists of easy problems available online) and solve them. As you solve, also read up on the basics of algorithms and computer science. TopCoder is a great resource since they have lots of tutorials and articles and also allow you to view other people's solutions.
IMHO, becoming a better programmer in general takes a lot of practice and study. There is no shortcut. You cannot assume that you are some sort of hero programmer who can just jump in and solve everything. You just have to accept that there is a long way to go, and start at the beginning.

You should start with much easier problems. Try looking for regionals, or even get the problems from the local schools contest.
You need to have a large view of general programming, from data structures to algorithms. Master a basic programming language, one that the answers are accepted.

You cannot start learning programming by doing competitions. If you don't know any programming at all, the first programs you will write are things like "hello world", fibonacci and ackermann. Starting with things like TopCoder is like learning to drive using a formula one car. It doesn't work that way.

In short, you have to know some basic techniques that are used to develop this kinds of problems. Knowing dynamic programming, backtracking algorithms, searching, etc, helps you a lot when solving the problems.
This one from Google Code Jam is actually pretty hard, and involves computational geometry algorithms. How to solve it is detailed here: http://code.google.com/codejam/contest/dashboard?c=311101#s=a&a=5

I've been working Google Code Jam problems for the past week or so, and I think they are great exercises. The key is to find problems that stretch your abilities a little, but aren't the ones that make you want to give up. Google Code Jam problems range widely in difficulty!
I recommend starting with the ones under "Where should I start" here:
http://code.google.com/codejam/contests.html
And then explore all of the competition round 1 problems. If those are too easy move up to the other rounds.
The thing I really like about code jam is that you can use pretty much any language you want, and you can get feedback from their automated judge. If you run out of Code Jam problems check out some of the other sites that others have mentioned.

You are very right JesperE. JPRO, go back to the basics and get it settled from there. Your ability to compete and win programming contests depends on just two things:
Your deep understanding of the language you are using and
your mental flexibility with the language.

Read Problem Solving Through Problems by Larson.
It's for mathematics but I find it extremely useful for solving algorithm problems.

Related

Developing a Checkers (Draughts) engine, how to begin?

I'm a relatively inexperienced programmer, and recently I've been getting interested in making a Checkers game app for a school project. I'm not sure where I can start (or if I should even attempt) at creating this. The project I have in mind probably wouldn't involve much more than a simple AI & a multiplayer player mode.
Can anyone give some hints / guidance for me to start learning?
To some extent I agree with some of the comments on the question that suggest 'try something simpler first', but checkers is simple enough that you may be able to get a working program - and you will certainly learn useful things as you go.
My suggestion would be to divide the problem into sections and solve each one in turn. For example:
1) Board representation - perhaps use an 8x8 array to represent the board. You need to be able to fill a square with empty, white piece, black piece, white king, black king. A more efficient solution might be to have a look at 'bit-boards' in which the occupancy of the board is described by a set of 64-bit integers. You probably want to end up with functions that can load or save a board state, print or display the board, and determine what (if anything ) is at some position.
2) Move representation - find a way to calculate legal moves. Which pieces can move and where they can move to. You will need to take into account - moving off the edges of the board, blocked moves, jumps, multiple jumps, kings moving 'backwards' etc. You probably want to end up with functions that can calculate all legal moves for a piece, determine if a suggested move is legal, record a game as a series of moves, maybe interface with the end user so by mousing or entering text commands you can 'play' a game on your board. So even if you only get that far then you have a 'product' you can demonstrate and people can interact with.
3) Computer play - this is the harder part - You will need to learn about minimax, alpha-beta pruning, iterative deepening and all the associated guff that goes into computer game AI - some of it sounds harder than it actually is. You also need to develop a position evaluation algorithm that measures the value of a position so the computer can decide which is the 'best' move to make. This can be as simple as the naive assumption that taking an opponents piece is always better than not taking one, that making a king is better than not making one, or that a move that leaves you with more future moves is better than one that leaves you with less choices for your next move. In practice, even a very simple 'greedy' board evaluation can work quite well if you can look 2-3 moves ahead.
All in all though, it may be simpler to look at something a little less ambitious than checkers - Othello is possibly a good choice and it is not hard to write an Othello player that can thrash a human who hasn't played a lot of the game. 3D tic-tac-toe, or a small dots-and-boxes game might be suitable too. Games like these are simpler as there are no kings or boundaries to complicate things, all (well most) moves are legal and they are sufficiently 'fun' to play to be a worthwhile software demonstration.
First let me state, the task you are talking about is a lot larger then you think it is.
How you should do it is break it down into very small manageable pieces.
The reasons are
Smaller steps are easier to understand
Getting fast feed back will help inspire you to continue and will help you fix things as they go wrong.
As you start think of the smallest step possible of something to do. Here are some ideas of parts to start:
Make a simple title screen- Just the title and to hit a key for it to
go away.
make the UI for an empty checkerboard grid.
I know those sound like not much but those will probably take much ore time than you think.
then add thing like adding the checkers, keeping the the gameboard data etc.,
Don't even think about AI until you have a game that two players can play with no UI.
What you should do is think about: what is the smallest increment I can do and add that, add that and then think about what the next small piece is.
Trust me this is the best way about going about it. If you try to write everything at once it will never happen.

Calculating efficient use of window casing (trim)

I'm working on an app that's going to estimate building material for my business. The part I'm working on right now deals specifically with trim that goes around windows.
The best way to explain this is to give an example:
Window trim is purchased in lengths of 14 feet (168 inches). Let say I have 5 rectangular windows of various sizes, all of which consist of 4 pieces of trim each (the top and bottom, and right and left). I'm trying to build an algorithm that will determine the best way to cut these pieces with the least amount of waste.
I've looked into using permutations to calculate every possibly outcome and keep track of waste, but the number of permutations where beyond the trillions once I got past 5 windows (20 different pieces of trim).
Does anyone have any insight on how I might do this.
Thanks.
You are looking at a typical case of the cutting stock problem.
I find this lecture from the University of North Carolina (pdf) is rather clear. More oriented towards implementing, with an example throughout, and few requirements -- maybe just looking up a few acronyms. But there are also 2 hours of video lectures from the university of Madras on the topic, if you want more details and at a reasonably slow pace.
It relies on solving the knapsack problem several times, which you can grab directly from Rosetta Code if you don't want to go through a second linear optimization problem.
In short, you want to select some ways (how many pieces of each length) in which to cut stock (in your case the window trim), and how many times to use each way.
You start with a trivial set : for each length you need, make a way of cutting with just that size. You then iterate : the knapsack problem gives the least favourable way to cut stock from your current configuration, and the simplex method then "removes" this combination from your set of ways to cut stock, by pivoting.
To optimize the casements on windows and bi-fold doors for the company i worked for, i used this simple matrix - i simply took the most common openings and decided what would be the most reasonable and optimal cut lengths.
for example a 3050 window could be trimmed with waste by using one 8' cut and one 12'cut.

Is there a well known algorithm fill in the grid given a set of points?

I saw this game here Flow, it looks quite interesting.
Connect matching colors with pipe to create a flow. Pair all colors,
and cover the entire board to solve each puzzle. But watch out, pipes
will break if they cross or overlap.
Given a set of pairs (x, y), is there an algorithm to solve the puzzle, i.e. fill in the whole grid (assuming there is a solution) that I'm not aware of?
This is a very specific instance of the global routing problem. Global routing is a well studied problem in VLSI CAD (where one needs to route millions of nets in an integrated circuit). The problem is NP-complete and can be solved in many ways depending upon the tradeoff you need between runtime and quality. Following wiki is a good starting point:
https://en.wikipedia.org/wiki/Routing_(electronic_design_automation)
Paper here gives a survey of various techniques:
http://dropzone.tamu.edu/~jhu/publications/HuIntegration01.pdf
Bear in mind that the pointers I had given typically try to solve a far more complex version of the problem you had stated. Never-the-less, the mathematical concepts remain the same.

Connecting pieces of wires- algorithm

I'm using C#, but in the future I might need to use it on other languages.
Many games have such puzzles. There is a group of wires (there are 2 types of wires: straight and curved.), there is a place from where a signal comes and there is a place from where the signal must leave. But the arrangement of the wires doesn't allow that to happen. You must turn some of the wires in order to create a path for the signal.
Yeah, I'm trying to find the continent America again, in order not to try finding it more than once in the future.
Somewhere in the future I will also try the same thing but this time with wires that split the signal to 2 or 3 signals.
Problem is that I can't think of an algorithm that I can imagine how to turn it into a code. I have been thinking for some time and I can't think of anything good.
So, can you help me? I will be able to understand the algorithm as "what does the program has to do", but I basically need help with understanding the algorithm as "how to write the code".
Thanks!
Take a look at some maze generation algorithms -- they do the same thing you're looking for, and as such are what you'll need to create the grid. Pick a simple 'cell-carver' from the ones I linked to; randomly rotate all of the wire-pieces, et voilà!
A comment on your question notes that turning an algorithm into code involves breaking it down bit by bit -- so that's what we'll do:
You'd start with a grid of potential wire locations (probably 2D, but a 3D game would be amazing).
To produce a solvable level, you could do a couple things -- produce a level, and see if it is solvable (bad), or produce a solved level, then "unsolve" it (better). As I alluded to above, producing a solved level involves algorithms very similar to maze generation -- a solved level would have many traversable "paths", just like a maze. Unsolving the level would just loop through all wire pieces, and rotate them a bit.
But what of maze generation? The resource I linked to contains some algorithms that would be perfect for your use -- a simple randomized DFS should be plenty for your needs.
You'll note that I covered the general case involving branching wires -- as it is, excluding branching means you have to do more coding, to "prune" branches intelligently -- that is, backtracking when your one true path gets stuck, say in a corner, due to the random movement probably necessary to make an interesting level.
You should also note that a solution for such a game would, if I understood correctly, require use of all given wire (I know some games like this). Otherwise, the game would probably be a lot simpler to play given the above generation tactics.
The Union-Find algorithms (that answer my problem) are explained in this PDF file: http://www.cs.princeton.edu/~rs/AlgsDS07/01UnionFind.pdf
I don't think I would have ever thought of this algorithm, let alone making it fast.

Algorithm for schematizing (metro) maps

This is a long shot, but I thought I might try before starting the dirty work.
I've got a project to build an application which will, for a defined input stations (vertices) and lines (edges), that is, a real map of some public transportation, schematize a given map into a metro map. I've done some research on the problem and it's an NP-complete problem equivalent to the 3-SAT problem. I also have some theoretic ideas on how to generate such a map, but they aren't detailed enough.
What I'm looking for is any other existing solution of this problem, some sort of pseudo-code, some real code in (almost) any other programming language etc, anything that would reduce the time I need to spend working on the algorithm itself, which will in return give me more time to work on other aspects of the application.
If anyone has ever seen anything that can help me, I'd appreciate it very much.
If you google for "metro map layout problem" and "metro map line crossing" you'll find a lot of references, since it has been researched very actively in the past 10 years.
The problem seems no trivial at all, and translating the "artistic" features to mathematical constraints is seemingly one of the most difficult tasks.
Anyway here are three publications that I found interesting to start with (among many, many others):
Metro Map Layout Using Multicriteria Optimization
Line Crossing Minimization on Metro Maps
The Metro Map Layout Problem
HTH!
Research that's similar to your topic: http://graphics.stanford.edu/papers/routemaps/
This is just some suggestion with handwaving - take with a pinch of salt.
My notion of a "metro" map is one where lines tend to one of the eight cardinal directions and stations are regularly spaced.
I'm assuming you're trying to convert a set of real coordinates into "metro" coordinates.
I would start with your main route (e.g., a city loop), then incrementally add other routes in order of importance.
For each route you want to find the nearest approximation that uses the fewest number of straight lines travelling in the eight cardinal directions. You might do this by starting with the bounding box for the real coordinates, splitting that into a grid, then finding a "metro" route from grid square to grid square, then successively refining that route to reduce the number of bends without distorting the map too much and without introducing crossings with other routes if at all possible.
Having done that, scale each line so that consecutive stations are the same distance apart on the "metro" view.
My guess is you'll still want to support manual tweaking of the result.
Good luck!
Feels like a planning problem.
Looks like your hard constraints are:
Every station must be on a point. A points are on a grid with a distance of X between points (I'd make this static on 2cm)
There should not be 2 stations on the same spot
There should be enough room to draw the station label. Note that the label can be assigned different directions from the point to which the station is assigned.
There should be enough room to draw the subway lines.
Looks like your soft constraints are:
For each station, minimize the actually geographical location distance to the point assigned to the station.
Then throw something like Drools Planner on it, here's an example of hard and soft constraints for nurse rostering.

Resources