nyoj592 spiral grid
spiral grid
- 描述
- 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
唉,好久没写搜索了,竟然写了两个晚上,终于AC了;
错误原因:当被找的是素数是,则不能找到,素数孔,能出不能进,也就是说,输入100 3 输出impossible,而输入3 100,则不是如此;
代码如下:#include<algorithm>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
int a[];
int dir[][]={,,,-,,,-,};
const int maxn=;
int vst[maxn][maxn];//访问标记
int map[][],map1[][];
struct state
{
int x,y;//坐标位置;
int step;//搜索统计
};
state mm[maxn];
bool check(state s,int bb)//判断该点是否满足条件
{
//cout<<"**"<<endl;
if( (map1[s.x][s.y]==bb)||(!vst[s.x][s.y] && map[s.x][s.y]!= && s.x>= && s.x< && s.y>= && s.y<) )
return ;
else return ;
}
int bfs(int aa,int bb)
{int i,j;
memset(vst,,sizeof(vst));
for(i=;i<=;i++)
for(j=;j<=;j++)
if(map1[i][j]==aa)
{
goto end;
}
end :
// cout<<i<<j<<endl;
queue<state>q;
state now,next,st;
st.x=i;st.y=j;
st.step=;
q.push(st);
vst[st.x][st.y]=;
while(!q.empty())
{
now=q.front();
q.pop();
if(map1[now.x][now.y]==bb)
{
return now.step;
}
for(i=;i<;i++)
{
next.x=now.x+dir[i][];
next.y=now.y+dir[i][];
next.step=now.step+;
if(check(next,bb))//满足条件;
{
//cout<<next.x<<"***"<<next.y<<endl;
q.push(next);
vst[next.x][next.y]=;
}
}
}
return ;
}
void fun( )//判断是否是素数
{
int i,j,k;
memset(a,,sizeof(a));
a[]=;
for(i=;i<=;i++)
for(j=;i*j<=;j++)
a[i*j]=;
}
void fuu()//蛇形填数
{
int tot,x=,y=,n=;
memset(map,,sizeof(map));
memset(map1,,sizeof(map1));
tot=map1[][]=;
map[][]=;
while(tot>)
{
while(y+<n && !map1[x][y+])
{
--tot;
if(a[tot]!=)
{
map[x][y+]=;//如果此位置不是素数则能走,能走的为1,否则为零;
}
map1[x][++y]=tot;//初始化二位数组,填数
}
while( x+<n && !map1[x+][y])
{
--tot;
if(a[tot]!=)
{map[x+][y]=;}
map1[++x][y]=tot;
}
while(y->= && !map1[x][y-])
{ --tot;
if(a[tot]!=)
{map[x][y-]=;}
map1[x][--y]=tot;
}
while(x> && !map1[x-][y])
{
--tot;
if(a[tot]!=)
{map[x-][y]=;}
map1[--x][y]=tot;
}
}
}
int main()
{
int m,n,nn=,k;
fun();fuu();
while(cin>>m>>n)
{
if(m==n)
{
printf("Case %d: 0\n",nn++);continue;//相同输入零
}
else if(a[n]==)//如果第二个是素数则输出impossible
{printf("Case %d: impossible\n",nn++);continue;}
k=bfs(m,n);
if(k==)
printf("Case %d: impossible\n",nn++);
else printf("Case %d: %d\n",nn++,k);
}
return ;
}
nyoj592 spiral grid的更多相关文章
- ACM spiral grid
spiral grid 时间限制:2000 ms | 内存限制:65535 KB 难度:4 描述 Xiaod has recently discovered the grid named &q ...
- nyoj 592 spiral grid(广搜)
题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=592 解决以下问题后就方便用广搜解: 1.将数字坐标化,10000坐标为(0,0),这样就 ...
- hdu 4255 A Famous Grid
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4255 A Famous Grid Description Mr. B has recently dis ...
- hdu4255筛素数+广搜
Mr. B has recently discovered the grid named "spiral grid".Construct the grid like the fol ...
- HDU-4255
A Famous Grid Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- ROS_Kinetic_x ROS栅格地图庫 Grid Map Library
源自:https://github.com/ethz-asl/grid_map Grid Map Overview This is a C++ library with ROS interface t ...
- [Swift]LeetCode885. 螺旋矩阵 III | Spiral Matrix III
On a 2 dimensional grid with R rows and C columns, we start at (r0, c0) facing east. Here, the north ...
- [Solution] 885. Spiral Matrix Ⅲ
Difficulty: Medium Problem On a 2 dimensional grid with R rows and C columns, we start at (r0, c0) f ...
- 【Gym - 100947G】Square Spiral Search
BUPT 2017 summer training (for 16) #1C 题意 A new computer scientist is trying to develop a new memory ...
随机推荐
- JS将数字转换为大写汉字人民币
<script language="jscript"> function convertCurrency(currencyDigits) { // Constants: ...
- windows上安装db2 spatial extender和ArcSDE的问题
因客户的DB2版本是9.7的版本,所以测试环境也只能安装这个版本,但在ArcSDE的最小支持版本里至少需要V9.7 pack 4的版本,低于这个版本是不行的. 安装DB2和spatial extend ...
- (转)CATALINA_BASE与CATALINA_HOME的区别
到底CATALINA_HOME和CATALINA_BASE有什么区别呢,之前因为都是小打小闹的在服务器上安装一个tomcat就得了,然后根据前人的配置,将CATALINA_HOME和CATALINA_ ...
- linux 系统文件的特殊权限
文件权限与归属 Linux系统中的一切都是文件,但每个文件的类型不尽相同,并且Linux系统会用不同的符号来加以区分,常见的包括有 -:普通文件,d:目录文件,l:链接文件,b:块设备文件,c:字符设 ...
- swift上传图片
import UIKit import AFNetworking class YJRequest: NSObject { //#pragma mark - 上传图片 func uploadImageW ...
- HDUOJ--2079选课时间(题目已修改,注意读题)
选课时间(题目已修改,注意读题) Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- 调试解决iOS内存泄漏
这里讲述在没有ARC的情况下,如何使用Instruments来查找程序中的内存泄露,以及NSZombieEnabled设置的使用. 本文假设你已经比较熟悉Obj-C的内存管理机制. 实验的开发环境:X ...
- 用浏览器访问WCF
在开发的时候,为客户端编写代码访问WCF的方式,我们应该比较熟悉,主要是添加一个代理,然后通过这个代理进行访问. 如果用浏览器访问WCF呢?(其实最终是想在JS里面访问)用浏览器访问.测试Web Se ...
- Linux命令-某个用户组下面的所有用户
groups 查看当前登录用户的组内成员 groups gliethttp 查看gliethttp用户所在的组,以及组内成员whoami 查看当前登录用户名 系统内有关组的信息放在/etc/group ...
- 基于HTTP在互联网传输敏感数据的消息摘要、签名与加密方案
基于HTTP在互联网传输敏感数据的消息摘要.签名与加密方案 博客分类: 信息安全 Java 签名加密AESMD5HTTPS 一.关键词 HTTP,HTTPS,AES,SHA-1,MD5,消息摘要,数 ...