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 ...
随机推荐
- Spring中依赖注入的四种方式
在Spring容器中为一个bean配置依赖注入有三种方式: · 使用属性的setter方法注入 这是最常用的方式: · 使用构造器注入: · 使用Filed注入(用于注解方式). 使用属性的sett ...
- sqlserver ssms ctrl+e快捷键问题
大部分的环境 win2008R2+sqlserver2014R2 ssms 中 ctrl+e都是可以执行查询的,某些情况下,ctrl+e是个组合键 ctrl+e已按下,正在等待按下第二个键 调整方法 ...
- HDU 5399 Too Simple(过程中略微用了一下dfs)——多校练习9
Too Simple Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Probl ...
- 使用JDK自带的JAXB进行类和xml的互转
Classroom.java public class Classroom { private int id; private String name; private int grade; publ ...
- Web服务器(容器)请求常见的错误及其解决方法
首先我们来看看容器如何找到service()方法?(1)当在浏览器中输入 http://localhost:8080/firstweb/sayHi 这个地址后,容器是如何找到 HelloServlet ...
- vscode简洁的代码编辑器
微软去年tuichu的代码编辑神器,vscode很不辞哦,感受还不错.微软也破天荒地跨平台地支持... 支持多种语言 vscode(官方):code.visualstudio.com vscode中文 ...
- iOS-夜间模式(换肤设置)
概述 iOS 开发中有时候会有夜间模式(换肤设置)的需求, 主要是更改相关颜色操作每次切换夜间/白天模式时,都会发出通知给所有ViewController,让它们切换到相应的主题. 详细 代码下载:h ...
- 解决:Access denied for user ''@'sinochip-79e833' (using password: NO)
uthentication to host '' for user '' using method 'mysql_native_password' failed with message: Acces ...
- 可以尝试用Google Font API来摆脱网页字体的单调 仅仅抛砖引玉
http://www.nowamagic.net/librarys/veda/detail/2513
- 利用C#改写JAVA中的Base64.DecodeBase64以及Inflater解码
最近正在进行项目服务的移植工作,即将JAVA服务的程序移植到DotNet平台中. 在JAVA程序中,有个HTTP请求数据头中,包含一个BASE64编码的字符串,例如: eJyVjMENgDAMA1fp ...