nyoj 592 spiral grid

spiral grid

时间限制: 2000  ms   内存限制: 65535  KB
难度: 4
描述
Xiaod 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.)
nyoj <wbr>592 <wbr>spiral <wbr>grid


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. In addition, traveling from a prime number is disallowed, either. 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.
nyoj <wbr>592 <wbr>spiral <wbr>grid
输入
Each test case is described by a line of input containing two nonprime integer 1 <=x, y<=10,000.
输出
For each test case, display its case number followed by the length of the shortest path or "impossible" (without quotes) in one line.
样例输入
1 4 
9 32 
10 12
样例输出
Case 1: 1 
Case 2: 7 
Case 3: impossible


#include
#include
#include
using namespace std;
int m[105][105], a, b, vis[105][105], su[10005] = {0}, dirx[4] = {0,0,1,-1}, diry[4] = {1,-1,0,0};
typedef struct node
{
    int x, y, step;
}node;
void prime()
{
    int i, j;
    su[1] = 1;
    for(i = 2 ; i <= 100 ; i++)         //外层循环,表示把i的倍数们标记
        for(j = i*2 ; j <= 10000 ; j+=i) //内层循环,j就是i的倍数。注意这里j要从i*2开始,否则连素数本身也会被标记
        if(su[j] == 0)
            su[j] = 1;
}
void store()         //打一个蛇形数组
{
    int n = 10000, i = 0 , j = 0, s = 1;
    while(n >= 1)
    {
        for(i = i+1, j = j+1 ; j <= 101 - s ; j++)
        {
            m[i][j] = n;
            n--;
        }
        for(i = i+1, j = j-1 ; i <= 101 - s ; i++)
        {
            m[i][j] = n;
            n--;
        }
        for(i = i-1, j = j-1 ; j >= s ; j--)
        {
            m[i][j] = n;
            n--;
        }
        s++;
        for(i = i-1 , j = j+1 ; i >= s ; i--)
        {
            m[i][j] = n;
            n--;
        }

    }
}
int bfs(int x, int y)           //普通广搜
{
    int i;
    queueq;
    node u, v;
    u.x = x;
    u.y = y;
    u.step = 0;
    q.push(u);
    while(!q.empty())
    {
        u = q.front();
        q.pop();
        if(m[u.x][u.y] == b)
            return u.step;
        for(i = 0 ; i < 4 ; i++)
        {
            v.x = u.x + dirx[i];
            v.y = u.y + diry[i];
            v.step = u.step + 1;
            if(v.x < 1 || v.y < 1 || v.x > 100 || v.y > 100)
                continue;
            if(!vis[v.x][v.y] && su[m[v.x][v.y]])
            {
                vis[v.x][v.y] = 1;
                q.push(v);
            }
        }
    }
    return -1;
}
int main()
{
    int k = 0, x, y, i, j, ans;

    prime();
    store();
    while(~scanf("%d %d", &a, &b))
    {
        k++;
        printf("Case %d: ", k);
        memset(vis, 0, sizeof(vis));
        for(i = 1 ; i <= 100 ; i++)
            for(j = 1 ; j <= 100 ; j++)
            if(m[i][j] == a)
        {
            x = i;
            y = j;
        }
        ans = bfs(x, y);
        if(ans >= 0)
            printf("%d\n", ans);
        else
            printf("impossible\n");
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值