题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=592

解决以下问题后就方便用广搜解:

1、将数字坐标化,10000坐标为(0,0),这样就可以通过数字获得其坐标

2、通过一个坐标知道在这个位置上的数字是否为素数

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
using namespace std;
#define N 10000

][];
][];
bool tprime[N];
][] = {,, ,, -,, ,-}; 

struct Point {
    int x, y;
    bool isPrime;
}c[N+];

struct Node {
    int x, y, step;
    bool operator == (const Node &pra) const {
        return x == pra.x && y == pra.y;
    }
};

void init()
{
    int n = N;
    , end = (int)sqrt(N+0.5), i;
    )
    {
        for(i=start; i<=end; i++){
            c[n].x = start;
            c[n--].y = i;
        }
        ; i<end; i++){
            c[n].x = i;
            c[n--].y = end;
        }
        for(i=end; i>=start; i--){
            c[n].x = end;
            c[n--].y = i;
        }
        ; i>start; i--){
            c[n].x = i;
            c[n--].y = start;
        }
        start++; end--;
    }
}

void initPrime()
{
    memset(tprime, true, sizeof(tprime));
    memset(prime, true, sizeof(prime));
    prime[c[].x][c[].y] = false;
    int m = sqrt(N+0.5);
    ; i<=m; i++){
        if(tprime[i]){
            for(int j=i*i; j<=N; j+=i){
                tprime[j] = false;
                prime[c[j].x][c[j].y] = false;
            }
        }
    }
}

int bfs(int a, int b)
{
    memset(vis, false, sizeof(vis));
    Node start, target;
    start.x = c[a].x; start.y = c[a].y; start.step = ;
    target.x = c[b].x; target.y = c[b].y;
    queue<Node>que;
    que.push(start);
    vis[start.x][start.y] = true;
    while (!que.empty()) {
        Node cur = que.front();
        que.pop();
        if(cur == target)
            return cur.step;
        ; i<; i++){
            ];
            ];
             || ty< || tx>N || ty>N || prime[tx][ty] || vis[tx][ty]) continue;
            vis[tx][ty] = true;
            Node next; next.x = tx; next.y = ty; next.step = cur.step+;
            que.push(next);
        }
    }
    ;
}

int main()
{
    init();
    initPrime();
    ;
    while(cin>>a>>b){
        int res = bfs(a, b);
        cout<<"Case "<<(++cnt)<<": ";
        ){
            cout<<res<<endl;
        } else {
            cout<<"impossible"<<endl;
        }
    }

    ;
}

nyoj 592 spiral grid(广搜)的更多相关文章

  1. nyoj 613 免费馅饼 广搜

    免费馅饼 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述 都说天上不会掉馅饼,但有一天gameboy正走在回家的小径上,忽然天上掉下大把大把的馅饼.说来gameboy ...

  2. NYOJ 483 Nightmare 【广搜】+【无标记】

    版权声明:长风原创 https://blog.csdn.net/u012846486/article/details/31032479 Nightmare 时间限制:1000 ms  |  内存限制: ...

  3. NYOJ 284 坦克大战 (广搜)

    题目链接 描述 Many of us had played the game "Battle city" in our childhood, and some people (li ...

  4. nyoj 523 双向广搜

    题目链接: http://acm.nyist.net/JudgeOnline/problem.php?pid=523 #include<iostream> #include<cstd ...

  5. nyoj 999——师傅又被妖怪抓走了——————【双广搜】

    师傅又被妖怪抓走了 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述 话说唐僧复得了孙行者,师徒们一心同体,共诣西方.自宝象国救了公主,承君臣送出城西,沿路饥餐渴饮,悟 ...

  6. hdu 1242:Rescue(BFS广搜 + 优先队列)

    Rescue Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submis ...

  7. UVA 10047 The Monocycle (状态记录广搜)

    Problem A: The Monocycle  A monocycle is a cycle that runs on one wheel and the one we will be consi ...

  8. UVa 1600 Patrol Robot(三维广搜)

    A robot has to patrol around a rectangular area which is in a form of m x n grid (m rows and ncolumn ...

  9. (广搜)Fire Game -- FZU -- 2150

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82828#problem/I Fire Game Time Limit:1000MS    ...

随机推荐

  1. Oracle琐碎笔记2

    备注:以下所有操作均在sqlplus中执行. 开始前输入:spool c:\jiyi.txt;结束后输入:spool off;就会记忆操作的所有记录save c:\sql.sql;保存sql脚本可以使 ...

  2. 无法识别的配置节 applicationSettings

    在.NETFramework2.0中,如果发布的虚拟目录程序引用了外部的webservice访问接口,配置文件会出现下边的applicationSettings节点,如 但是访问应用程序时候,可能会出 ...

  3. Web Components

    Web Components是不是Web的未来   今天 ,Web 组件已经从本质上改变了HTML.初次接触时,它看起来像一个全新的技术.Web组件最初的目的是使开发人员拥有扩展浏览器标签的能力,可以 ...

  4. CSS3(transform/transition/animation) 基础 总结

    1.CSS3新增的样式(常用) //颜色透明表示rgba(0,0,0,.5) //圆角(定义角半径)border-radius: 5px 10px 15px 20px; //文字/盒子阴影text-s ...

  5. 在win下,如何用bat看程序运行的时间

    上网搜了下用bat记录程序运行时间的方法,结果连google跳出的都是些什么ctime啥的- - 一点都不靠谱 傍晚问了几个大神,也大多都是ctime党,不过还好明哲造![跪跪跪] 在此mark 就比 ...

  6. 关于sscanf函数的各种详细用法

    看书的时候碰到sscanf函数,就上网查了很多资料,并加以自己的整理,希望对大家有所帮助. (因为参考的博客太多太散,就不一一注明,望大神们见谅) sscanf()  :从一个字符串中读进与指定格式相 ...

  7. C语言实现进制转换——超图面试题

    递归:递归的原理,就是自己调用自己本身.存在一个顺序的问题,如果在递归前的是顺序执行,递归后的是逆序执行,如下: void gogogo() { //递归之前 gogogo(); //递归之后 } 递 ...

  8. Xcode5和ObjC新特性

    Welcome to Xcode 5 这是我的WWDC2013系列笔记中的一篇,完整的笔记列表请参看这篇总览.本文仅作为个人记录使用,也欢迎在许可协议范围内转载或使用,但是还烦请保留原文链接,谢谢您的 ...

  9. 项目Splash页面的开发与设计

    项目Splash页面的开发与设计 首先建立一个安卓的项目,然后修改manifest.xml文件,修改应用程序的logo和显示名称,效果图如下: 对应的代码如下: 1 <?xml version= ...

  10. 简明CSS属性:定位

    简明CSS属性:定位 第一话 定位 (Positioning) 关键词:position/z-index/top/bottom/right/left/clip POSITION 该属性用来决定元素在页 ...