A Famous Grid
Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1252 Accepted Submission(s): 493
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
以BFS为主的综合题
1.蛇形矩阵
2.筛选法求素数
3.BFS
AC代码
1.蛇形矩阵
2.筛选法求素数
3.BFS
AC代码
#include <iostream>
#include <stdio.h>
#include <queue>
#include <string.h>
#define clr(x) memset(x,0,sizeof(x));
#define MAXN 106
using namespace std;
int a[MAXN+10][MAXN+10],prime[11010],vis[MAXN+10][MAXN+10],pace[MAXN+10][MAXN+10];
int m[4][2]= {{0,1},{1,0},{-1,0},{0,-1}};
void is_prime()
{
int i,j;
for(i=2; i<=11000; i++)
{
if(prime[i]==0)
for(j=i+i; j<=11000; j+=i)
prime[j]=1;
}
}
int f(int x1,int y1)
{
if(vis[x1][y1]==0&&x1>=0&&y1<MAXN&&x1<MAXN&&y1>=0&&prime[a[x1][y1]])
return 1;
return 0;
}
int bfs(int x,int y)
{
queue<int> Q,Qx,Qy;
int i,j,c;
int ex,ey,fx,fy,tx,ty;
int flag1=0,flag2=0;
for(i=0; i<MAXN; i++)
{
for(j=0; j<MAXN; j++)
{
if(a[i][j]==x) fx=i,fy=j,flag1=1;
if(a[i][j]==y ) ex=i,ey=j,flag2=1;
}
if(flag1&&flag2) break;
}
Q.push(x);
Qx.push(fx);
Qy.push(fy);
vis[fx][fy]=1;
while(!Q.empty())
{
c=Q.front();
Q.pop();
fx=Qx.front();
Qx.pop();
fy=Qy.front();
Qy.pop();
if(fx==ex&&fy==ey)
{
return pace[fx][fy] ;
}
for(i=0; i<4; i++)
{
tx=fx+m[i][0];
ty=fy+m[i][1];
if(f(tx,ty))
{
vis[tx][ty]=1;
Q.push(a[tx][ty]);
Qx.push(tx);
Qy.push(ty);
pace[tx][ty]=pace[fx][fy]+1;
}
}
}
return -1;
}
int main()
{
int n,x,y,tot =0 ;
clr(prime);
clr(a);
prime[0]=prime[1]=1;
is_prime();
n=MAXN;
clr(a);
tot = a[x=0][y=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(y-1>=0 && !a[x][y-1]) a[x][--y] = --tot;
while(x-1>=0 && !a[x-1][y]) a[--x][y] = --tot;
}
int Case=1;
while(cin >> x >> y)
{
printf("Case %d: ",Case++);
clr(vis);
clr(pace);
int ans=bfs(x,y);
if(ans==-1) cout << "impossible"<<endl;
else
cout << ans << endl;
}
return 0;
}