A Famous Grid

Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1496    Accepted Submission(s): 567

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.

 
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 4
9 32
10 12
 
Sample Output
Case 1: 1
Case 2: 7
Case 3: impossible
 
Source
  1. /**
  2. 题意:给出两个数,问两点之间的最短距离
  3. 做法:蛇形矩阵 + bfs + 优先队列
  4. **/
  5. #include <iostream>
  6. #include <stdio.h>
  7. #include <cmath>
  8. #include <algorithm>
  9. #include <string.h>
  10. #include <queue>
  11. #define maxn 40000
  12. using namespace std;
  13. int mmap[][];
  14. int a[][];
  15. int vis[][];
  16. int dx[] = {,,-,};
  17. int dy[] = {,-,,};
  18. int n,m;
  19. bool num[maxn];
  20. void is_prime()
  21. {
  22. int tot = ;
  23. memset(num,false,sizeof(num));
  24. num[] = true;
  25. for(long long i=; i<maxn; i++)
  26. {
  27. if(!num[i])
  28. {
  29. for(long long j=i*i; j<=maxn; j+=i)
  30. {
  31. num[j] = true;
  32. }
  33. }
  34. }
  35. }
  36. struct Node
  37. {
  38. int x;
  39. int y;
  40. int step;
  41. Node() {}
  42. Node(int _x,int _y,int _step)
  43. {
  44. x = ;
  45. y = ;
  46. step =;
  47. }
  48. } start,endd;
  49. struct cmp
  50. {
  51. bool operator () (const Node &a,const Node &b)
  52. {
  53. return a.step>b.step;
  54. }
  55. };
  56. int check(int x,int y)
  57. {
  58. if(x>= && x < && y >= && y < && num[a[x][y]] == true&& !vis[x][y]) return ;
  59. return ;
  60. }
  61. priority_queue<Node,vector<Node>,cmp >que;
  62. bool bfs()
  63. {
  64. memset(vis,,sizeof(vis));
  65. Node tmp,now;
  66. while(!que.empty()) que.pop();
  67. que.push(start);
  68. vis[start.x][start.y] = ;
  69. start.step = ;
  70. while(!que.empty())
  71. {
  72. now = que.top();
  73. que.pop();
  74. //cout<<now.x<<" "<<now.y<<" "<<now.step<<endl;
  75. if(now.x == endd.x && now.y == endd.y)
  76. {
  77. endd.step = now.step;
  78. return true;
  79. }
  80. for(int i=; i<; i++)
  81. {
  82. tmp.x = now.x + dx[i];
  83. tmp.y = now.y + dy[i];
  84. tmp.step = now.step + ;
  85. if(check(tmp.x,tmp.y))
  86. {
  87. vis[tmp.x][tmp.y] = ;
  88. que.push(tmp);
  89. }
  90. }
  91. }
  92. return false;
  93. }
  94. void init()
  95. {
  96. int x = ;
  97. int y = ;
  98. int nn = ;
  99. int num=a[][]=;
  100. while(num>)
  101. {
  102. while((y+)<nn&&!a[x][y+]) a[x][++y]= --num;
  103. while((x+)<nn&&!a[x+][y]) a[++x][y]= --num;
  104. while((y-)>=&&!a[x][y-]) a[x][--y]= --num;
  105. while((x-)>=&&!a[x-][y]) a[--x][y]= --num;
  106.  
  107. }
  108. }
  109. int main()
  110. {
  111. //freopen("in.txt","r",stdin);
  112. init();
  113. is_prime();
  114. int Case = ;
  115. while(~scanf("%d %d",&n,&m))
  116. {
  117. for(int i=; i<; i++)
  118. {
  119. for(int j=; j<; j++)
  120. {
  121. if(a[i][j] == n)
  122. {
  123. start.x = i;
  124. start.y = j;
  125. }
  126. if(a[i][j] == m)
  127. {
  128. endd.x= i;
  129. endd.y = j;
  130. }
  131. }
  132. }
  133. bool prime = false;
  134. prime = bfs();
  135. printf("Case %d: ",Case++);
  136. if(prime) printf("%d\n",endd.step);
  137. else printf("impossible\n");
  138. }
  139. return ;
  140. }
 

