NYOJ592spiral grid



 思路:(1)建图  (2)打素数表 (3)BFS搜索

代码:
#include <cstdio>
#include <cstring>
#include <cmath>

int s[110][100];
int v[110][100];
int t_cnt = 1;
bool flag ;
typedef struct node
{
    int x,y;
    int step;
} node;
node st,en,que[500000];
void init()

{
    memset(s,0,sizeof(s));
    int cnt = 10000;
    //printf("!!");
    for(int i = 0; i < 100; ++i)s[0][i] = cnt--;
    for(int i = 1; i < 100; ++i)s[i][99] = cnt--;
    for(int i = 98; i >= 0; --i)s[99][i] = cnt--;
    for(int i = 98; i > 0; --i)s[i][0] = cnt--;
    int i = 1,j = 1;
    //printf("haha");
    while(cnt > 0)
    {
        while(!s[i][j])s[i][j++] = cnt--;
        --j;
        ++i;
        while(!s[i][j])s[i++][j] = cnt--;
        --i;
        --j;
        while(!s[i][j])s[i][j--] = cnt--;
        ++j;
        --i;
        while(!s[i][j])s[i--][j] = cnt--;
        ++i;
        ++j;
    }

}
int isP[10000 + 5];

void prime()

{
    memset(isP,-1,sizeof(isP));
    isP[1] = 0;
    for(int i = 2; i <= 10000; ++i)
    {
        if(isP[i])
        {
            for(int j = 2; j * i <= 10000; ++j)
            {
                isP[j * i] = 0;
            }
        }
    }
}

int dx[] = {-1,0,1,0};
int dy[] = {0,1,0,-1};

void bfs()

{
    memset(que,0,sizeof(que));
    node t,temp;
    st.step = 0;
    int head,tail;
    head = tail = 0;
    que[tail++] = st;
    while(head < tail)
    {
         temp = que[head++];
         for(int i = 0;i < 4;++i)
         {
             t.x = temp.x + dx[i];
             t.y = temp.y + dy[i];
             t.step = temp.step + 1;
             if(t.x == en.x && t.y == en.y)
             {
                 flag = true;
                 printf("Case %d: %d\n",t_cnt++,t.step);
                 return ;
             }
             if(t.x >= 0&& t.x <100&&t.y>=0&&t.y<100&&!isP[s[t.x][t.y]]&&!v[t.x][t.y])
             {
                 v[t.x][t.y] = 1;
                 que[tail++] = t;
             }
         }
    }
}
int main()

{
    int a,b;
    init();
    prime();

    while(~scanf("%d%d",&a,&b))
    {
       if(isP[b])
            printf("Case %d: impossible\n",t_cnt++);
       else if(a == b)
            printf("Case %d: 0\n",t_cnt++);
        else
        {
            memset(v,0,sizeof(v));
            flag = false;
            for(int i = 0; i < 100; ++i)
                for(int j = 0; j < 100; ++j)
                    if(s[i][j] == a)
                    {
                        st.x = i;
                        st.y = j;
                    }
                    else if(s[i][j] == b)
                    {
                        en.x = i;
                        en.y = j;
                    }
            v[st.x][st.y] = 1;
            bfs();
            if(!flag)
                printf("Case %d: impossible\n",t_cnt++);
        }
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值