A Famous Grid

Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1496    Accepted Submission(s): 567

Problem Description
Mr. B has recently discovered the grid named "spiral grid".
Construct the grid like the following figure. (The grid is actually infinite. The figure is only a small part of it.)

Considering
traveling in it, you are free to any cell containing a composite number
or 1, but traveling to any cell containing a prime number is
disallowed. You can travel up, down, left or right, but not diagonally.
Write a program to find the length of the shortest path between pairs of
nonprime numbers, or report it's impossible.

 
Input
Each test case is described by a line of input containing two nonprime integer 1 <=x, y<=10,000.
 
Output
For
each test case, display its case number followed by the length of the
shortest path or "impossible" (without quotes) in one line.
 
Sample Input
1 4
9 32
10 12
 
Sample Output
Case 1: 1
Case 2: 7
Case 3: impossible
 
Source
/**
题意:给出两个数,问两点之间的最短距离
做法:蛇形矩阵 + bfs + 优先队列
**/
#include <iostream>
#include <stdio.h>
#include <cmath>
#include <algorithm>
#include <string.h>
#include <queue>
#define maxn 40000
using namespace std;
int mmap[][];
int a[][];
int vis[][];
int dx[] = {,,-,};
int dy[] = {,-,,};
int n,m;
bool num[maxn];
void is_prime()
{
int tot = ;
memset(num,false,sizeof(num));
num[] = true;
for(long long i=; i<maxn; i++)
{
if(!num[i])
{
for(long long j=i*i; j<=maxn; j+=i)
{
num[j] = true;
}
}
}
}
struct Node
{
int x;
int y;
int step;
Node() {}
Node(int _x,int _y,int _step)
{
x = ;
y = ;
step =;
}
} start,endd;
struct cmp
{
bool operator () (const Node &a,const Node &b)
{
return a.step>b.step;
}
};
int check(int x,int y)
{
if(x>= && x < && y >= && y < && num[a[x][y]] == true&& !vis[x][y]) return ;
return ;
}
priority_queue<Node,vector<Node>,cmp >que;
bool bfs()
{
memset(vis,,sizeof(vis));
Node tmp,now;
while(!que.empty()) que.pop();
que.push(start);
vis[start.x][start.y] = ;
start.step = ;
while(!que.empty())
{
now = que.top();
que.pop();
//cout<<now.x<<" "<<now.y<<" "<<now.step<<endl;
if(now.x == endd.x && now.y == endd.y)
{
endd.step = now.step;
return true;
}
for(int i=; i<; i++)
{
tmp.x = now.x + dx[i];
tmp.y = now.y + dy[i];
tmp.step = now.step + ;
if(check(tmp.x,tmp.y))
{
vis[tmp.x][tmp.y] = ;
que.push(tmp);
}
}
}
return false;
}
void init()
{
int x = ;
int y = ;
int nn = ;
int num=a[][]=;
while(num>)
{
while((y+)<nn&&!a[x][y+]) a[x][++y]= --num;
while((x+)<nn&&!a[x+][y]) a[++x][y]= --num;
while((y-)>=&&!a[x][y-]) a[x][--y]= --num;
while((x-)>=&&!a[x-][y]) a[--x][y]= --num; }
}
int main()
{
//freopen("in.txt","r",stdin);
init();
is_prime();
int Case = ;
while(~scanf("%d %d",&n,&m))
{
for(int i=; i<; i++)
{
for(int j=; j<; j++)
{
if(a[i][j] == n)
{
start.x = i;
start.y = j;
}
if(a[i][j] == m)
{
endd.x= i;
endd.y = j;
}
}
}
bool prime = false;
prime = bfs();
printf("Case %d: ",Case++);
if(prime) printf("%d\n",endd.step);
else printf("impossible\n");
}
return ;
}
 

HDU-4255的更多相关文章

  1. hdu 4255 A Famous Grid

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4255 A Famous Grid Description Mr. B has recently dis ...

  2. HDOJ 2111. Saving HDU 贪心 结构体排序

    Saving HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  3. 【HDU 3037】Saving Beans Lucas定理模板

    http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...

  4. hdu 4859 海岸线 Bestcoder Round 1

    http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...

  5. HDU 4569 Special equations(取模)

    Special equations Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

  6. HDU 4006The kth great number(K大数 +小顶堆)

    The kth great number Time Limit:1000MS     Memory Limit:65768KB     64bit IO Format:%I64d & %I64 ...

  7. HDU 1796How many integers can you find(容斥原理)

    How many integers can you find Time Limit:5000MS     Memory Limit:32768KB     64bit IO Format:%I64d ...

  8. hdu 4481 Time travel(高斯求期望)(转)

    (转)http://blog.csdn.net/u013081425/article/details/39240021 http://acm.hdu.edu.cn/showproblem.php?pi ...

  9. HDU 3791二叉搜索树解题(解题报告)

    1.题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=3791 2.参考解题 http://blog.csdn.net/u013447865/articl ...

  10. hdu 4329

    problem:http://acm.hdu.edu.cn/showproblem.php?pid=4329 题意:模拟  a.     p(r)=   R'/i   rel(r)=(1||0)  R ...

随机推荐

  1. UVA.11300 Spreading the Wealth (思维题 中位数模型)

    UVA.11300 Spreading the Wealth (思维题) 题意分析 现给出n个人,每个人手中有a[i]个数的金币,每个人能给其左右相邻的人金币,现在要求你安排传递金币的方案,使得每个人 ...

  2. JavaScript滚动条的制作

    效果演示   这个效果的制作是借助setTimeout的第三个参数.setTimeout/setInterval,这两个函数相信前端开发同学都很熟悉.它们在非IE(6-9)浏览器中还可以如下使用: v ...

  3. ExtJs在页面上window再调用Window的事件处理

    今天在开发Ext的过程中遇到了一个恶心的问题,就是在ext.window页面,点击再次弹出window时,gridpanel中的store数据加载异常,不能正常被加载,会出现缓存,出现该问题,是因为w ...

  4. ACE反应器(Reactor)模式(4)

    转载于:http://www.cnblogs.com/TianFang/archive/2006/12/18/596012.html 定时器的实现 通过Reactor机制,还可以很容易的实现定时器的功 ...

  5. HDU 5640

    King's Cake Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

  6. Inner join case when

    SELECT ( ), wn.ActualWorkflowNumber) + ' ' + wi.SN ) AS SN , wi.RecordID , wi.WorkflowName , wc.Work ...

  7. HUST 1103 校赛 邻接表-拓扑排序

    Description N students were invited to attend a party, every student has some friends, only if someo ...

  8. javascript「篱式」条件判断

    我们已经知道,null 没有任何的属性值,并且无法获取其实体(existence)值.所以 null.property 返回的是错误(error)而不是 undefined . 考虑下面的代码 if ...

  9. 【NOIP】提高组2013 转圈游戏

    [算法]快速幂运算 [题解]ans=(m*10^k+x)%n,用快速幂计算10^k即可,复杂度为O(log k). #include<cstdio> long long n,m,k,x,a ...

  10. How to write educational schema.

    Sometimes, writing such educational schemas could be of much use, and creating such docs can be bene ...