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. ubuntu 下使用 putty 调试

    转自:http://blog.csdn.net/wh_19910525/article/details/39313457 Ubuntu的机子上,插上USB2COM线,准备开工. 检查USB2COM在本 ...

  2. 关于 redis、memcache、mongoDB 的对比(转载)

    from:http://yang.u85.us/memcache_redis_mongodb.pdf 从以下几个维度,对 redis.memcache.mongoDB 做了对比.1.性能都比较高,性能 ...

  3. 理解理解python中的'*','*args','**','**kwargs'

    http://blog.csdn.net/callinglove/article/details/45483097 讲了一大堆, 我也是用来理解类继承当中的参数行为的. =============== ...

  4. 讲解JS的promise,这篇是专业认真的!

    http://www.zhangxinxu.com/wordpress/2014/02/es6-javascript-promise-%E6%84%9F%E6%80%A7%E8%AE%A4%E7%9F ...

  5. [LeetCode] Ugly Number

    Ugly Number Total Accepted: 20760 Total Submissions: 63208 Difficulty: Easy Write a program to check ...

  6. Build Instructions (Windows) – The Chromium Projects

    转自:http://121.199.54.6/wordpress/?p=1156 原始地址:http://www.chromium.org/developers/how-tos/build-instr ...

  7. 11g添加asm

    1.创建组 2.创建grid用户 3.用grid安装Gride Infrastructure软件 4.执行root.sh[root@ora11g softdb]# /u01/app/11.2.0/gr ...

  8. 基于socket、多线程的客户端服务器端聊天程序

    服务器端: using System; using System.Windows.Forms; using System.Net.Sockets; using System.Net;//IPAddre ...

  9. [荐]使用jQuery清空file文件域

    file是文本域,我们一般都会使用它来上传文件,在上传文件时我们需要验证,验证完成后,如果存在错误,为了防止将错误信息也上传上去,我们总是希望能够将其清空.但是在IE中,为了安全起见它是不允许我们改变 ...

  10. 同一个项目,项目名称不一致,这两个项目同时在Eclipse中出现

    在Eclispse中,实际同一个项目,项目名称不一致,这两个项目同时在Eclipse中出现. ①打开项目文件夹,找到“.cproject”文件 ② 在<name>节点重命名 ③ 导入Ecl ...