HDU-4255 BFS 最短路
题意:蛇形填数,然后素数处是障碍,给你起点终点,求步数;
思路:其实就是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 最短路的更多相关文章
- POJ 2251 Dungeon Master (BFS最短路)
三维空间里BFS最短路 #include <iostream> #include <cstdio> #include <cstring> #include < ...
- hdu 4568 Hunter 最短路+dp
Hunter Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- hdu 4531 bfs(略难)
题目链接:点我 第一次不太清楚怎么判重,现在懂了,等下次再做 /* *HDU 4531 *BFS *注意判重 */ #include <stdio.h> #include <stri ...
- 【bzoj5049】[Lydsy九月月赛]导航系统 并查集+双向BFS最短路
题目描述 给你一张 $n$ 个点 $m$ 条边的随机图,边权为1.$k$ 次询问两点间最短路,不连通则输出-1. 输入 第一行包含3个正整数n,m,k(2<=n<=100000,1< ...
- 【bzoj1189】[HNOI2007]紧急疏散evacuate BFS最短路+动态加边网络流
题目描述 发生了火警,所有人员需要紧急疏散!假设每个房间是一个N M的矩形区域.每个格子如果是'.',那么表示这是一块空地:如果是'X',那么表示这是一面墙,如果是'D',那么表示这是一扇门,人们可以 ...
- BZOJ 1195 [HNOI2006]最短母串 (Trie图+状压+bfs最短路)
BZOJ1195 LOJ10061 题目大意:给你$n$个模式串,求一个最短且字典序最小的文本串并输出这个串,$n<=12,len<=50$ 首先对所有模式串构造$Trie$图,$Trie ...
- UVa 1600 Patrol Robot (BFS最短路 && 略不一样的vis标记)
题意 : 机器人要从一个m * n 网格的左上角(1,1) 走到右下角(m, n).网格中的一些格子是空地(用0表示),其他格子是障碍(用1表示).机器人每次可以往4个方向走一格,但不能连续地穿越k( ...
- BFS(最短路) HDU 2612 Find a way
题目传送门 /* BFS:和UVA_11624差不多,本题就是分别求两个点到KFC的最短路,然后相加求最小值 */ /***************************************** ...
- C - 小明系列故事――捉迷藏 HDU - 4528 bfs +状压 旅游-- 最短路+状压
C - 小明系列故事――捉迷藏 HDU - 4528 这个题目看了一下题解,感觉没有很难,应该是可以自己敲出来的,感觉自己好蠢... 这个是一个bfs 用bfs就很好写了,首先可以预处理出大明和二明能 ...
- 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 ...
随机推荐
- 数据库中Schema(模式)概念的理解
在学习SQL的过程中,会遇到一个让你迷糊的Schema的概念.实际上,schema就是数据库对象的集合,这个集合包含了各种对象如:表.视图.存储过程.索引等.为了区分不同的集合,就需要给不同的集合起不 ...
- 3.7 嵌入式SQL
可以放入所有高级语言中去,如C 因为,SQL是过程性语句,需要高级语言的非过程性处理集合的分类处理 一.一般形式 所有的SQL语句都必须加前缀EXEC SQL SQL语句完成结束标志(:或END EX ...
- ios基础篇(八)——UITabBarController的简单介绍
一.简介 UITabBarController和UINavigationController类似,UITabBarController也可以轻松地管理多个控制器,轻松完成控制器之间的切换,典型的例子就 ...
- SVN删除同名文件夹
解 释一下: SVN 出现这个错误的原因是我删除了一个文件夹后又创建了一个同名文件夹. 在 svn server 端,好像是不能区分这两个文件夹,所以出现了错误. ...
- JDE变量说明
BC Business view columns. Columns that are included in the attached business view. These columns are ...
- win7刷新图标缓存
建立bat文件 rem 关闭explorer.exetaskkill /f /im explorer.exeattrib -h -i %userprofile%\AppData\Local\IconC ...
- 介绍开源的.net通信框架NetworkComms
Networkcomms 是一款C# 语言编写的TCP/UDP通信框架 作者是英国人 以前是收费的 目前作者已经开源 开源地址是:https://github.com/MarcFletcher/ ...
- 在ASP.NET MVC中使用CKEditor和CkFinder
在你需要使用editor控件的页面头部添加: <head> ... <script type="text/javascript" src="/ckedi ...
- JS访问剪切板中的图片
google出来一个html2canvas,它利用canvas来渲染读取的DOM树,也就是说它只能截取document里的内容,如果要像qq截图那样,应该怎么做?用过百度的Ueditor编辑器的朋友都 ...
- socket.io遇到的问题
一.socket.io指定客户端html文件所用到的sendFile()方法中的文件路径必须是绝对路径,而且要符合一定规则: app.get('/',function(req,res){ res.se ...