bnuoj 25662 A Famous Grid (构图+BFS)
http://www.bnuoj.com/bnuoj/problem_show.php?pid=25662
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <stack>
#include <queue> #define N 150
using namespace std; struct Nod
{
int x,y,step;
}node[]; int map[N][N];
int mark[N][N];
int prim[]; int cx[]={,,-,};
int cy[]={,,,-}; int cnt=; void getMap(int x,int y,int re)
{
while()
{
int dx,dy;
dx = x + cx[(re+)%];
dy = y + cy[(re+)%];
if(!(dx>=&&dx<N&&dy>=&&dy<N))
{
break;
}
if(map[dx][dy]==)
{
map[dx][dy]= ++cnt;
node[cnt].x = dx;
node[cnt].y = dy;
// getMap(dx,dy,(re+1)%4);
x = dx;
y = dy;
re = (re+)%;
}
else
{
dx = x + cx[re%];
dy = y + cy[re%];
if(!(dx>=&&dx<N&&dy>=&&dy<N))
{
break ;
}
map[dx][dy]= ++cnt;
node[cnt].x = dx;
node[cnt].y = dy;
// getMap(dx,dy,re);
x = dx;
y = dy;
}
}
} int bfs(int x,int y)
{
memset(mark,,sizeof(mark));
queue<Nod> Q;
Nod temp;
temp.x = node[x].x;
temp.y = node[x].y;
temp.step = ;
Q.push(temp);
mark[temp.x][temp.y] = ;
while(!Q.empty())
{
Nod td = Q.front();
Q.pop();
if(td.x==node[y].x&&td.y==node[y].y)
{
return td.step;
}
int i;
for(i=;i<;i++)
{
temp.x = td.x + cx[i];
temp.y = td.y + cy[i];
temp.step = td.step + ;
if(map[temp.x][temp.y]>=&&map[temp.x][temp.y]<=&&prim[map[temp.x][temp.y]]&&!mark[temp.x][temp.y])
{
mark[temp.x][temp.y]=;
Q.push(temp);
}
}
}
return -;
} int main()
{
cnt=;
int cas=;
map[][]=;
getMap(,,);
node[].x = ;
node[].y = ;
// cout<<cnt<<endl;
int i,j;
prim[]=;
for(i=;i<;i++)
{
for(j=;j*j<=i;j++)
{
if(i%j==)
{
prim[i]=;
break;
}
}
}
// for(i=1;i<=100;i++)
// if(!prim[i])cout<<i<<" "; int x,y;
while(~scanf("%d%d",&x,&y))
{
printf("Case %d: ",cas++);
int ans = bfs(x,y);
if(ans==-)
{
puts("impossible");
}
else
{
printf("%d\n",ans);
}
} return ;
}
bnuoj 25662 A Famous Grid (构图+BFS)的更多相关文章
- hdu 4255 A Famous Grid
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4255 A Famous Grid Description Mr. B has recently dis ...
- bnuoj 1071 拼图++(BFS+康拓展开)
http://www.bnuoj.com/bnuoj/problem_show.php?pid=1071 [题意]:经过四个点的顺逆时针旋转,得到最终拼图 [题解]:康拓展开+BFS,注意先预处理,得 ...
- bnuoj 25659 A Famous City (单调栈)
http://www.bnuoj.com/bnuoj/problem_show.php?pid=25659 #include <iostream> #include <stdio.h ...
- HDU-4255
A Famous Grid Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- [LeetCode] 827. Making A Large Island
In a 2D grid of 0s and 1s, we change at most one 0 to a 1. After, what is the size of the largest is ...
- Unity 2018 Artificial Intelligence Cookbook Second Edition (Jorge Palacios 著)
https://github.com/PacktPublishing/Unity-2018-Artificial-Intelligence-Cookbook-Second-Edition 1 Beha ...
- Robots on a grid(DP+bfs())
链接:http://www.bnuoj.com/bnuoj/problem_show.php?pid=25585 Current Server Time: 2013-08-27 20:42:26 Ro ...
- ZOJ 3781 Paint the Grid Reloaded(BFS)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3781 Leo has a grid with N rows an ...
- Paint the Grid Reloaded(缩点,DFS+BFS)
Leo has a grid with N rows and M columns. All cells are painted with either black or white initially ...
随机推荐
- z-index无效
1.要想给元素设置z-index样式,必须先让它变成定位元素,说的明白一点,就是要给元素设置一个postion:relative(定位元素:position属性值设置除默认值static以外的元素,包 ...
- html5 requestAnimationFrame制作动画:旋转风车
详细内容请点击 在以往,我们在网页上制作动画效果的时候,如果是用javascript实现,一般都是通过定时器和间隔来实现的,出现HTML5之后,我们还可以用CSS3 的transitions和anim ...
- hexo资源--theme等
Hexo (https://github.com/hexojs/hexo) [3]hexo你的博客(http://ibruce.info/2013/11/22/hexo-your-blog/) [4] ...
- 桌面虚拟化之XenDesktop7
安装篇 1. 安装所需要的组件 2. 安装核心组件 配置篇 完整的部署 1. 打开Citrix Studio软件 2. 选择完整部署 3. 创建站点 4. 选择数据库(可以使用自己的或者默认使用自带的 ...
- js格式化日期,获取当月的第一天,与最后一天.
//格式化日期 function setDate(date){ y=date.getFullYear(); m=date.getMonth()+1; d=date.getDate(); ...
- Apple Watch开发之界面之间的正向传值
主要分两种,一种是故事板中通过segue进行的传值,第二种是使用代码. 贴代码 24行代码是代码进行传值 35是故事板中的
- iOS-设置启动图片
启动图片设置 设置方法一 这种方法里,默认模拟器和真机的尺寸和启动图片的尺寸相同. 通过美工提供各种尺寸的启动图片来适配屏幕的大小.这种方法要求美工提供各种屏幕大小的图片. 步骤如下: 1.如图所示, ...
- 20141212--C#对象比较
static void Main(string[] args) { Class2 oo = new Class2(); oo.shu = ; oo.zi = "你"; Class2 ...
- 对象this、currentTarget和target
在事件处理程序内部,对象this始终等于currentTarget的值,而target则只包含事件的实际目标.如果直接将事件处理程序指定给了目标元素,则this.currentTarget和targe ...
- 数组的join()函数操作
join()函数的功能为:把数组的所有元素放入一个字符串,元素通过指定的分隔符分隔. 设置这样的数组操作 var a = []; a.push(1);a.push(3.1415926);a.push( ...