SPIRALGR - A Famous Grid

Mr. B has recently discovered the grid named "spiral grid". Construct the grid like the following figure. (The grid is actually infinite. The figure is only a small part of it.)

Considering traveling around it, you are free to any cell containing a composite number or 1, but traveling to any cell containing a prime number is disallowed. You can travel up, down, left or right, but not diagonally. Write a program to find the length of the shortest path between pairs of nonprime numbers, or report it's impossible.


Each test case is described by a line of input containing two nonprime integer 1 <=x, y<=10,000.


For each test case display its case number followed by the length of the shortest path or impossible in one line.


1 4
9 32
10 12

Case 1: 1
Case 2: 7
Case 3: impossible

hide comments
navneet_01: 2020-10-13 09:19:57

can anyone give me a hint?

Bojan Rosko: 2013-04-15 18:20:15

The spiral is infinite. So, even though x, y <10000 , the output can theoretically be larger than 200 , am I right ? You get numbers from grid 100 x 100, but the path can get out of that grid because the spiral is infinite ?

PetarV: 2013-04-11 06:39:33

It is supposed to keep reading the test cases and solving them until it covers all of them. (there are finitely many test cases in the test input)

Last edit: 2013-04-11 06:42:30
Aleksandar BorzanoviƦ: 2013-04-09 19:12:52

is the program supposed to stop after each test case, or just spin in an infinite loop, solving cases in real time?

Added by:Fudan University Problem Setters
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64
Resource:ACM/ICPC World Finals 2012 - Dressing Rehearsal Contest, also used in FDU Local Contest 2012