nyoj 592 spiral grid(广搜)
题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=592
解决以下问题后就方便用广搜解:
1、将数字坐标化,10000坐标为(0,0),这样就可以通过数字获得其坐标
2、通过一个坐标知道在这个位置上的数字是否为素数
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
using namespace std;
#define N 10000
][];
][];
bool tprime[N];
][] = {,, ,, -,, ,-};
struct Point {
int x, y;
bool isPrime;
}c[N+];
struct Node {
int x, y, step;
bool operator == (const Node &pra) const {
return x == pra.x && y == pra.y;
}
};
void init()
{
int n = N;
, end = (int)sqrt(N+0.5), i;
)
{
for(i=start; i<=end; i++){
c[n].x = start;
c[n--].y = i;
}
; i<end; i++){
c[n].x = i;
c[n--].y = end;
}
for(i=end; i>=start; i--){
c[n].x = end;
c[n--].y = i;
}
; i>start; i--){
c[n].x = i;
c[n--].y = start;
}
start++; end--;
}
}
void initPrime()
{
memset(tprime, true, sizeof(tprime));
memset(prime, true, sizeof(prime));
prime[c[].x][c[].y] = false;
int m = sqrt(N+0.5);
; i<=m; i++){
if(tprime[i]){
for(int j=i*i; j<=N; j+=i){
tprime[j] = false;
prime[c[j].x][c[j].y] = false;
}
}
}
}
int bfs(int a, int b)
{
memset(vis, false, sizeof(vis));
Node start, target;
start.x = c[a].x; start.y = c[a].y; start.step = ;
target.x = c[b].x; target.y = c[b].y;
queue<Node>que;
que.push(start);
vis[start.x][start.y] = true;
while (!que.empty()) {
Node cur = que.front();
que.pop();
if(cur == target)
return cur.step;
; i<; i++){
];
];
|| ty< || tx>N || ty>N || prime[tx][ty] || vis[tx][ty]) continue;
vis[tx][ty] = true;
Node next; next.x = tx; next.y = ty; next.step = cur.step+;
que.push(next);
}
}
;
}
int main()
{
init();
initPrime();
;
while(cin>>a>>b){
int res = bfs(a, b);
cout<<"Case "<<(++cnt)<<": ";
){
cout<<res<<endl;
} else {
cout<<"impossible"<<endl;
}
}
;
}
nyoj 592 spiral grid(广搜)的更多相关文章
- nyoj 613 免费馅饼 广搜
免费馅饼 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 都说天上不会掉馅饼,但有一天gameboy正走在回家的小径上,忽然天上掉下大把大把的馅饼.说来gameboy ...
- NYOJ 483 Nightmare 【广搜】+【无标记】
版权声明:长风原创 https://blog.csdn.net/u012846486/article/details/31032479 Nightmare 时间限制:1000 ms | 内存限制: ...
- NYOJ 284 坦克大战 (广搜)
题目链接 描述 Many of us had played the game "Battle city" in our childhood, and some people (li ...
- nyoj 523 双向广搜
题目链接: http://acm.nyist.net/JudgeOnline/problem.php?pid=523 #include<iostream> #include<cstd ...
- nyoj 999——师傅又被妖怪抓走了——————【双广搜】
师傅又被妖怪抓走了 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 话说唐僧复得了孙行者,师徒们一心同体,共诣西方.自宝象国救了公主,承君臣送出城西,沿路饥餐渴饮,悟 ...
- hdu 1242:Rescue(BFS广搜 + 优先队列)
Rescue Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Submis ...
- UVA 10047 The Monocycle (状态记录广搜)
Problem A: The Monocycle A monocycle is a cycle that runs on one wheel and the one we will be consi ...
- UVa 1600 Patrol Robot(三维广搜)
A robot has to patrol around a rectangular area which is in a form of m x n grid (m rows and ncolumn ...
- (广搜)Fire Game -- FZU -- 2150
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82828#problem/I Fire Game Time Limit:1000MS ...
随机推荐
- DataGridView的使用和批量修改
DataGridView的属性:AllowUserToAddRows:如果为true允许用户添加行,false不允许用户添加行ReadOnly:true表示只读.不能修改单元格中的值,false可以对 ...
- AngularJS学习笔记filter
filter是对数据进行过滤操作,比如按某个字段搜索.格式化数据等等,是一个非常有用的接口.下面就简单介绍下它的用法. AngularJS自带的filter接口,|是filter的分隔符,参数用:分隔 ...
- 动动手,写个knockout的分页模板
最近一个项目用ASP.NET + knockout开发,很多列表页面都带分页,于是就有了写一个公共的分页模板的想法. 先把template写好: <script type="text/ ...
- Django解决 'ascii' codec can't encode characters in position
问题: 文件上传可以上传英文,无法上传中文的. 解决方法:对Apache进行配置 在/etc/apache2/envvars文件加上: export LANG='en_US.UTF-8'export ...
- MVC应用程序请求密码的功能(二)
MVC应用程序请求密码的功能(二) 在完成<MVC应用程序请求密码的功能(一)>http://www.cnblogs.com/insus/p/3471534.html之后,如果你照着做,所 ...
- 在一个view类里面获取viewcontroller
- (UIViewController *)viewController { for (UIView* next = [selfsuperview]; next; next = next.su ...
- iOS由ImageIO.framework实现gif的系统解码
首先先简单介绍一下gif的几个算是术语吧: frame(帧):一个gif可以简单认为是多张image组成的动画,一帧就是其中一张图片image. frameCount(帧数): 就是一个gif有多少帧 ...
- Linux网络编程(四)
在linux网络编程[1-3]中,我们编写的网络程序仅仅是为了了解网络编程的基本步骤,实际应用当中的网络程序并不会用那样的.首先,如果服务器需要处理高并发访问,通常不会使用linux网络编程(三)中那 ...
- Prolog学习:基本概念 and Asp.net与Dojo交互:仪器仪表实现
Asp.net与Dojo交互:仪器仪表实现 项目中需要用到仪器仪表的界面来显示实时的采集信息值,于是便遍地寻找,参考了fusionchart和anychart之后,发现都是收费的,破解的又没有这些功能 ...
- Cassandra
NoSQL之Cassandra 9月初听了一个讲座,演讲者是张月同学,他给我们分享了Cassandra nosql数据库,讲得很精彩,听完之后收益良多. Cassandra是一个noSQL数据库, ...