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 ...
随机推荐
- Introducing ASP.NET vNext and MVC 6
[译]Introducing ASP.NET vNext and MVC 6 原文:http://www.infoq.com/news/2014/05/ASP.NET-vNext?utm_source ...
- JavaScript实例技巧精选(11)—计算器实例3
>>点击这里下载完整html源码<< 界面如下 将以下代码插入<body></body>中 <FORM NAME="Calc" ...
- 新认识:SDF数据库
新认识:SDF数据库 一.SDF数据库初探 SDF是一个标准缩略数据库格式.这个数据库包含扩展名为.sdf的文件并且以结构化文件格式进行数据存储.这些SDF文件通常用于在不同数据库应用之间移动数据.它 ...
- poj3083走玉米地问题
走玉米地迷宫,一般有两种简单策略,遇到岔路总是优先沿着自己的左手方向,或者右手方向走.给一个迷宫,给出这两种策略的步数,再给出最短路径的长度. ######### #.#.#.#.# S....... ...
- MEF只导出类的成员
MEF只导出类的成员 通过前面两篇文章的介绍,相信各位会明白MEF中有不少实用价值.上一文中我们也讨论了导入与导出,对于导出导入,今天我们再深入一点点,嗯,只是深入一点点而已,不会很难的,请大家务必放 ...
- 解决中文乱码( jsp表单提交中文时出现乱码)
有三种方法: 1.建立一个filter中文解决乱码 2.Struts2在struts.xml中修改默认的编码设定 3.用Spring解决中文乱码 4.直接在jsp中修改解决 1.建立一个filter解 ...
- .Net用户使用期限的设置、限制通用小组件
.Net用户使用期限的设置.限制通用小组件 最近比较项目组的同事都比较烦,不断的穿梭在不同的项目之间,一个人同时要兼顾多个项目的维护修改.甚至刚放下这个客户的电话,另一个客户的电话就进来了.究其原因, ...
- Dragon Balls(hdu3635带权并查集)
题意:n个城市有n个龙珠,T a,b 代表将龙珠a移动到b城市,Q a代表查询龙珠a所在城市,该城市有多少颗龙珠,该龙珠移动了多少次. 注意:移动时是将龙珠所在该城市所有龙珠都移动,每个龙珠不会再回 ...
- 一种利用异常机制基于MVC过滤器的防止重复提交的机制分享
防止重复提交验证机制 某些时候因为系统反应稍慢,急性子用户可能不耐烦会进行重复的提交,这个操作不仅可能造成系统负担,也可能产生垃圾数据. 出现这两种状况都是我们不希望的. 为此,在公司项目系统设计了以 ...
- [置顶] 和孩子们一起学Python编程
1. 推荐书名 Computer Programming for Kids and Other Beginners in Python, 4Ed.pdf 中文译名:<和孩子们一起学Pyt ...