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 注意起点可以是素数,蛇形填数+广度搜索
本题用到的技巧
(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的更多相关文章

  1. nyoj592 spiral grid

    spiral grid 时间限制:2000 ms  |  内存限制:65535 KB 难度:4   描述 Xiaod has recently discovered the grid named &q ...

  2. nyoj 592 spiral grid(广搜)

    题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=592 解决以下问题后就方便用广搜解: 1.将数字坐标化,10000坐标为(0,0),这样就 ...

  3. hdu 4255 A Famous Grid

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

  4. hdu4255筛素数+广搜

    Mr. B has recently discovered the grid named "spiral grid".Construct the grid like the fol ...

  5. HDU-4255

    A Famous Grid Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  6. [ACM] POJ 1942 Paths on a Grid (组合)

    Paths on a Grid Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 21297   Accepted: 5212 ...

  7. zjuoj 3773 Paint the Grid

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3773 Paint the Grid Time Limit: 2 Secon ...

  8. zjuoj 3780 Paint the Grid Again

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3780 Paint the Grid Again Time Limit: 2 ...

  9. 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 ...

随机推荐

  1. Q3 2016 State of the Internet – Security Report

    https://content.akamai.com/PG7476-Q3-2016-SOTI-Security-Report.html?utm_source=GoogleSearch&gcli ...

  2. 自动复制转换StringBuffer

    自动复制转换StringBuffer http://www.cnblogs.com/coqn/archive/2012/07/31/all_StringBuufer.html http://blog. ...

  3. Java集合源码学习(一)集合框架概览

    >>集合框架 Java集合框架包含了大部分Java开发中用到的数据结构,主要包括List列表.Set集合.Map映射.迭代器(Iterator.Enumeration).工具类(Array ...

  4. Pyqt 屏幕截图工具

    从Pyqt的examples中看到一段截图代码, (路径:examplas\desktop\screenshot.py) 所以想自己UI下界面,手动练习下 通过UI生成的: Screenshot.py ...

  5. test1.A[【dfs简单题】

    Test1.A Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 sdut 2274:http://acm.sdut.edu.cn/ ...

  6. penghui_031413 Bat命令学习

    penghui_031413   Bat命令学习 基础部分:====================================================================== ...

  7. .NET Framework 4 与 .NET Framework 4 Client Profile

    今天碰到的一个问题和Client Profile相关的.问题是这样的:一个WPF工程,需要引用另外几个.NET的assembly, 在WPF工程中添加了对这几个assembly的引用,并在程序中可以添 ...

  8. Centos 上使用Mono+MVC5+WebApi+Sqlite

    鉴于现在网上很多Mono安装Jexus的方法已经过时,你打开百度搜索基本是几个前辈写的文字,很多其实是过去式了.踩的坑多自然使人望而生畏,而方便快捷的方法百度排名却太低,这里就安利下笔者刚成功使用的方 ...

  9. mathematica练习程序(图像取反)

    代码很简单,就四行,我想到可以用mathematica干点什么了. 有人通过mathematica编程研究过视频编解码算法么,挺有意思,可以尝试一下. img=Import["f:/lena ...

  10. AIDL

    在介绍跨程序进程间通信AIDL前,先看一下本程序activity与某个服务是怎么绑定在一起进行交互的. 需求:服务有两个方法.分别是播放音乐与停止播放音乐.该程序的活动要访问这两个方法,在activi ...