Topic Connection
http://acm.hdu.edu.cn/showproblem.php?pid=4255
A Famous griddescription
Mr. B has recently discovered the grid named "Spiral Grid".
Construct the grid like the following figure. (The grid is actually infinite. The figure was only a small part of it.)
Considering traveling in it, you is free to any cell containing a composite number or 1, but traveling to any cell contai Ning a prime number is disallowed. You can travel up, down, left or right, and not diagonally. Write a program to find the length of the shortest path between pairs of nonprime numbers, or report it ' s impossible.
Input
Each test case was described by a line of input containing the nonprime integer 1 <=x, y<=10,000.
Output
For each test case, display it case number followed by the length of the shortest path or "impossible" (without quotes) I n One line.
Sample Input
1 4
9 32
10 12
Sample Output
Case 1:1
Case 2:7
Case 3:impossible
Snake fill number +bfs.
#include <algorithm> #include <iostream> #include <cstdlib> #include <cstring> #include < cstdio> #include <vector> #include <queue> #include <map>using std::map;using std::min;using std:: Find;using std::p air;using std::vector;using std::multimap;using std::p riority_queue; #define PB (E) push_back (e) # Define SZ (c) (int) (c). Size () #define MP (A, b) Make_pair (A, B) #define ALL (c) (c). Begin (), (c). End () #define ITER (c) __typeof ((c). Begin ()) #define CLS (arr, Val) memset (arr, Val, sizeof (arr)) #define Cpresent (C, E) (Find (All (c), (e))! = (c). End ()) #d Efine Rep (i, n) for (int i = 0; l < (int) n; i++) #define TR (c, I) for (ITER (c) i = (c). Begin (); I! = (c). end (); ++i) const int N = 110;int Sx, Sy, g[n][n];bool prime[n * N + ten], Vis[n][n];const int dx[] = {0, 0,-1, 1}, dy[] = {-1, 1, 0, 0}; struct Node {int x, y, S; Node (int i = 0, int j = 0, int k = 0): x (i), Y (j), S (k) {} inline bool operator< (const Node &x) Const {R Eturn s > x.s; }};bool IsPrime (int n) {for (int i = 2; I * I <= n; i++) {if (n% i = = 0) return false; } return n! = 1;} void init () {int x = 0, y = 0, tot = n * n; G[0][0] = n * N; while (Tot > 1) {while (Y + 1 < N &&!) G[x][y + 1]) g[x][++y] =--tot; while (x + 1 < N &&!) G[x + 1][y]) g[++x][y] =--tot; while (y-1 >= 0 &&!) G[X][Y-1]) g[x][--y] =--tot; while (x-1 >= 0 &&!) G[x-1][y]) g[--x][y] =--tot; } for (int i = 1; I <= n * n; i++) {Prime[i] = IsPrime (i); }}void BFS (int tar) {cls (Vis, false); Priority_queue<node> Q; Q.push (Node (Sx, Sy, 0)); Vis[sx][sy] = true; while (!q.empty ()) {Node t = q.top (); Q.pop (); Rep (I, 4) {int x = Dx[i] + t.x, y = dy[i] + t.y; if (x < 0 | | x >= N | | y < 0 | | y >= N) continue; if (Prime[g[x][y]] | | vis[x][y]) continue; if (g[x][y] = = tar) {printf ("%d\n", T.s +1); Return } q.push (Node (x, y, T.S + 1)); Vis[x][y] = true; }} puts ("impossible");} void Solve (int n, int m, int &k) {Rep (i, N) {rep (j, N) {if (g[i][j] = = N) Sx = i, Sy = j; }} printf ("Case%d:", k++); BFS (m);} int main () {#ifdef LOCAL freopen ("In.txt", "R", stdin); Freopen ("OUT.txt", "w+", stdout); #endif init (); int n, m, k = 1; while (~SCANF ("%d%d", &n, &m)) {Solve (n, M, k); } return 0;}
Hdu 4255 A Famous Grid