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 ...
随机推荐
- sql distinct 不能比较或排序 text、ntext 和 image 数据类型,除非使用 IS NULL 或 LIKE 运算符
有个文章的表内容是列项,类型是text 我查询的是内容相同的文章,把其中的一个删除 select 内容 from 文章 group by 内容 having count(*)>1 查询ID和题目 ...
- 关于linux的添加永久静态路由的static-routes方法
一:使用 route 命令添加 使用route 命令添加的路由,机器重启或者网卡重启后路由就失效了,方法: //添加到主机的路由 # route add –host 192.168.1.11 dev ...
- Swift Modules for React Native
React Native is an Objective-C application framework that bridges JavaScript applications running in ...
- google打不开解决的方法
14.5.27以来.谷歌又打不开了. 从网上找了些国内的googleserverIP,例如以下: const char* g_google_ips[18] = { "203.208.48.1 ...
- java 打包war包
jar -cvf news.war news war包放在Tomcat webApp中可以自动解压.
- &和&&的一点区别
&两边的语句都会执行,&&有可能只执行左边的语句. &做位运算与的时候可以更高效的判断一个数字是不是奇数. 1&1 = 1 0&1 = 0 所以和1与结 ...
- How to forcefully delete a daemonset or a pod in kubernetes cluster
I have setup a kubernetes cluster which is working fine. I created deployment with type as daemonset ...
- Servlet线程安全性
问题:使用以下的代码演示servlet的线程安全问题? public class MultiThreadQuestion extends HttpServlet { public int count ...
- SDL 2.0 API by Category
Basics View information and functions related to... View the header Initialization and Shutdown SDL. ...
- C++:借助tinyxml2读取XML文件
// XMLT01.cpp : 定义控制台应用程序的入口点.//#include "stdafx.h"#include <iostream>#include " ...