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 ...
随机推荐
- 导入maven项目出现 Unsupported IClasspathEntry kind=4
Unsupported IClasspathEntry kind=4 这个异常会导致项目无法使用spring ide启动 来自:http://blog.csdn.net/kongqz/article/ ...
- Windows平台Hadoop编译、安装、配置与运行(转)
http://www.srccodes.com/p/article/38/build-install-configure-run-apache-hadoop-2.2.0-microsoft-windo ...
- Android:子线程向UI主线程发送消息
在Android里,UI线程是不同意被堵塞的.因此我们要将耗时的工作放到子线程中去处理. 那么子线程耗时处理后要如何通知UI线程呢? 我们能够在UI主线程中创建一个handler对象,然后通过重写其h ...
- php如何实现页面跳转
•PHP页面跳转一.header()函数 header()函数是PHP中进行页面跳转的一种十分简单的方法.header()函数的主要功能是将HTTP协议标头(header)输出到浏览器. header ...
- 解决this web application instance has been stopped already
重启tomcat的时候出错 Illegal access: this web application instance has been stopped already. Could not loa ...
- openerp发送给群组信息
发送给群组 self.pool.get('mail.group').message_post(cr, uid, [1], body=_('Welcome to ! Please ...
- EXCEPTION-SPRING
CreateTime--2016年8月23日09:00:47Author:Marydon 声明:异常类文章主要是记录了我遇到的异常信息及解决方案,解决方案大部分都是百度解决的,(这里只是针对我遇到 ...
- Centos调出图形化的网络管理
在Linux中设置网路,图形化很方便.在命令行下/etc/sysconfig/network-scripts/ifcfg-eth0 00.命令行下修正网路 [root@dzswj-test ~]#ca ...
- HDUOJ--4565 So Easy!
So Easy! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- 马老师 linux必备web服务入门及高级进阶
http://edu.51cto.com/course/course_id-866.html HTTP: HyperText Transfer Protocol 超文本传输协议 超链接: Web: h ...