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 ...
随机推荐
- viewstate cookie和session原理回顾
--个人理解会存在些错误仅供参考!!! ----浏览器保持会话状态原理 用户发送一次请求,服务器端会检索报文中是否存在sessionid不存在,就分配一个写到cookie当中,存在浏览器的缓存中,当再 ...
- Ubuntu(Linux) + mono + jexus +asp.net MVC3
Ubuntu(Linux) + mono + jexus +asp.net MVC3 部署 感谢 张善友 的建议,我把 微信订餐 由nginx 改成 jexus,目前运行状况来说,确实稳定了很多, ...
- C#动态表达式计算(续2)
上两篇废话太多,这一次我就不多说了,由于代码比较简单,可以直接从https://github.com/scottshare/DynamicExpress.git地址下载. 以下说明一下使用方法: Dy ...
- IIS发布网站,浏览网站时候,出现 试图加载格式不正确的程序。
异常来自HRESULT:0x8007000B 缘由:在64位操作系统下IIS发布32位的项目,报“项目依赖的dll无法读取,试图加载格式不正确的程序”错误. 原因:程序集之间的通讯要么全是64位环境下 ...
- exit与_exit
一.main函数: 先从程序的执行开始谈起,C程序总是从main函数开始执行,当内核执行C程序时(使用了一个exec函数),在调用main函数前先调用一个特殊的启动例程.可执行程序 文件将此启动例程指 ...
- 【转】Objective-C并发编程:API和挑战
并发指的是在同一时间运行多个任务.在单核CPU的情况下,它通过分时的方式实现,如果有多个CPU可用,则是真正意义上的多个任务“并行”执行了. OS X和iOS提供了多个API支持并发编程.每个API都 ...
- VIJOS1107 求树的最长链
vijos1107环游大同80天 学习了一下求树的最长链的方法 最简单的思路就是两次dfs 两次dfs分别有什么用呢? 第一次dfs,求出某个任意的点能到达的最远的点 第二次dfs,从所搜到的最远的点 ...
- discuz X3.1的门户文章实现伪静态,利于搜索引擎收录url的地址修改
最近在捣鼓DZ框架,这两天发现文章的收录情况并不是太理想,做了很多优化方面的工作,今天主要解决了DZ门户的文章链接伪静态化,在这次修改之前,也做过一次在网上找的静态化修改,之前做的方式是: 1.在DZ ...
- linux不同环境下c/c++程序移植方法
这边之前的大多数项目都用的java,而自己用的c++,等到快要上线的时候才发现线上机器的gcc和libc的版本都巨低,跟自己测试开发用的环境不兼容,编译出的c++可执行文件没法运行.解决c++程序的移 ...
- Linux下搭建Nginx+MySQL+PHP
在CentOS6.3下实践通过. 1.关闭防火墙 [root@CentOS ~]# chkconfig iptables off 2.关闭selinux vi /etc/sysconfig/selin ...