A Famous Grid
Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1496 Accepted Submission(s): 567
Problem Description
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 in 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.
Input
Each test case is described by a line of input containing two nonprime integer 1 <=x, y<=10,000.
Output
For
each test case, display its case number followed by the length of the
shortest path or "impossible" (without quotes) in one line.
Sample Input
1 4
9 32
10 12
Sample Output
Case 1: 1
Case 2: 7
Case 3: impossible
Source
Fudan Local Programming Contest 2012
/** 题意:给出两个数,问两点之间的最短距离 做法:蛇形矩阵 + bfs + 优先队列 **/ #include <iostream> #include <stdio.h> #include <cmath> #include <algorithm> #include <string.h> #include <queue> #define maxn 40000 using namespace std; int mmap[200][200]; int a[200][200]; int vis[200][200]; int dx[4] = {0,0,-1,1}; int dy[4] = {1,-1,0,0}; int n,m; bool num[maxn]; void is_prime() { int tot = 0; memset(num,false,sizeof(num)); num[1] = true; for(long long i=2; i<maxn; i++) { if(!num[i]) { for(long long j=i*i; j<=maxn; j+=i) { num[j] = true; } } } } struct Node { int x; int y; int step; Node() {} Node(int _x,int _y,int _step) { x = 0; y = 0; step =0; } } start,endd; struct cmp { bool operator () (const Node &a,const Node &b) { return a.step>b.step; } }; int check(int x,int y) { if(x>=0 && x <200 && y >= 0 && y < 200 && num[a[x][y]] == true&& !vis[x][y]) return 1; return 0; } priority_queue<Node,vector<Node>,cmp >que; bool bfs() { memset(vis,0,sizeof(vis)); Node tmp,now; while(!que.empty()) que.pop(); que.push(start); vis[start.x][start.y] = 1; start.step = 0; while(!que.empty()) { now = que.top(); que.pop(); //cout<<now.x<<" "<<now.y<<" "<<now.step<<endl; if(now.x == endd.x && now.y == endd.y) { endd.step = now.step; return true; } for(int i=0; i<4; i++) { tmp.x = now.x + dx[i]; tmp.y = now.y + dy[i]; tmp.step = now.step + 1; if(check(tmp.x,tmp.y)) { vis[tmp.x][tmp.y] = 1; que.push(tmp); } } } return false; } void init() { int x = 0; int y = 0; int nn = 200; int num=a[0][0]=40000; while(num>1) { while((y+1)<nn&&!a[x][y+1]) a[x][++y]= --num; while((x+1)<nn&&!a[x+1][y]) a[++x][y]= --num; while((y-1)>=0&&!a[x][y-1]) a[x][--y]= --num; while((x-1)>=0&&!a[x-1][y]) a[--x][y]= --num; } } int main() { //freopen("in.txt","r",stdin); init(); is_prime(); int Case = 1; while(~scanf("%d %d",&n,&m)) { for(int i=0; i<200; i++) { for(int j=0; j<200; j++) { if(a[i][j] == n) { start.x = i; start.y = j; } if(a[i][j] == m) { endd.x= i; endd.y = j; } } } bool prime = false; prime = bfs(); printf("Case %d: ",Case++); if(prime) printf("%d\n",endd.step); else printf("impossible\n"); } return 0; }