题目链接: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. ASP.NET MVC 例子演示如何在 Knockout JS 的配合下,使用 TypeScript 。

    一个简单的 ASP.NET MVC 例子演示如何在 Knockout JS 的配合下,使用 TypeScript . 前言 TypeScript 是一种由微软开发的自由和开源的编程语言.它是JavaS ...

  2. Gitlab,Github与Bitbucket

    这段时间开始做毕设,决定使用git来管理代码和相关的文档. 同时希望有一个远程托管,决定在github.bitbucket,以及我自己搭建的gitlab服务器中间选一个,最终决定使用bitbuckt. ...

  3. windows下搭建Cygwin环境

    windows下搭建Cygwin环境 在上一篇博文<Android学习——windows下搭建NDK_r9环境>中,我们详细的讲解了在windows下进行Android NDK开发环境的配 ...

  4. MVC常见的控制器,接口,数据层之间的操作

    user_books_info 类 namespace CiWong.LearningLevel.Mapping { public class user_books_info { /// <su ...

  5. C# 添加敏感词

    public class CheckStreamReader { //使用的数据: private static HashSet<string> hash = new HashSet< ...

  6. 简单实现android和wp聊天

    使用Beetle.NetPackage简单实现android和wp聊天 Beetle.NetPackage是一个多台平开源Client TCP通讯组件,它针对不同平台提供统一的消息描述规则和使用规范可 ...

  7. ngx-push-stream模块源码学习(四)——订阅

    一.概述 push stream模块允许三种模式的订阅者: longpolling:每收到服务端响应数据即断开连接然后迅速重连,连接耗时可以忽略 stream:与服务端保持长连接,持续不断的请求-&g ...

  8. RILC

    RILC RIL层的作用大体上就是将上层的命令转换成相应的AT指令,控制modem工作.生产modem的厂家有很多:Qualcomm, STE, Infineon... 不同的厂家都有各自的特点,当然 ...

  9. hdu 1166 敌兵布阵(线段树基础题)

    学习线段树~~~~~~~~~~~~要好好理解 此题是单点更新的线段树,考虑基本的询问,更新. #include <iostream> #include <algorithm> ...

  10. Android 经验: 5555 端口会被 adb 误认为 emulator

    在本机启动 Android, 再用本机的的 adb 去连接 adb connect 127.0.0.1:5555 而后 adb devices 查看 user@ubuntu:~$ adb device ...