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
 
 
 
 
 
 
 
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
int book[401][401];
int a[401][401];
int b[401][401];
int vis[160005];
int prime[160005];
struct node
{
    int x;
    int y;
    int s;
} que[160005];
void make1()
{
   for(int i=1;i<=160001;i++)
   prime[i]=1;
        prime[1] = 0;
    for(int i = 2; i <= 160001; i++)
    {
        if(prime[i])
        {
            for(int j = 2*i; j <= 160001; j+=i)
                prime[j] = 0;
        }
    }

int x,y;
    int n=400;
    int tot=160000;
    a[0][0]=160000;
    x=0,y=0;
    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;
        }
    }
    for(int i=0; i<400; i++)

for(int j=0; j<400; j++)
        {
            if(prime[a[i][j]]==1)
                b[i][j]=1;
            else
                b[i][j]=0;
        }
}
int main()
{
    int t1,t2;
    int ans=0;
  make1();
    while(scanf("%d%d",&t1,&t2)!=EOF)
    {

int next[4][2]= {0,1,1,0,0,-1,-1,0};
        memset(book,0,sizeof(book));
        if(t1==t2)
            printf("Case %d: 0\n",++ans);
        else
        {
            int startx,starty,endx,endy;
            for(int i=0; i<=399; i++)
                for(int j=0; j<=399; j++)
                {
                    if(a[i][j]==t1)
                    {
                        startx=i;
                        starty=j;
                    }
                    if(a[i][j]==t2)
                    {
                        endx=i;
                        endy=j;
                    }
                }
            int head=1,tail=1;
            que[head].x=startx;
            que[head].y=starty;
            tail++;
            book[startx][starty]=1;
            int flag=0;
            while(head<tail)
            {
                for(int k=0; k<4; k++)
                {
                    int tx=que[head].x+next[k][0];
                    int ty=que[head].y+next[k][1];
                    if(tx<0||tx>399||ty<0||ty>399)
                        continue;
                    if(b[tx][ty]==0&&book[tx][ty]==0)
                    {
                        book[tx][ty]=1;
                        que[tail].x=tx;
                        que[tail].y=ty;
                        que[tail].s=que[head].s+1;
                        tail++;
                    }
                    if(tx==endx&&ty==endy)
                    {
                        flag=1;
                        break;
                    }
                }
                if(flag==1)
                    break;
                head++;
            }
            if(flag==1)
                printf("Case %d: %d\n",++ans,que[tail-1].s);
            else
                printf("Case %d: impossible\n",++ans);
        }
    }
    return 0;

}

 
 
 
 
 

hdu4255筛素数+广搜的更多相关文章

  1. 双向广搜 POJ 3126 Prime Path

      POJ 3126  Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16204   Accepted ...

  2. nyoj 592 spiral grid(广搜)

    题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=592 解决以下问题后就方便用广搜解: 1.将数字坐标化,10000坐标为(0,0),这样就 ...

  3. poj3126 Prime Path 广搜bfs

    题目: The ministers of the cabinet were quite upset by the message from the Chief of Security stating ...

  4. [Luogu]A%BProblem——线性筛素数与前缀和

    题目描述 题目背景 题目名称是吸引你点进来的[你怎么知道的] 实际上该题还是很水的[有种不祥的预感..] 题目描述 区间质数个数 输入输出格式 输入格式: 一行两个整数 询问次数n,范围m接下来n行, ...

  5. 广搜 poj3278 poj1426 poj3126

    Catch That Cow Time Limit: 2000 MS Memory Limit: 65536 KB 64-bit integer IO format: %I64d , %I64u Ja ...

  6. HDU--杭电--1195--Open the Lock--深搜--都用双向广搜,弱爆了,看题了没?语文没过关吧?暴力深搜难道我会害羞?

    这个题我看了,都是推荐的神马双向广搜,难道这个深搜你们都木有发现?还是特意留个机会给我装逼? Open the Lock Time Limit: 2000/1000 MS (Java/Others)  ...

  7. HDU 5652(二分+广搜)

    题目链接:http://acm.hust.edu.cn/vjudge/contest/128683#problem/E 题目大意:给定一只含有0和1的地图,0代表可以走的格子,1代表不能走的格 子.之 ...

  8. 【板子】gcd、exgcd、乘法逆元、快速幂、快速乘、筛素数、快速求逆元、组合数

    1.gcd int gcd(int a,int b){ return b?gcd(b,a%b):a; } 2.扩展gcd )extend great common divisor ll exgcd(l ...

  9. CF449C Jzzhu and Apples (筛素数 数论?

    Codeforces Round #257 (Div. 1) C Codeforces Round #257 (Div. 1) E CF450E C. Jzzhu and Apples time li ...

随机推荐

  1. [AaronYang]C#人爱学不学[5]

    这世上有三样东西是别人抢不走的:一是吃进胃里的食物,二是藏在心中的梦想,三是读进大脑的书 --Aaronyang的博客(www.ayjs.net) 1. 数组-的疑惑? 1.1  多维数组      ...

  2. AaronYang的C#私房菜[二][提供编程效率的技巧]

    前言 我的文章简单易懂,能学到东西.因为复杂的东西,讲起来,好累.阅读者只是膜拜,学不到东西,就是没用的东西,好多文章都是看不下去.我写不出来<大话设计模式>那种为了读者的书,因为没有时间 ...

  3. oracle-5-的升级步骤

    升级数据库的步骤 1.决定升级到那个版本 2.确定最近的数据已经备份(非常的重要) 3.安装软件升级包 4.升级方式启动数据库 5.执行必要的脚本 6.升级后的检查

  4. Java 并发编程 Executor

    Executor框架是指java 5中引入的一系列并发库中与executor相关的一些功能类,其中包括线程池,Executor,Executors,ExecutorService,Completion ...

  5. jdownload的使用

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  6. 换了XCode版本之后,iOS应用启动时不占满全屏,上下有黑边

    原因是没有Retina4对应的启动图片,解决方法很简单,就是把Retina4对应的图片给补上就只可以了

  7. <jsp:invoke fragment=""/>的理解和使用

    在传统 JSP 中,想要实现页面布局管理比较麻烦,为了解决在 JSP 中布局的问题,出现了很多开源软件,比如 Apache Tiles 和 SiteMesh 就是其中比较优秀的.但是使用开源软件实现布 ...

  8. iOS开源项目汇总

    扫描wifi信息: http://code.google.com/p/uwecaugmentedrealityproject/ http://code.google.com/p/iphone-wire ...

  9. iOS动画中的枚举UIViewAnimationOptions

    若本帖转出“博客园”请注明出处(博客园·小八究):http://www.cnblogs.com/xiaobajiu/p/4084747.html 笔记 首先这个枚举属于UIViewAnimation. ...

  10. 安装python爬虫scrapy踩过的那些坑和编程外的思考

    这些天应朋友的要求抓取某个论坛帖子的信息,网上搜索了一下开源的爬虫资料,看了许多对于开源爬虫的比较发现开源爬虫scrapy比较好用.但是以前一直用的java和php,对python不熟悉,于是花一天时 ...