题目链接: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. mac下安装配置java jdk,maven,resin

    mac jdk安装及环境变量配置 安装http://www.ifunmac.com/2013/04/mac-jdk-7/ jdk安装目录:/Library/Java/JavaVirtualMachin ...

  2. Eclipse编辑器样式修改

    很多的开发工具都可以更改主题样式,但eclipse作为一款影响力巨大的开源开发工具,却没有自带更改样式的功能,这多少令人有点小遗憾.Eclipse 4之后,Eclipse使用者呼声高涨,就有人开始做起 ...

  3. Linq无聊练习系列8---开放式并发事务,null和 DateTime,string操作练习

    /*********************开放式并发事务,null和 DateTime,string操作练习**********************************/ //所谓并发,就是 ...

  4. ASP.NET WEB API构建基于REST风格

    使用ASP.NET WEB API构建基于REST风格的服务实战系列教程[开篇] 最近发现web api很火,园内也有各种大神已经在研究,本人在asp.net官网上看到一个系列教程,原文地址:http ...

  5. JavaScript学习--(智能社视频)

    JavaScript学习,这是智能社官网上的JS视频,讲解的挺不错的,就是没有视频的demo,在视频中附上自己编写的一些demo... 下载地址:http://pan.baidu.com/s/1cPz ...

  6. 认识WinDbg

    WinDbg学习笔记(一)--认识WinDbg 一.前言    本人学习WinDbg已经有好几天了,虽说技术掌握的还不太熟练,不过也总算是入门了在学习WinDbg的过程中,觉得WinDbg真的比Oll ...

  7. MongoDB:利用官方驱动改装为EF代码风格的MongoDB.Repository框架 三

    本次改动的主要内容是实现MongoDB.Repository在MongoDB中建立索引. 建立索引主要使用MongoDB的官方驱动中EnsureIndex方法. 在MongoDB.Repository ...

  8. 使用数据流的方式 将数据导出到指定形式到Word中 (不使用插件,很简单的一种)

    protected void Page_Load(object sender, EventArgs e) { string fileName = "路政许可事项办理流转情况汇总统计" ...

  9. Object-c学习之路五(@protocol协议)

    今天模拟Button的delegate来联系一下protocol. Button类 // Button.h // Protocal // // Created by WildCat on 13-7-2 ...

  10. jQuery中delegate与on的用法与区别

    在jQuery1.7中 .delegate()已被.on()取代.对于早期版本,它仍然使用事件委托的最有效手段. 在事件绑定和委派,delegate()和on在一般情况下,这两种方法是等效的. .de ...