HDU-4255的更多相关文章

  1. hdu 4255 A Famous Grid

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

  2. HDOJ 2111. Saving HDU 贪心 结构体排序

    Saving HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  3. 【HDU 3037】Saving Beans Lucas定理模板

    http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...

  4. hdu 4859 海岸线 Bestcoder Round 1

    http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...

  5. HDU 4569 Special equations(取模)

    Special equations Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

  6. HDU 4006The kth great number(K大数 +小顶堆)

    The kth great number Time Limit:1000MS     Memory Limit:65768KB     64bit IO Format:%I64d & %I64 ...

  7. HDU 1796How many integers can you find(容斥原理)

    How many integers can you find Time Limit:5000MS     Memory Limit:32768KB     64bit IO Format:%I64d ...

  8. hdu 4481 Time travel(高斯求期望)(转)

    (转)http://blog.csdn.net/u013081425/article/details/39240021 http://acm.hdu.edu.cn/showproblem.php?pi ...

  9. HDU 3791二叉搜索树解题(解题报告)

    1.题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=3791 2.参考解题 http://blog.csdn.net/u013447865/articl ...

  10. hdu 4329

    problem:http://acm.hdu.edu.cn/showproblem.php?pid=4329 题意:模拟  a.     p(r)=   R'/i   rel(r)=(1||0)  R ...

随机推荐

  1. 《Java程序设计》第六周学习总结 20165218 2017-2018-1

    20165218 2017-2018-1 <Java程序设计>第六周学习总结 教材学习内容总结 第8章 常用实用类 String类 不可以有子类 在java.lang 包中,被默认引入 S ...

  2. IntelliJ IDEA 详细图解最常用的配置 ,适合新人,解决eclipse转idea的烦恼

    刚刚使用IntelliJ IDEA 编辑器的时候,会有很多设置,会方便以后的开发,磨刀不误砍柴工. 比如:设置文件字体大小,代码自动完成提示,版本管理,本地代码历史,自动导入包,修改注释,修改tab的 ...

  3. RGB向yuv的转化最优算法,快得让你吃惊!

    朋友曾经给我推荐了一个有关代码优化的pdf文档<让你的软件飞起来>,看完之后,感受颇深.为了推广其,同时也为了自己加深印象,故将其总结为word文档.下面就是其的详细内容总结,希望能于己于 ...

  4. Just Random HDU - 4790 思维题(打表找规律)分段求解

    Coach Pang and Uncle Yang both love numbers. Every morning they play a game with number together. In ...

  5. unix awk手册读书笔记

    http://note.youdao.com/noteshare?id=9ac76eb63a53ac000f7814454642d2b0

  6. DataGridView导出到Word

    #region 使用Interop.Word.dll将DataGridView导出到Word /// <summary> /// 使用Interop.Word.dll将DataGridVi ...

  7. LightOJ 1226 - One Unit Machine Lucas/组合数取模

    题意:按要求完成n个任务,每个任务必须进行a[i]次才算完成,且按要求,第i个任务必须在大于i任务完成之前完成,问有多少种完成顺序的组合.(n<=1000 a[i] <= 1e6 mod ...

  8. 快速排序Quick sort

    快速排序Quick sort 原理,通过一趟扫描将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有数据都要小,然后再按此方法对这两部分数据分别进行快速排序,整个排序过程可以递归 ...

  9. C# 如何用多字符分割字符串

    用单字符分割字符串大家应该很熟悉,例如: string source = "dfd^Afdf^AAAAAA^Adfdf"; var list= source.Split('A'); ...

  10. loj6102 「2017 山东二轮集训 Day1」第三题

    传送门:https://loj.ac/problem/6102 [题解] 贴一份zyz在知乎的回答吧 https://www.zhihu.com/question/61218881 其实是经典问题 # ...