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>
using namespace std;
#define N 10000

bool vis[101][101];
bool prime[101][101];
bool tprime[N];
int dir[4][2] = {0,1, 1,0, -1,0, 0,-1}; 

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

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

void init()
{
    int n = N;
    int start=1, end = (int)sqrt(N+0.5), i;
    while(n > 0)
    {
        for(i=start; i<=end; i++){
            c[n].x = start;
            c[n--].y = i;
        }
        for(i=start+1; 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;
        }
        for(i=end-1; 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[1].x][c[1].y] = false;
    int m = sqrt(N+0.5);
    for(int i=2; 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 = 0;
    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;
        for(int i=0; i<4; i++){
            int tx = cur.x+dir[i][0];
            int ty = cur.y+dir[i][1];
            if(tx<1 || ty<1 || 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+1;
            que.push(next);
        }
    }
    return -1;
}

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

    return 0;
}
时间: 2024-10-14 11:38:22

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

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

Nightmare 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描述 Ignatius had a nightmare last night. He found himself in a labyrinth with a time bomb on him. The labyrinth has an exit, Ignatius should get out of the labyrinth before the bomb explodes. The initial e

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

nyoj 523 双向广搜

题目链接: http://acm.nyist.net/JudgeOnline/problem.php?pid=523 #include<iostream> #include<cstdio> #include<queue> using namespace std; /* 用普通搜索TLE,已知起点和终点,可以考虑双向广搜或A*算法加速搜索 双向广搜,一个方向从出发点向终点搜索,一个方向从终点向出发点搜索,搜索到相同的结点时,即找到最短路径. */ const int N

深搜 ,广搜,队列 nyoj 27 水池数目

水池数目 时间限制:3000 ms  |  内存限制:65535 KB 难度:4 描述 南阳理工学院校园里有一些小河和一些湖泊,现在,我们把它们通一看成水池,假设有一张我们学校的某处的地图,这个地图上仅标识了此处是否是水池,现在,你的任务来了,请用计算机算出该地图中共有几个水池. 输入 第一行输入一个整数N,表示共有N组测试数据 每一组数据都是先输入该地图的行数m(0<m<100)与列数n(0<n<100),然后,输入接下来的m行每行输入n个数,表示此处有水还是没水(1表示此处是水

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

师傅又被妖怪抓走了 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描述 话说唐僧复得了孙行者,师徒们一心同体,共诣西方.自宝象国救了公主,承君臣送出城西,沿路饥餐渴饮,悟空便为师傅去化斋,等悟空回来,悟净慌慌张张的对悟空说:“不好了,不好了”,还没等悟净说完,悟空说:“师傅又被妖怪抓走了”,悟净:“NO!” ,悟空一脸茫然,悟净:“师傅和二师兄都被妖怪抓走了”.悟空(晕!).为了防止悟空救人,妖怪先把唐憎和八戒分别藏起来,如果悟空在T分钟之后还没找到人,那必定是被妖怪吃

杭电 1242 Rescue(广搜)

http://acm.hdu.edu.cn/showproblem.php?pid=1242 Rescue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 15597    Accepted Submission(s): 5663 Problem Description Angel was caught by the MOLIGPY!

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

Rescue Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submission(s) : 14   Accepted Submission(s) : 7 Font: Times New Roman | Verdana | Georgia Font Size: ← → Problem Description Angel was caught by the MOLIGPY

ACdream 1191(广搜)

题目链接:http://acdream.info/problem?pid=1191 Dragon Maze Time Limit: 2000/1000MS (Java/Others)Memory Limit: 128000/64000KB (Java/Others) SubmitStatisticNext Problem Problem Description You are the prince of Dragon Kingdom and your kingdom is in danger o

F广搜

<span style="color:#330099;">/* F - 广搜 基础 Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Submit Status Description Technicians in a pathology lab analyze digitized images of slides. Objects on a slide are selected