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. JQ 全选设定与设置选中

    复选数据框 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w ...

  2. mysql TIME_WAIT

    http://www.th7.cn/system/lin/201404/53762.shtml http://kerry.blog.51cto.com/172631/105233/ http://ww ...

  3. 与你相遇好幸运,Tippecanoe在Centos下の安装

    全新的CentOS 7 x86_64 安装编译工具 yum install -y gcc automake autoconf libtool make yum insyall -y gcc gcc-c ...

  4. 【131202】SQL

    SELECT TOP 2 * FROM Persons SELECT *FROM PersonsWHERE ROWNUM <= 5   SELECT * FROM Persons WHERE C ...

  5. supervisor使用

    supervisor是一个C/S系统,它可以在类unix操作系统让用户来监视和控制后台服务进程的数量,一个很重要的功能就是监控服务器的主要后台进程,并在出现问题是自动重启. 根据服务器上的python ...

  6. php同步mysql两个数据库中表的数据

    分别创建两个数据库和两张表study库-zone表teaching库-area表 //****SQL脚本****// 1.创建teaching数据库area数据表 create database te ...

  7. STUN和TURN技术浅析

    转自:http://blog.csdn.net/yu_xiang/article/details/9227023 在现实Internet网络环境中,大多数计算机主机都位于防火墙或NAT之后,只有少部分 ...

  8. csc.rsp Invent by Microshaoft

    # This file contains command-line options that the C# # command line compiler (CSC) will process as ...

  9. canvas加载进度条

    <!DOCTYPE html> <html><head><meta http-equiv="Content-Type" content=& ...

  10. js获取今天明天

    目的:记录中展现"今天","明天",除外展现月日. 借鉴: <html> <head> <meta http-equiv=&quo ...