Avoider
Time limit1sMemory limit256 MB
Count self-avoiding walks of each length from a to b in the first quadrant starting east from the origin and print the sum.
- Level
Medium7 of 10
- Topics
- Backtracking, DFS, Brute force
- Solved
- No attempts yet
Problem
Use only the points of the plane that have integer coordinates. A walk starts at the origin and its first step goes to . Every later step goes to one of the four integer points next to the current point, up, down, left, or right. The walk may stand only on points whose coordinates are both nonnegative, and it may never step on a point it has already visited.
Fix the number of steps and count the walks of that kind. The first step counts toward . For equal to 2, 3, and 4 the counts are 2, 5, and 12.

You are given two integers and . Compute the number of walks for each of and print the sum.
Input
The first line contains two integers and separated by a space.
Output
Print, on one line, the sum of the walk counts for . The sum can exceed the range of a 32-bit integer.