题目链接: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. wcf的binding和host

    ----------------------------Binding绑定:定义:绑定表示通讯信道的配置:定义了客户端与服务端之间的协议:---传输协议:http.tcp.命名管道.msmq,自定义( ...

  2. PreparedStatemnet预编译操作数据库的增删改

    /**************PreparedStatemnet使用******************/ /*特点:相当于ADO.NET中的参数化命令对象  * 1.预编译,执行相同的sql语句,提 ...

  3. SpringMVC源码

    SpringMVC源码分析系列 说到java的mvc框架,struts2和springmvc想必大家都知道,struts2的设计基本上完全脱离了Servlet容器,而springmvc是依托着Serv ...

  4. web 富文本编辑器总结

    前言 富文本编辑器,就是除了能输入不同的文本之外,还可以之间粘贴图画等其他的多媒体信息.也可说是所见即所得的编辑器. 目前可以使用的编辑器有很多, 在网络上有找到这样一份比较表格: 编辑器 产地 稳定 ...

  5. 2 MySQL安装

    目录: 1. mysql安装简介2. 下载AppServ3. 安装AppServ4. 使用phpmyadmin连接MySQL5. 使用MySQL Command Line Client 连接MySQL ...

  6. 链表c语言实现

    链表(c语言实现)--------------小练习   #include <stdio.h> #include <stdlib.h> #include <string. ...

  7. 安装xdebug后var_dump输出没有格式化的问题

    在ubuntu10.04下面配置Apache2.2 + PHP5.3的开发环境,顺便装了一个xdebug扩展方便调试代码. 但是环境配置好了之后却发现xdebug加载成功了但是var_dump输出的内 ...

  8. 通过google的inurl:backupdata*dede_admin获取账号密码

    简要描述:很简单,通过google可找出备份路径. 详细说明:http://www.google.com.hk/search?q=inurl:backupdata*dede_admin&hl= ...

  9. Lak3 Counting(POJ No.2386)

    问题描述: 有个大小为N*M的园子,雨后积起了水.八连通的积水被认为是连接在一起的.求出园子里总共有多少水洼. N, M <= 100 输入例: : 问题分析: 八连通即:上.左上.左,左下,下 ...

  10. GCC/G++ 学习笔记

    本文是<An introduction to GCC>的学习笔记,记录使用GCC/G++主要的实用技巧,本文讲述的知识基本上摘自本书,附带自己的一些体验.如果想详细查看本书,请戳这里. 一 ...