spiral grid

时间限制: 2000 ms  |  内存限制: 65535 KB
难度: 4
描述
Xiaod 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. In addition, traveling from a prime number is disallowed, either. 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.

输入
Each test case is described by a line of input containing two nonprime integer 1 <=x, y<=10,000.
输出
For each test case, display its case number followed by the length of the shortest path or "impossible" (without quotes) in one line.
样例输入
1 4
9 32
10 12
样例输出
Case 1: 1
Case 2: 7
Case 3: impossible
#include<iostream>
#include<queue>
#include<cstdio>
#include<cstring>
#include<cmath>
#define N 100
using namespace std;
int map[N][N];
bool vis[N][N];
int ex,ey,bx,by;
int maze[N][N];
void init(int *t,int n,int x,int y)
{if(n==0)return;int t1,t2,t3,t4;t1=t2=t3=t4=n-1;//循环n-1遍,打印n-1个数while(t1--)map[x][y++]=(*t)--;//右 while(t2--)map[x++][y]=(*t)--;//下 while(t3--)map[x][y--]=(*t)--;//左 while(t4--)map[x--][y]=(*t)--;//上 init(t,n-2,x+1,y+1);
}
int prime[10015];
struct node
{int x,y,step;node(int a,int b,int c):x(a),y(b),step(c){}node(){}
};
int dir[4][2]={{0,1},{1,0},{-1,0},{0,-1}};
int bfs()
{queue<node>q;q.push(node(bx,by,0));vis[bx][by]=true;while(!q.empty()){node cur=q.front();if(cur.x==ex&&cur.y==ey)return cur.step;q.pop();for(int i=0;i<4;i++){int xx=cur.x+dir[i][0];int yy=cur.y+dir[i][1];if(xx<0||xx>=N||yy<0||yy>=N||vis[xx][yy]||!prime[map[xx][yy]])continue;vis[xx][yy]=true;q.push(node(xx,yy,cur.step+1));}}return -1;
}
int main()
{int t=N*N;init(&t,N,0,0);int x,y;int cnt=0;memset(prime,0,sizeof(prime));prime[0]=prime[1]=1;for(int i=2;i<=10000;i++){if(prime[i])continue;for(int j=2*i;j<=10000;j+=i){prime[j]=1;}}while(~scanf("%d%d",&x,&y)){printf("Case %d: ",++cnt);if(prime[y]==0){printf("impossible\n");continue;}for(int i=0;i<N;i++){for(int j=0;j<N;j++){vis[i][j]=false;if(map[i][j]==x){bx=i;by=j;}if(map[i][j]==y){ex=i;ey=j;}}}if(bx==ex&&by==ey){printf("impossible\n");continue;}int res=bfs();if(res==-1){printf("impossible\n");continue;}else printf("%d\n",res);}return 0;
}  
终点不能是素数啊啊啊啊啊,运行了好一会,素数在开始的时候用筛法打表,主要是那个地图,学知识了,这两部弄完之后bfs,这一题挺不错的。
												

spiral grid相关推荐

  1. 13-19年复旦大学计算机学院研究生复试机试题

    (不保证代码完全正确,自己敲的可能存在不完善的地方,请各位大佬发现后指出,谢谢!) 暂时结束. 试题列表 2019上机题 2018上机题 2017上机题 2016上机题 2015上机题 2014上机题 ...

  2. 使用NVIDIA GRID vPC支持视频会议和算力工具

    使用NVIDIA GRID vPC支持视频会议和算力工具 随着2020年的发展,远程工作解决方案已成为许多人的新常态.企业正在寻找行之有效的解决方案,如虚拟桌面基础设施(VDI),以使他们的团队能够在 ...

  3. CSS grid 的用法

    grid 的用法 加三个宽度为 200px 的列. .container {display: grid;grid-template-columns: 200px 200px 200px; } 用 fr ...

  4. mvc4 ajax grid,mvc4中用上一种grid

    view 视图@modelIEnumerable@using(Html.Configurator("The grid should...").PostTo("FirstL ...

  5. datagrid底部显示水平滚动_DevExpress WPF v19.1:Data Grid/Tree List等控件功能增强

    行业领先的.NET界面控件DevExpress 日前正式发布v19.1版本,本站将以连载的形式介绍各版本新增内容.在本系列文章中将为大家介绍DevExpress WPF v19.1中新增的一些控件及部 ...

  6. CUDA中grid、block、thread、warp与SM、SP的关系

    首先概括一下这几个概念.其中SM(Streaming Multiprocessor)和SP(streaming Processor)是硬件层次的,其中一个SM可以包含多个SP.thread是一个线程, ...

  7. 设置echarts的grid、tooltip、柱状图渐变色、折线图渐变色

    grid: {show: false,left: '0px',top: '50px',right: '1px',bottom: '0px',containLabel: true,backgroundC ...

  8. 你真的了解Grid布局吗?

    Grid网格布局 概述:Grid将容器划分为一个个网格,通过任意组合不同的网格,做出你想想要的布局 Grid与flex布局相似,将整个Grid分为了容器与子项(格子) Grid容器的三个重要的概念: ...

  9. [转]Ext Grid控件的配置与方法

    http://www.blogjava.net/wangdetian168/archive/2011/04/12/348651.html 1.Ext.grid.GridPanel 主要配置项: sto ...

最新文章

  1. 20CSS中的高级技巧
  2. java继承 映射_hibernate继承关系映射和java反射机制的运用
  3. python字典的数据结构_Python数据结构之三——dict(字典)
  4. 两个不同分支之间的代码合并
  5. unzip命令 – 解压缩zip文件
  6. 体系结构笔记------动态调度中的Tomasulo算法
  7. Ubuntu新建用户后使用sudo报错:不在sudoers文件中,此事将被报告
  8. OpenStack安装流程(juno版)- 添加网络服务(neutron)- controller节点
  9. 数据结构——查找最全总结(期末复习必备)
  10. 罗克韦尔AB PLC ControlLogix PLC的介绍和选型
  11. mysql 8.0 初识
  12. Java项目员工信息管理系统
  13. 大数问题:大数加法 与 大数乘法 最简单大数乘法
  14. 魔力宝贝html5模板,魔力宝贝辅助脚本最新版
  15. SQL截取字符串(substring与patindex的使用)
  16. 嵌入式软件开发杂谈(4):Binutils工具集
  17. Android Studio下载gitLab项目
  18. MySQL中的真实案例:某网站有email字段,存储邮件地址,想调查163,126,qq,eyou邮箱的比例;
  19. 使用OkHttp3发起POST或GET请求
  20. Java动态编程之javassist

热门文章

  1. android屏幕共享demo,屏幕共享
  2. 居然之家联手分众巨额投入:引爆2亿目标人群 双11力争破百亿
  3. 超快CN2线路 UDOMAIN CDN使用体会
  4. 声呐数据集---处理
  5. 生鲜行业渠道商经销管理系统:加强生鲜渠道连接,提升销售转化
  6. VS调试技巧:让断点在for循环中i为某个值的时候停下来
  7. python复制出现错误_Python中的错误和异常
  8. 【ZYNQ】黑金教程_OV5640加LCD显示实验的Bug分享
  9. php接口内session,php4的session功能评述(一)
  10. 什么是MVVM模式?