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. C#的LINQ to Object

    using System; using System.Collections; using System.Collections.Generic; using System.IO; using Sys ...

  2. Newtonsoft.Json(Json.Net)学习笔记(转)

    概述 Newtonsoft.Json,一款.NET中开源的Json序列化和反序列化类库,通过Nuget获取.(查看原文) 下面是Json序列化和反序列化的简单封装: /// <summary&g ...

  3. 【20140113】package 与 import

    一个完整的java源程序应该包括下列部分: package语句: //该部分至多只有一句,必须放在源程序的第一句 import语句: public classDefinition: //公共类定义部分 ...

  4. Win7下的内置FTP组件的设置详解

    在局域网中共享文件,FTP是比较方便的方案之一.Win7内部集成了FTP,只是设置起来颇费一番功夫.着文以记之. 一.安装FTP组件 由于Win7默认没有安装FTP组件.故FTP的设置第一步就是安装F ...

  5. Oracle数据库 控制文件

    一.概念控制文件的主要任务是管理数据库的状态以及描述数据库的物理结构 二.所含有的信息1.数据库名2.数据库标识符(DBID)3.数据库创建时间戳4.数据库字符集5.数据文件信息6.临时文件信息7.在 ...

  6. 跟着鸟哥学Linux系列笔记0-如何解决问题

    跟着鸟哥学Linux系列笔记0-扫盲之概念 在发生问题怎么处理: 1.  在自己的主机.网络数据库上查询How-To或FAQ -Linux 自身的文件数据: /usr/share/doc -CLDP中 ...

  7. 通过Small Basic把儿子/女儿带入编程的世界

    (此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 题记:今天是儿子3岁的生日,就来介绍一下适合给儿童培养兴趣的编程语言--微软Small Ba ...

  8. 配置ogg异构oracle-mysql(1)基础环境配置

    一.环境描述: 192.168.0.164 ( Oracle ) —> 192.168.0.165 (Mysql ) 版本: 操作系统:redhat5.8 Oracle:  11.2.0.3 M ...

  9. 第一篇:SOUI是什么?

    概述 用C++做产品最痛苦的是什么?肯定是做UI. SOUI的使命就是把痛苦的UI变化成快乐的UI. 什么?UI还能快乐?脑子进水了吗? 当你看完这个系统教程的时候相信你面对UI至少不会再痛苦.你可以 ...

  10. POJ 1625 Censored!(AC自动机+DP+高精度)

    Censored! Time Limit: 5000MS   Memory Limit: 10000K Total Submissions: 6956   Accepted: 1887 Descrip ...