hdu4255筛素数+广搜
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. 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, display its case number followed by the length of the
shortest path or "impossible" (without quotes) in one line.
9 32
10 12
Case 2: 7
Case 3: impossible
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
int book[401][401];
int a[401][401];
int b[401][401];
int vis[160005];
int prime[160005];
struct node
{
int x;
int y;
int s;
} que[160005];
void make1()
{
for(int i=1;i<=160001;i++)
prime[i]=1;
prime[1] = 0;
for(int i = 2; i <= 160001; i++)
{
if(prime[i])
{
for(int j = 2*i; j <= 160001; j+=i)
prime[j] = 0;
}
}
int x,y;
int n=400;
int tot=160000;
a[0][0]=160000;
x=0,y=0;
while(tot>1)
{
while(y+1<n&&!a[x][y+1])
{
a[x][++y]=--tot;
}
while(x+1<n&&!a[x+1][y])
{
a[++x][y]=--tot;
}
while(y-1>=0&&!a[x][y-1])
{
a[x][--y]=--tot;
}
while(x-1>=0&&!a[x-1][y])
{
a[--x][y]=--tot;
}
}
for(int i=0; i<400; i++)
for(int j=0; j<400; j++)
{
if(prime[a[i][j]]==1)
b[i][j]=1;
else
b[i][j]=0;
}
}
int main()
{
int t1,t2;
int ans=0;
make1();
while(scanf("%d%d",&t1,&t2)!=EOF)
{
int next[4][2]= {0,1,1,0,0,-1,-1,0};
memset(book,0,sizeof(book));
if(t1==t2)
printf("Case %d: 0\n",++ans);
else
{
int startx,starty,endx,endy;
for(int i=0; i<=399; i++)
for(int j=0; j<=399; j++)
{
if(a[i][j]==t1)
{
startx=i;
starty=j;
}
if(a[i][j]==t2)
{
endx=i;
endy=j;
}
}
int head=1,tail=1;
que[head].x=startx;
que[head].y=starty;
tail++;
book[startx][starty]=1;
int flag=0;
while(head<tail)
{
for(int k=0; k<4; k++)
{
int tx=que[head].x+next[k][0];
int ty=que[head].y+next[k][1];
if(tx<0||tx>399||ty<0||ty>399)
continue;
if(b[tx][ty]==0&&book[tx][ty]==0)
{
book[tx][ty]=1;
que[tail].x=tx;
que[tail].y=ty;
que[tail].s=que[head].s+1;
tail++;
}
if(tx==endx&&ty==endy)
{
flag=1;
break;
}
}
if(flag==1)
break;
head++;
}
if(flag==1)
printf("Case %d: %d\n",++ans,que[tail-1].s);
else
printf("Case %d: impossible\n",++ans);
}
}
return 0;
}
hdu4255筛素数+广搜的更多相关文章
- 双向广搜 POJ 3126 Prime Path
POJ 3126 Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16204 Accepted ...
- nyoj 592 spiral grid(广搜)
题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=592 解决以下问题后就方便用广搜解: 1.将数字坐标化,10000坐标为(0,0),这样就 ...
- poj3126 Prime Path 广搜bfs
题目: The ministers of the cabinet were quite upset by the message from the Chief of Security stating ...
- [Luogu]A%BProblem——线性筛素数与前缀和
题目描述 题目背景 题目名称是吸引你点进来的[你怎么知道的] 实际上该题还是很水的[有种不祥的预感..] 题目描述 区间质数个数 输入输出格式 输入格式: 一行两个整数 询问次数n,范围m接下来n行, ...
- 广搜 poj3278 poj1426 poj3126
Catch That Cow Time Limit: 2000 MS Memory Limit: 65536 KB 64-bit integer IO format: %I64d , %I64u Ja ...
- HDU--杭电--1195--Open the Lock--深搜--都用双向广搜,弱爆了,看题了没?语文没过关吧?暴力深搜难道我会害羞?
这个题我看了,都是推荐的神马双向广搜,难道这个深搜你们都木有发现?还是特意留个机会给我装逼? Open the Lock Time Limit: 2000/1000 MS (Java/Others) ...
- HDU 5652(二分+广搜)
题目链接:http://acm.hust.edu.cn/vjudge/contest/128683#problem/E 题目大意:给定一只含有0和1的地图,0代表可以走的格子,1代表不能走的格 子.之 ...
- 【板子】gcd、exgcd、乘法逆元、快速幂、快速乘、筛素数、快速求逆元、组合数
1.gcd int gcd(int a,int b){ return b?gcd(b,a%b):a; } 2.扩展gcd )extend great common divisor ll exgcd(l ...
- CF449C Jzzhu and Apples (筛素数 数论?
Codeforces Round #257 (Div. 1) C Codeforces Round #257 (Div. 1) E CF450E C. Jzzhu and Apples time li ...
随机推荐
- 如何引用XML文件生成C#类
目录 XSD File Generate Class File Simply. 1 Why use XSD file to create C# classes?... 2 How to convert ...
- Javascript基础系列之(五)条件语句(if条件语句)
if 是flash的常用语法之一,其格式如下 if(coditon) statement1 (else statement2) 其中,coditon可以是任何表达式,甚至不比是真正的布尔值,因为Jav ...
- jQuery基础之(五)jQuery自定义添加"$"与解决"$"的冲突
1.自定义添加$ 从上面四篇文章我们看到jQuery的强大,但无论如何,jQuery都不可能满足所有用户的需求,而且有一些需求十分小众,也不适合放到整个jQuery框架中,正是因为这一点,jQuery ...
- 每天一个linux命令(37):free 命令
free命令可以显示Linux系统中空闲的.已用的物理内存及swap内存,及被内核使用的buffer.在Linux系统监控的工具中,free命令是最经常使用的命令之一. 1.命令格式: free [参 ...
- webstrom 中启用emmet插件的方法
参考页面:https://www.jetbrains.com/help/webstorm/2016.2/enabling-emmet-support.html Basics Native Emmet ...
- logic标签用法
logic <logic:iterate> <% Dog dog1=new Dog(); dog1.setAge(2); dog1.setName("xiaoming& ...
- bean标签
bean的uri的路径 bean标签是属于struts中的标签,使用要在 Struts 1.3 Libraries中 struts-taglib-1.3.8.jar 中META-INFtld ...
- Tomcat tomcat-users.xml详解
conf/tomcat-users.xml <?xml version='1.0' encoding='utf-8'?> <tomcat-users> <role rol ...
- 【CodeForces 613A】Peter and Snow Blower
题 题意 给出原点(不是(0,0)那个原点)的坐标和一个多边形的顶点坐标,求多边形绕原点转一圈扫过的面积(每个顶点到原点距离保持不变). 分析 多边形到原点的最小距离和最大距离构成的两个圆之间的圆环就 ...
- 44.Android之Shape设置虚线、圆角和渐变学习
Shape在Android中设定各种形状,今天记录下,由于比较简单直接贴代码. Shape子属性简单说明一下: gradient -- 对应颜色渐变. startcolor.endcolor就不多说 ...