ACM spiral grid
spiral grid
- 描述
- 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 注意起点可以是素数,蛇形填数+广度搜索
本题用到的技巧
(1)注意蛇形填数,为grid加了一个边框,边框的值为-1,这样生成数字时不用考虑是否越界,在广度搜索时也不用判断是否越界
(2)利用pair<int,int>充当point不用再定义数据结构
(3)在广度搜索时,要确定每层是否已经遍历完,每层遍历完后将Point(0,0)压入队列中作为层与层的分隔#include <iostream>
#include <algorithm>
#include <vector>
#include <utility>
#include <queue>
using namespace std;
typedef pair<int,int> Point;
int grid[][]={};
const int dx[] = {,,,-}; //右,下,左,上
const int dy[] = {,,-,};
void make_spiral_grid(){
for(int i = ;i < ; ++ i) grid[][i]=grid[i][]=grid[][i]=grid[i][]= -;
int number =*,x = ,y = ,step = ;
while(number){
step%=;
while(grid[x+dx[step]][y+dy[step]] == ){
x+=dx[step];y+=dy[step];
grid[x][y]=number--;
}
step++;
}
} bool isPrime(int n){
if(n < ) return true;
if(n == ) return false;
if(n% == ) return (n==);
if(n% == ) return (n==);
if(n% == ) return (n==);
for(int i = ; i*i <= n; i+= ){
if(n%i == ) return false;
}
return true;
} Point getPointFromGrid(int v){
for(int i = ; i <= ; i++){
for(int j = ; j <= ; j++){
if(grid[i][j] == v) return Point(i,j);
}
}
return Point(,);
} int bfs(int startV,int endV){
vector<vector<bool> > visit(,vector<bool>(,false));
Point startPoint = getPointFromGrid(startV);
queue<Point> points;
points.push(startPoint);
visit[startPoint.first][startPoint.second] = true;
points.push(Point(,));
int step = ;
while(!points.empty()){
Point a = points.front(); points.pop();
if(a.first == && a.second == ) {
if(points.empty()) break;
points.push(a);
step++;
continue;
}
for(int i = ; i < ; ++ i){
int newx = a.first + dx[i];
int newy = a.second + dy[i];
if(grid[newx][newy]==endV) return step;
if(!isPrime(grid[newx][newy]) && !visit[newx][newy]) {
points.push(Point(newx,newy));
visit[newx][newy] = true;
}
}
}
return ;
} int main(){
make_spiral_grid();
int icase = ,x,y;
while(cin >> x >> y){
if(isPrime(y)) cout<<"Case "<<++icase<<": impossible"<<endl;
else if(x == y ) cout<<"Case "<<++icase<<": 0"<<endl;
else{
int res = bfs(x,y);
if(res == ) cout<<"Case "<<++icase<<": impossible"<<endl;
else cout<<"Case "<<++icase<<": "<<res<<endl;
}
}
}
ACM spiral grid的更多相关文章
- nyoj592 spiral grid
spiral grid 时间限制:2000 ms | 内存限制:65535 KB 难度:4 描述 Xiaod has recently discovered the grid named &q ...
- nyoj 592 spiral grid(广搜)
题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=592 解决以下问题后就方便用广搜解: 1.将数字坐标化,10000坐标为(0,0),这样就 ...
- hdu 4255 A Famous Grid
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4255 A Famous Grid Description Mr. B has recently dis ...
- hdu4255筛素数+广搜
Mr. B has recently discovered the grid named "spiral grid".Construct the grid like the fol ...
- HDU-4255
A Famous Grid Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- [ACM] POJ 1942 Paths on a Grid (组合)
Paths on a Grid Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 21297 Accepted: 5212 ...
- zjuoj 3773 Paint the Grid
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3773 Paint the Grid Time Limit: 2 Secon ...
- zjuoj 3780 Paint the Grid Again
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3780 Paint the Grid Again Time Limit: 2 ...
- ZOJ 3781 Paint the Grid Reloaded(BFS)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3781 Leo has a grid with N rows an ...
随机推荐
- AIX性能监控
http://www.ibm.com/developerworks/cn/aix/library/au-aix7memoryoptimize2/ http://www.aixchina.net/Art ...
- SQLServer操作结果集
union组合结果集 --相同合并 union --全部显示 union all 公用表表达式 with CET( wName,dName) as ( select wName,dName from ...
- java中常用的工具类(二)
下面继续分享java中常用的一些工具类,希望给大家带来帮助! 1.FtpUtil Java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 ...
- Win10 兼容性 Visual studio web应用程序 ASP.NET 4.0 尚未在 Web 服务器上注册
系统升级到windows10 ,Visual studio 创建web应用程序时出现如下提示ASP.NET 4.0尚未在 Web 服务器上注册.为了使网站正确运行,可能需要手动将 Web 服务器配置为 ...
- WPF ListView展示层叠信息
通常我们在ListView中展示一列同类数据,例如城市名称.不过可以对ListView的DataTemplate稍作修改,让其显示层叠信息.例如:需要在ListView中显示省份和省份对应的城市名称. ...
- WPF 自定义Metro Style窗体
为了使WPF程序在不同版本的操作系统上保持一致的显示效果,我们需要重写WPF控件样式.这篇博客将展示如何创建一个Metro Style的WPF窗体. 首先先看一下最终窗体的效果图, 通过截图我们可以看 ...
- 用康托展开实现全排列(STL、itertools)
康拓展开: $X=a_n*(n-1)!+a_{n-1}*(n-2)!+\ldots +a_2*1!+a_1*0!$ X=an*(n-1)!+an-1*(n-2)!+...+ai*(i-1)!+...+ ...
- 跟着鸟哥学Linux系列笔记0-如何解决问题
跟着鸟哥学Linux系列笔记0-扫盲之概念 在发生问题怎么处理: 1. 在自己的主机.网络数据库上查询How-To或FAQ -Linux 自身的文件数据: /usr/share/doc -CLDP中 ...
- 退出Activity(转)
退出Activity 如何退出Activity?如何安全退出已调用多个Activity的Application? 退出activity 直接调用 finish () 方法 . //用户点击back键 ...
- 数字信号处理实验(六)——FIR滤波器的设计
一.四种线性相位FIR滤波器的振幅响应 1.自编函数 [Hr,w,a,L]=-n) [Hr,w,a,L]=-n) [Hr,w,a,L]=-n) [Hr,w,a,L]=-n) 2.一个demo clea ...