hdu A Famous Grid (bfs)

本文介绍了一个基于无限螺旋网格的算法问题,目标是在不经过素数格子的情况下,找到两个非素数格子间的最短路径。文章详细描述了输入输出格式、样例,并提供了一段C++代码实现,涉及深度优先搜索、广度优先搜索和素数判断等关键技术。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

A Famous Grid

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

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 4
9 32
10 12

Sample Output
Case 1: 1
Case 2: 7
Case 3: impossible
//这题好坑,100x100的数据大小,但是最优的走法的网格却有可能超过100x100.。。

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N= 1e6+1000;
int w[305][305], maxt=200;
bool vis[305][305];
int dir[4][2]= {{1,0},{0,-1},{-1,0},{0,1}};
unordered_map<int,pair<int,int>>q;
void dfs(int x,int y,int v,int id)
{
    q[v]=pair<int,int>(x,y);
    //cout<<q[v].first<<q[v].second<<endl;
    w[x][y]=v;
    if(v==1)return ;
    int ax=x+dir[id][0],ay=y+dir[id][1];
    if(ax>maxt||ax<=0||ay>maxt||ay<=0||w[ax][ay]!=-1)
    {
        id=(id+1)%4;
        ax=x+dir[id][0],ay=y+dir[id][1];
        dfs(ax,ay,v-1,id);
    }
    else dfs(ax,ay,v-1,id);
    return ;
}
struct node
{
    int x, y, step;
    node(int u,int v,int w):x(u),y(v),step(w) {}
};

queue<node>qx;

bool check(int x)
{
    if(x==1)return 0;
    int k=sqrt(x);
    for(int i=2; i<=k; i++)
        if(x%i==0)return 0;
    return 1;
}
int in[100001], out[100001];
int main()
{
//    for(int i=0;i<=10000;i++)
//    {
//        qx.push(node(1,1,0));
//    }
    memset(w,-1,sizeof(w));
    memset(in,0,sizeof(in));
    memset(out,0,sizeof(out));
    for(int i=2; i<=40000; i++)
    {
        if(in[i])continue;
        out[i]=1;
        for(int j=i*2; j<=40000; j+=i)
        {
            in[j]=1;
        }
    }
    int cx=1, cy=200, id=0, v=40000;
    while(v!=0)
    {
        q[v]=pair<int,int>(cx,cy);
        //cout<<q[v].first<<q[v].second<<endl;
        w[cx][cy]=v;
        if(v==1)break;
        int ax=cx+dir[id][0],ay=cy+dir[id][1];
        if(ax>maxt||ax<=0||ay>maxt||ay<=0||w[ax][ay]!=-1)
        {
            id=(id+1)%4;
            ax=cx+dir[id][0],ay=cy+dir[id][1];
        }
        cx=ax,cy=ay;
        v--;
    }
    //dfs(1,200,40000,0);
    //cout<<q[9].first<<" "<<q[9].second<<endl;
    //cout<<q[32].first<<" "<<q[32].second<<endl;
//    for(int i=10;i>=1;i--)
//    {
//        for(int j=1;j<=10;j++)
//        {
//            printf("%4d ",w[j][i]);
//        }
//        puts("");
//    }
    //cout<<w[1][100]<<endl;
    int x, y, ncase=1;
    //cout<<check(1)<<" "<<check(4)<<endl;
    while(scanf("%d %d", &x, &y)!=EOF)
    {
        memset(vis,0,sizeof(vis));
        while(!qx.empty())qx.pop();
        vis[q[x].first][q[x].second]=1;
        qx.push(node(q[x].first,q[x].second,0));
        int flag=0, cnt=-1;
        while(!qx.empty())
        {
            node u=qx.front();
            qx.pop();
            int ax=u.x,ay=u.y,ans=u.step;
            if(w[ax][ay]==y)
            {
                if(cnt==-1||cnt>ans)cnt=ans;
                flag=1;
                break;
            }
            for(int i=0; i<4; i++)
            {
                int bx=ax+dir[i][0],by=ay+dir[i][1];
                if(bx<=0||bx>maxt||by<=0||by>maxt||vis[bx][by]||w[bx][by]==-1||check(w[bx][by]))continue;
                if(w[bx][by]==y)
                {
                    cnt=ans+1;
                    flag=1;
                    break;
                }
                vis[bx][by]=1;
                qx.push(node(bx,by,ans+1));
            }
            if(cnt!=-1)break;
        }
        if(!flag)printf("Case %d: impossible\n",ncase++);
        else printf("Case %d: %d\n",ncase++,cnt);
    }

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值