hdu 4255 #BFS#筛素数#环形数

来源:互联网 发布:2017网络热词 编辑:程序博客网 时间:2024/06/11 14:40

A Famous Grid

Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 183    Accepted Submission(s): 66


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 49 3210 12
 

Sample Output
Case 1: 1Case 2: 7Case 3: impossible
 

Source
Fudan Local Programming Contest 2012
 

Statistic | Submit | Discuss | Note



///BFS;notice-- the 2D_array must larger than 100*100#include <stdio.h>#include <string.h>#include <queue>using namespace std;struct Point{    int x,y,step;    Point(int a = 0,int b = 0,int c = 0){step = c;x = a;y = b;}};const int N = 200,NN = N * N;int mat[N][N];int i,j,k,n = NN;int pri[NN] = {1,1,0};int vis[N][N] ;int move[][2] = {0,1,1,0,0,-1,-1,0};int ok(int x,int y){    return x >= 0&& y>= 0 && x < N && y < N && pri[mat[x][y]] ;}int bfs(int x,int y){    if(x == y)        return 0;    int i,j;    memset(vis,0,sizeof(vis));    Point p,dst,pt;    for(i = 0; i < N; ++i)        for(j = 0; j < N; ++j)        {            if(mat[i][j] == x)            {                p = Point(i,j,0);                vis[i][j] = 1;            }        }    queue<Point> que;    que.push(p);    while(!que.empty())    {        p = que.front();        que.pop();        for(i = 0; i < 4; ++i)        {            pt.x = p.x + move[i][0];            pt.y = p.y + move[i][1];            if(ok(pt.x,pt.y) && !vis[pt.x][pt.y])            {                vis[pt.x][pt.y] = 1;                pt.step = p.step + 1;                if(mat[pt.x][pt.y] == y)                    return pt.step;                que.push(pt);            }        }    }    return -1;}int main(){    int t = N/2;    for(k = 0; k < t; ++k)    {        for(i = k,j = i; j < N - k; ++j)            mat[i][j] = n--;        for(--j,++i; i < N - k; ++i )            mat[i][j] = n--;        for(--i,--j; j >= k; --j)            mat[i][j] = n--;        for(++j,--i; i > k; --i)            mat[i][j] = n--;    }    for(i = 2; i < NN; ++i)        if(!pri[i])            for(j = i<<1; j < NN; j += i)                pri[j] = 1;    int cas = 0,x,y;    while(scanf("%d%d",&x,&y) == 2)    {        int dis = bfs(x,y);        if(dis == -1)            printf("Case %d: impossible\n",++cas);        else            printf("Case %d: %d\n",++cas,dis);    }    return 0;}