题意:蛇形填数,然后素数处是障碍,给你起点终点,求步数;

思路:其实就是bfs,关键是将数字转换成位置比较难;

bfs其实比较简单,就是固定的思路,固定的步骤;

模板:

 const int dir[][] = {{-, }, {, }, {, }, {, -}};
int vis[maxn], d[maxn];
bool is_ok(int x, int y)///坐标是否合格,按照题意来进行
{
if(x< || y<||x>n||y>n)
return false;
return true;
}
int dfs(Node st,Node ed)///起点终点
{
queue<Node> q;
q.push(st);///压进起点
memset(vis,,sisteof(vis));
memset(d,,sizeof(d));
int ss = a[st.x][st.y];
d[ss] = ;
int edd = a[ed.x][ed.y];
while(!q.empty())
{
Node c = q.front(),v;
q.pop();
int stt = a[c.x][c.y];
if(stt == edd)///先判断是否到达终点
return d[stt];
repu(i,,)
{
v.x = c.x + dir[i][];
v.y = c.y + dir[i][];
if(is_ok(v.x,v.y))///坐标是否合格
{
int sd = a[v.x][v.y];
if(!vis[sd])///符合所有条件后
{
q.push(v);///压进
vis[sd] = ;///V过
d[sd] = d[stt] + ;///步数为之前的+1
}
}
else
continue;
}
}
return -;
}

该题代码

 #include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <algorithm>
#include<queue>
#include <set>
#define repu(i,a,b) for(int i=a;i<b;i++)
using namespace std;
#define N 1000010
#define ll long long
#define _cle(m, a) memset(m, a, sizeof(m))
const int maxn = ;
const int mn = ;
int tot;
int a[][];
struct Node
{
int x, y;
} nodes[maxn];
void init()
{
memset(a, , sizeof(a));
a[mn][mn] = ;
tot = ;
nodes[].x = mn;
nodes[].y = mn;
int cur = ;
int i = mn, j = mn+;
while(tot <= maxn)
{
int t;
t = ;
i++;
while(t < cur*)
{
a[--i][j] = ++tot;
nodes[tot].x = i;
nodes[tot].y = j;
t++;
}
t = ;
while(t < cur*)
{
a[i][--j] = ++tot;
nodes[tot].x = i;
nodes[tot].y = j;
t++;
}
t = ;
while(t < cur*)
{
a[++i][j] = ++tot;
nodes[tot].x = i;
nodes[tot].y = j;
t++;
}
t = ;
while(t < cur*)
{
a[i][++j] = ++tot;
nodes[tot].x = i;
nodes[tot].y = j;
t++;
}
++j;
cur++;
}
}
int prime[maxn];
void is_prime()
{
memset(prime, , sizeof(prime));
int m = sqrt(maxn+0.5);
prime[] = prime[] = ;
for(int i = ; i <= m; i++)
{
if(!prime[i])
{
for(int j = i*i; j <= maxn; j+=i)
{
prime[j] = ;
}
}
}
} const int dir[][] = {{-, }, {, }, {, }, {, -}};
int vis[maxn], d[maxn];
bool is_ok(int x, int y)
{
return x>= && y >=;
}
int bfs(Node z,Node b)
{
queue<Node> q;
q.push(z);
memset(vis,,sizeof(vis));
memset(d,,sizeof(d));
int ss = a[z.x][z.y];
d[ss] = ;
int ed = a[b.x][b.y];
while(!q.empty())
{
Node c = q.front(),v;
q.pop();
int st = a[c.x][c.y];
if(st == ed)
return d[st];
repu(i,,)
{
v.x = c.x + dir[i][];
v.y = c.y + dir[i][];
if(is_ok(v.x,v.y))
{
int sd = a[v.x][v.y];
if(!vis[sd]&&prime[sd])
{
q.push(v);
vis[sd] = ;
d[sd] = d[st] + ;
}
}
else
continue;
}
}
return -;
}
int main()
{
init();
is_prime();
int x, y, kase = ;
while(~scanf("%d%d", &x, &y))
{
if(!prime[x] || !prime[y])
{
printf("Case %d: impossible\n", ++kase);
continue;
}
int ans = bfs(nodes[x], nodes[y]);
if(ans == -) printf("Case %d: impossible\n", ++kase);
else printf("Case %d: %d\n", ++kase, ans);
} return ;
}

