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 ;
}
 

最新文章

  1. How to convert webp to png/jpg/gif in MacOS
  2. salesforce 零基础学习(三十八)Translate 的使用(国际化处理)
  3. javascript学习笔记(四):事件处理函数和动态创建html标记。
  4. Swift闭包概念与常见使用场景总结
  5. 导出程序界面(UI)到图片
  6. redis专题--slow log详解
  7. github创建tag
  8. web.confgi转换,web发布时发布配置(debug/release)生成不同的配置文件
  9. 四、Hbase
  10. Linux-进程描述(3)之进程状态僵尸进程与孤儿进程
  11. C#控制台程序使用Log4net日志组件
  12. sql中奇怪的sum(1),sum(2),count(1),count(6),count(*):统计总数
  13. Python合并多个Excel数据
  14. mysql 的远程链接字符
  15. SignalR 行实时通信最大连接数
  16. Beyond-Compare 4 -linux 破解
  17. gcc下inline的一个问题
  18. 2、Keepalived提供日志与双主模型演示
  19. python 类方法中参数使用默认值的方法
  20. python自学——函数-strftime

热门文章

  1. BZOJ1013:[JSOI2008]球形空间产生器——题解
  2. git安装和使用 linux系统和window系统
  3. bzoj3302&amp;bzoj2447&amp;bzoj2103(树的重心)
  4. 2017-7-19-每日博客-关于Linux下的CentOS中文件夹基本操作命令.doc
  5. How to configue session timeout in Hive
  6. 轮廓问题/Outline Problem
  7. ExtJS下拉列表使用方法(异步传输数据)
  8. CSS知识之 background-size 用法详细介绍
  9. 【转载】Java JVM : Xms Xmx PermSize MaxPermSize 区别
  10. free命令buff和cache的区别