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 ...
随机推荐
- Python(2.7.6) 标准日志模块的简单示例
Python 标准库中的 logging 模块提供了一套标准的 API 来处理日志信息的打印. import logging logging.basicConfig( level = logging. ...
- java模拟有验证码的Http登陆
所需资源下载链接(资源免费,重在分享) Tesseract:http://download.csdn.net/detail/chenyangqi/9190667 jai_imageio-1.1-alp ...
- PAT1005—— 继续(3n+1)猜想
卡拉兹(Callatz)猜想已经在1001中给出了描述.在这个题目里,情况稍微有些复杂. 当我们验证卡拉兹猜想的时候,为了避免重复计算,可以记录下递推过程中遇到的每一个数.例如对n=3进行验证的时候, ...
- CSS之拖拽1
PageX:鼠标在页面上的位置,从页面左上角开始,即是以页面为参考点,不随滑动条移动而变化. clientX:鼠标在页面上可视区域的位置,从浏览器可视区域左上角开始,即是以浏览器滑动条此刻的滑动 到的 ...
- SQL IDENTITY(int,1,1) 用法
select IDENTITY(int,1,1) as SortID from tb_order 仅当 SELECT 语句中有 INTO 子句时,才能使用 IDENTITY 函数. select ID ...
- Sql Server 数据库之间如何进行跨网远程连接访问
场景说明 现在有一台A电脑和一台B电脑,两台电脑都安装了Sql Server数据库,两台电脑不在一个局域网(我们考虑的是不同网络的两台数据库连接),比如A电脑在公司,B电脑在家里,现在我要在家里用B电 ...
- 【原创】CQ数据库损坏修复
上周三中午CQ数据库数据文件损坏,导致登录时一张关键的表无法查询报错从而cq无法登录,此次故障和上次的一样,不过恢复的非常曲折,导致停机两天,现简单的通报下恢复过程: 故障原因: oracle-902 ...
- Cocos2d-JS坐标系
在图形图像和游戏应用开发中坐标系是非常重要的,我们在Android和iOS等平台应用开发的时候使用的二维坐标系它的原点是在左上角的.而在Cocos2d-JS坐标系中它原点是在左下角的,而且Cocos2 ...
- cass实体编码列表
地物名称 编码 图层 类别 参数一 参数二 实体类型 三角点 131100 KZD 20 gc113 3 SPECIAL,1 三角点分数线 131110 KZD 附 LINE 三角点高程注记 1311 ...
- MyBatis用嵌套ResultMap实现一对多映射
我的技术博客经常被流氓网站恶意爬取转载.请移步原文:http://www.cnblogs.com/hamhog/p/3959451.html,享受整齐的排版.有效的链接.正确的代码缩进.更好的阅读体验 ...