hdu 4255 #BFS#筛素数#环形数
来源:互联网 发布:2017网络热词 编辑:程序博客网 时间:2024/06/11 14:40
A Famous Grid
Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 183 Accepted Submission(s): 66
Problem Description
Mr. B 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. 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.
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. 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.
Input
Each test case is described by a line of input containing two nonprime integer 1 <=x, y<=10,000.
Output
For each test case, display its case number followed by the length of the shortest path or "impossible" (without quotes) in one line.
Sample Input
1 49 3210 12
Sample Output
Case 1: 1Case 2: 7Case 3: impossible
Source
Fudan Local Programming Contest 2012
///BFS;notice-- the 2D_array must larger than 100*100#include <stdio.h>#include <string.h>#include <queue>using namespace std;struct Point{ int x,y,step; Point(int a = 0,int b = 0,int c = 0){step = c;x = a;y = b;}};const int N = 200,NN = N * N;int mat[N][N];int i,j,k,n = NN;int pri[NN] = {1,1,0};int vis[N][N] ;int move[][2] = {0,1,1,0,0,-1,-1,0};int ok(int x,int y){ return x >= 0&& y>= 0 && x < N && y < N && pri[mat[x][y]] ;}int bfs(int x,int y){ if(x == y) return 0; int i,j; memset(vis,0,sizeof(vis)); Point p,dst,pt; for(i = 0; i < N; ++i) for(j = 0; j < N; ++j) { if(mat[i][j] == x) { p = Point(i,j,0); vis[i][j] = 1; } } queue<Point> que; que.push(p); while(!que.empty()) { p = que.front(); que.pop(); for(i = 0; i < 4; ++i) { pt.x = p.x + move[i][0]; pt.y = p.y + move[i][1]; if(ok(pt.x,pt.y) && !vis[pt.x][pt.y]) { vis[pt.x][pt.y] = 1; pt.step = p.step + 1; if(mat[pt.x][pt.y] == y) return pt.step; que.push(pt); } } } return -1;}int main(){ int t = N/2; for(k = 0; k < t; ++k) { for(i = k,j = i; j < N - k; ++j) mat[i][j] = n--; for(--j,++i; i < N - k; ++i ) mat[i][j] = n--; for(--i,--j; j >= k; --j) mat[i][j] = n--; for(++j,--i; i > k; --i) mat[i][j] = n--; } for(i = 2; i < NN; ++i) if(!pri[i]) for(j = i<<1; j < NN; j += i) pri[j] = 1; int cas = 0,x,y; while(scanf("%d%d",&x,&y) == 2) { int dis = bfs(x,y); if(dis == -1) printf("Case %d: impossible\n",++cas); else printf("Case %d: %d\n",++cas,dis); } return 0;}
- hdu 4255 #BFS#筛素数#环形数
- hdu 4255 A Famous Grid--模拟--筛素数--bfs
- HDU 1973 BFS+素数打表
- HDU 1431 素数回文数
- HDU 4255(蛇形填数+BFS+优先队列)
- POJ 3126 - 筛素数+BFS
- NYOJ592-spiral grid(蛇形填数+筛法素数+BFS)
- hdu 1372 bfs最少棋子步数
- hdu 1728 bfs 最小拐弯数
- hdu 1728 bfs 变形最小转弯数
- BFS hdu 1728 逃离迷宫 转弯数
- POJ 3126 && HDU 1973 Prime Path (素数表 + BFS)
- HDU 筛素数
- Prime Path(素数筛+BFS)
- [数论] HDU 1431 素数回文 筛素数
- HDU 4548 美素数 // 素数筛法
- 数素数
- 数素数
- NSdata 与 NSString,Byte数组,UIImage 的相互转换
- Servlet 之Cookie、Session
- 字符串题目
- JVM内部实现机制 转载:赛迪网
- JSP九个内置对象
- hdu 4255 #BFS#筛素数#环形数
- 拷贝构造函数 转载 百度百科
- 火狐插件火狐黑客插件将Firefox变成黑客工具的七个插件
- JSP指令标签
- 深拷贝 浅拷贝 转载 csdn
- [java]变量
- Dalvik和标准Java虚拟机的区别
- Thinkphp2.2 config.inc.php常用配置
- opengl 关于glGenBuffers函数没有定义