spiral grid

时间限制:2000 ms  |  内存限制:65535 KB
难度:4
 
描述
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的更多相关文章

  1. ACM spiral grid

    spiral grid 时间限制:2000 ms  |  内存限制:65535 KB 难度:4   描述 Xiaod has recently discovered the grid named &q ...

  2. nyoj 592 spiral grid(广搜)

    题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=592 解决以下问题后就方便用广搜解: 1.将数字坐标化,10000坐标为(0,0),这样就 ...

  3. hdu 4255 A Famous Grid

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

  4. hdu4255筛素数+广搜

    Mr. B has recently discovered the grid named "spiral grid".Construct the grid like the fol ...

  5. HDU-4255

    A Famous Grid Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  6. 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 ...

  7. [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 ...

  8. [Solution] 885. Spiral Matrix Ⅲ

    Difficulty: Medium Problem On a 2 dimensional grid with R rows and C columns, we start at (r0, c0) f ...

  9. 【Gym - 100947G】Square Spiral Search

    BUPT 2017 summer training (for 16) #1C 题意 A new computer scientist is trying to develop a new memory ...

随机推荐

  1. Starting MySQL.. ERROR! The server quit without updating PID file (/gechong/mysqldata/10-9-23-119.pid).

    配置文件修改错误了,还原一下重启服务就OK了. # /etc/init.d/mysql start

  2. git删除本地的资源,如何恢复?

    1.$ git reset --hard HEAD  将提交重置 2.使用 $ git checkout TestTimer.java(文件名) 恢复过来了

  3. netsh学习

    show allowedprogram –显示被允许的程序配置 show config - 显示防火墙的配置 show currentprofile -显示 Windows 防火墙的当前配置文件. s ...

  4. 关闭ubuntu终端的BELL声音

    在shell提示符下面操作时有时会用到Tab来自动补全,这个时候系统就会发出BELL的声音,听了让人挺烦的. 有个方法能解决:编辑 /etc/inputrc,找到#set bell-style non ...

  5. MVC第一次访问比较慢的解决方案

    一.NGen优化 %WINDIR%\Microsoft.NET\Framework64\v4.0.30319\ngen install EntityFramework.Core.dll %WINDIR ...

  6. 基于LumiSoft.Net.dll发、收、删邮件

    发邮件: using LumiSoft.Net.SMTP.Client; Mime m = new Mime(); MimeEntity mainEntity = m.MainEntity; // F ...

  7. python学习笔记之函数(方法)

    def func(x): print 'x is', x x = 2 print 'Changed local x to', x x = 50 func(x) print 'x is still', ...

  8. 13、java中8中基本类型

    一.基本类型介绍 关键字 数据类型 占用字节数 取值范围 默认值 byte 字节型 1个字节 -128~127 0 char 字符型 2个字节 Unicode0~Unicode215-1 \u0000 ...

  9. 【mysql】Innodb三大特性之double write

    1.doublewrite buffer(mysql官方的介绍) InnoDB uses a novel file flush technique called doublewrite. Before ...

  10. Nginx中Laravel的配置

    server { listen 80; server_name sub.domain.com; set $root_path '/var/www/html/application_name/publi ...