A Famous Grid
Time Limit: 10000/3000 MS (Java/Others)Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 579Accepted Submission(s): 225
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.

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
题意:按图中方式 打印出map(其实就是蛇形填数) 填好数以后如果是素数 则不能走 否则能走
问从 某点到某点的最短距离
另外 打表不能为100*100 要大于100 因为它可以从100之外过去 仅仅只是提问的数据最大为100、
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<queue>
using namespace std;
int a[200][200],s_x,s_y,e_x,e_y,used[200][200];
int prime[40000+100];
struct haha
{
int x;
int y;
int steps;
friend bool operator<(struct haha a,struct haha b)
{
return a.steps>b.steps;
}
}q,temp;
int step[4][2]={1,0,-1,0,0,1,0,-1};
void get_prime()
{
int m=(int)sqrt(40000+0.5);
int c=0,i,j,n=40000;
memset(prime,0,sizeof(prime));
for(i=2;i<=m;i++) if(!prime[i])//目前prime 里存储的还不是素数 只是借用这个数组
{
for(j=i*i;j<=n;j+=i)
prime[j]=1;
}
for(i=2;i<=n;i++) prime[i]=!prime[i]; //现在才是素数
}
void get_map()//蛇形填数
{
int n=120,x=0,y=0,tot=n*n;
memset(a,0,sizeof(a));
a[0][0]=n*n;
while(tot>1)
{
while(y+1<n&&!a[x][y+1]) a[x][++y]=--tot;
while(x+1<n&&!a[x+1][y]) a[++x][y]=--tot;
while(x-1>=0&&!a[x-1][y]) a[--x][y]=--tot;
while(y-1>=0&&!a[x][y-1]) a[x][--y]=--tot;
}
}
int BFS()
{
int i,xx,yy,n=200;
priority_queue<struct haha>que;
memset(used,0,sizeof(used));
q.x=s_x;
q.y=s_y;
q.steps=0;
used[s_x][s_y]=1;
que.push(q);
while(!que.empty())
{
temp=que.top();
que.pop();
if(temp.x==e_x&&temp.y==e_y) return temp.steps;
for(i=0;i<4;i++)
{
xx=temp.x+step[i][0];
yy=temp.y+step[i][1];
if(xx>=0&&xx<n&&yy>=0&&yy<n&&!prime[a[xx][yy]]&&!used[xx][yy])
{
q.x=xx;
q.y=yy;
q.steps=temp.steps+1;
que.push(q);
used[xx][yy]=1;
}
}
}
return -1;
}
int main()
{
int i,j,cas=0,s,e,ans;
get_prime();
get_map();
while(scanf("%d %d",&s,&e)!=EOF)
{
for(i=0;i<200;i++)
for(j=0;j<200;j++)
{
if(a[i][j]==s) {s_x=i;s_y=j;}
if(a[i][j]==e) {e_x=i;e_y=j;}
}
if(prime[a[s_x][s_y]]||prime[a[e_x][e_y]])
{printf("Case %d: %d\n",++cas,ans);continue;}
ans=BFS();
if(ans==-1)
printf("Case %d: impossible\n",++cas);
else printf("Case %d: %d\n",++cas,ans);
}
return 0;
}