HDU-4255 BFS 最短路的更多相关文章

  1. POJ 2251 Dungeon Master (BFS最短路)

    三维空间里BFS最短路 #include <iostream> #include <cstdio> #include <cstring> #include < ...

  2. hdu 4568 Hunter 最短路+dp

    Hunter Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  3. hdu 4531 bfs(略难)

    题目链接:点我 第一次不太清楚怎么判重,现在懂了,等下次再做 /* *HDU 4531 *BFS *注意判重 */ #include <stdio.h> #include <stri ...

  4. 【bzoj5049】[Lydsy九月月赛]导航系统 并查集+双向BFS最短路

    题目描述 给你一张 $n$ 个点 $m$ 条边的随机图,边权为1.$k$ 次询问两点间最短路,不连通则输出-1. 输入 第一行包含3个正整数n,m,k(2<=n<=100000,1< ...

  5. 【bzoj1189】[HNOI2007]紧急疏散evacuate BFS最短路+动态加边网络流

    题目描述 发生了火警,所有人员需要紧急疏散!假设每个房间是一个N M的矩形区域.每个格子如果是'.',那么表示这是一块空地:如果是'X',那么表示这是一面墙,如果是'D',那么表示这是一扇门,人们可以 ...

  6. BZOJ 1195 [HNOI2006]最短母串 (Trie图+状压+bfs最短路)

    BZOJ1195 LOJ10061 题目大意:给你$n$个模式串,求一个最短且字典序最小的文本串并输出这个串,$n<=12,len<=50$ 首先对所有模式串构造$Trie$图,$Trie ...

  7. UVa 1600 Patrol Robot (BFS最短路 && 略不一样的vis标记)

    题意 : 机器人要从一个m * n 网格的左上角(1,1) 走到右下角(m, n).网格中的一些格子是空地(用0表示),其他格子是障碍(用1表示).机器人每次可以往4个方向走一格,但不能连续地穿越k( ...

  8. BFS(最短路) HDU 2612 Find a way

    题目传送门 /* BFS:和UVA_11624差不多,本题就是分别求两个点到KFC的最短路,然后相加求最小值 */ /***************************************** ...

  9. C - 小明系列故事――捉迷藏 HDU - 4528 bfs +状压 旅游-- 最短路+状压

    C - 小明系列故事――捉迷藏 HDU - 4528 这个题目看了一下题解,感觉没有很难,应该是可以自己敲出来的,感觉自己好蠢... 这个是一个bfs 用bfs就很好写了,首先可以预处理出大明和二明能 ...

  10. HDU 1548 A strange lift (bfs / 最短路)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1548 A strange lift Time Limit: 2000/1000 MS (Java/Ot ...

随机推荐

  1. CSS垂直居中对齐

    用CSS有多种方法实现垂直居中对齐.如果已知外部div的高度,不管是否知道内部div的高度,垂直居中实现起来很简单,但如果内部div高度是变量,如文字,垂直居中实现起来就比较复杂了,很可能需要使用ha ...

  2. java面试---summay

    1:合适的建立索引,数据量比较大的时候,如果频繁的进行修改插入则不建议建立索引! 2:什么时候适合建索引,在什么字段上面建立索引? (被当做查询条件的) 3:什么叫做编译错误,什么叫做运行时异常 能被 ...

  3. BZOJ一天提交 51纪念(二)

    今天作死又交了一发呢...于是屯题就全用完啦~ 有一次拷错CE,还有一次本来的程序就是错的的说... 可是我希望看到我努力的人并不会看到我的努力呢,尽管如此一个人也要坚持走到底哦,就如同这不完美的提交 ...

  4. idea tomcat +eclipse式的部署

    使用习惯了eclipse, 还没开始使用maven, 使用idea 有些不太习惯,现在记录下来,以备忘. /*这一步在tomcat使用external source时,其实是不起作用的**/   a. ...

  5. 原创:ASP.Net状态管理读书笔记--思维导图

    导图文件下载 课前提问几个问题 使用Session 配置 model aspnet_regsql.exe 常见问答 问:为什么Session在有些机器上偶尔会丢失?答:可能和机器的环境有关系,比如:防 ...

  6. 2015.11.16JQuery 隐藏,显示按钮.

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  7. Oracle创建表

    //创建表,列的内容 -- Create tablecreate table T_HQ_PC( pinpai VARCHAR2(20) not null, xingh VARCHAR2(40), ji ...

  8. bzoj 2330: [SCOI2011]糖果

    #include<cstdio> #include<iostream> using namespace std; ],next[],u[],v[],h,t,a[]; ],f[] ...

  9. treap 1286郁闷的出纳员.cpp

    #include<cstdio>#include<cstdlib>#include<ctime>struct shu{ int l,r,sum,zhi,dui;}a ...

  10. win10 ctrl + 空格 热键取消

    关键:添加English,并上移到第一,即开机默认即可.Win8+不用修改注册表的,只有win7里才可能需要修改注册表. http://superuser.com/questions/327479/c ...