hdu4255筛素数+广搜

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;

}

时间: 2024-11-06 10:29:30

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

双向广搜 POJ 3126 Prime Path

POJ 3126  Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16204   Accepted: 9153 Description The ministers of the cabinet were quite upset by the message from the Chief of Security stating that they would all have to change th

poj 3126 prime path 简单广搜

http://poj.org/problem?id=3126 题意:给你两个四位数a,b,从a开始    每次只能改变上一次数的其中一位,问至少需要几步才能得到b 分析:求最小路   典型的广搜   表面上是    40入口的bfs   但是除去有的数不是素数   入口数远小于40 可以写一个  判断一个数是否为素数的函数  , 每次去 调用 判断一个数是否要进队列 也可以 事先打一个素数表  这样会快点 注意:output  :either with a number stating the

POJ 3126 Prime Path( 广搜 )

Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12974   Accepted: 7342 Description The ministers of the cabinet were quite upset by the message from the Chief of Security stating that they would all have to change the four-dig

nyoj 592 spiral grid(广搜)

题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=592 解决以下问题后就方便用广搜解: 1.将数字坐标化,10000坐标为(0,0),这样就可以通过数字获得其坐标 2.通过一个坐标知道在这个位置上的数字是否为素数 #include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<queue> us

NYOJ 284 坦克大战 &amp;&amp; POJ 2312 Battle City (广搜+优先队列)

链接:click here~~ 题意: 描述 Many of us had played the game "Battle city" in our childhood, and some people (like me) even often play it on computer now. What we are discussing is a simple edition of this game. Given a map that consists of empty space

Catch That Cow(广搜)

个人心得:其实有关搜素或者地图啥的都可以用广搜,但要注意标志物不然会变得很复杂,想这题,忘记了标志,结果内存超时: 将每个动作扔入队列,但要注意如何更简便,更节省时间,空间 Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,000) on a number line and t

codevs 1225:八数码难题【双向广搜】

这里是传送门 这道题用普通BFS是可以做的,但是很明显没得过,效率太低了.效率更高的算法A*和双向广搜都可取,这写一下双向广搜的. 注意题目中的判重很重要,可以转化成九位数用hash来解决这个问题. #include <set> #include <string> #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define

迷宫广搜

上学期学了C,这学期学C++.感觉最难的还是算法,上周作业的一道广搜题是我第一次接触广搜,由于第一学期刚学编程就接触的太多的算法难题,不禁对代码产生畏惧,不过还好没有放弃,虽然算法很难,但我慢慢找到了一点学数学时的乐趣.先介绍一下这道未来的我看过来会觉得很简单一道题吧 You are provided a maze(迷宫), and you need to program to find the least steps to walk from the start to the end.And

宽搜和广搜、

广搜与深搜的小区别 一般来说,广搜常用于找单一的最短路线,或者是规模小的路径搜索,它的特点是"搜到就是最优解", 而深搜用于找多个解或者是"步数已知(好比3步就必需达到前提)"的标题,它的空间效率高,然则找到的不必定是最优解,必需记实并完成全数搜索,故一般情况下,深搜需要很是高效的剪枝(优化). 像搜索最短路径这些的很显著若是用广搜,因为广搜的特征就是一层一层往下搜的,保证当前搜到的都是最优解,当然,最短路径只是一方面的操作,像什么起码状态转换也是可以操作的.深搜就