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 ...
随机推荐
- SkinSoft中.vssf样式文件在VS2005中的应用(图文)
前些天在项目中应用了皮肤控件,涉及到了(.ssk),也做了相应的记录. 但还是觉得.ssk的并不算满意,所以今天又粗略的研究了一下SkinSoft这个软件: 如题,他的作用就是自定义控件与窗体样式. ...
- C#传递参数
与函数交换数据的最好方式就是传递参数,在C#中有四种方法来控制参数如何传递给目标方法 C#中的参数修饰符 无修饰 如果一个参数没有用参数修饰符,则认为它将按值传递 out 输出参数由被调用的方法赋值. ...
- PAT1003——我要通过!
“答案正确”是自动判题系统给出的最令人欢喜的回复.本题属于PAT的“答案正确”大派送 —— 只要读入的字符串满足下列条件,系统就输出“答案正确”,否则输出“答案错误”. 得到“答案正确”的条件是: 1 ...
- 让项目管理理论“落地”——读《IT项目经理成长手记》有感
最近利用业余时间阅读了一本好书--<IT项目经理成长手记>(潘东.韩秋泉著).本书的两位作者是神州数码(中国本土最大的整合IT服务提供商)的高管,在书中他们介绍了神州数码在IT项目管理领域 ...
- hive sql 语法详解
Hive 是基于Hadoop 构建的一套数据仓库分析系统,它提供了丰富的SQL查询方式来分析存储在Hadoop 分布式文件系统中的数据,可以将结构 化的数据文件映射为一张数据库表,并提供完整的SQL查 ...
- (转)Android之接口回调机制
开发中,接口回调是我们经常用到的. 接口回调的意思即,注册之后并不立马执行,而在某个时机触发执行. 举个例子: A有一个问题不会,他去问B,B暂时解决不出来,B说,等我(B)解决了再告诉你(A)此时A ...
- ios卡牌游戏项目源码
上传者fu_sion,源码CardGame,一副牌从中抽取十二张牌,翻开牌可以相同的花色+1 相同的数字+4分 同一张牌翻开后可以盖上(算是一个BUG)就酱紫~~ 项目源码下载:http://code ...
- ListView 总结----持续中
ListView是常用的显示控件,默认背景是和系统窗口一样的透明色,如果给ListView加上背景图片,或者背景颜色时,滚动时listView会黑掉, 原因是,滚动时,列表里面的view重绘时,用的依 ...
- Fragstats软件使用及其景观生态学意义
[转]Fragstats软件使用及其景观生态学意义 原文地址:http://blog.163.com/shuailai@126/blog/static/13238040820104152513 ...
- c++11之右值引用
本文大部分来自这里,并不是完全着行翻译,如有不明白的地方请参考原文. 在c++中,创建临时对象的开销对程序的影响一直很大,比如以下这个例子: String getName(){ return “Kia ...