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 ...
随机推荐
- SZU:A26 Anagram
Judge Info Memory Limit: 32768KB Case Time Limit: 10000MS Time Limit: 10000MS Judger: Normal Descrip ...
- iOS基础 - UITableView的数据源和代理
一.UITableView的代理方法 #pragma mark 每一行的高度 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtI ...
- Windows 8 Store Apps
重新想象 Windows 8 Store Apps 系列文章索引 Posted on 2013-11-18 08:33 webabcd 阅读(672) 评论(3) 编辑 收藏 [源码下载] 重新想象 ...
- Couchbase集群和Redis集群解析
Couchbase集群和Redis集群解析 首先,关于一些数据库或者是缓存的集群有两种结构,一种是Cluster;一种是master-salve. 关于缓存系统一般使用的就是Redis,Redis是开 ...
- IOC and DI
Spring.Net 技术简介 IOC and DI 一 简单介绍 IOC 控制转移,就是将创建放到容器里,从而达到接耦合的目的,DI是 在容器创建对象的时候,DI读取配置文 ...
- django下载文件
赶快记录一下写的一个django下载文件的例子,以便以后复习: 在views.py中设置 from django.core.servers.basehttp import FileWrapper im ...
- z-index 解析
很多人对z-index的认识仅止于:z-index是控制元素在页面中的堆叠顺序,z-index值高的元素显示在z-index值低的前面.而其中的原因才很少有人去深究,直到自己在实际项目中碰到由于z-i ...
- cocos2d-x 通过socket实现http下载及断点续传的实现
cocos2d-x 通过socket实现http下载及断点续传的实现 代码未经进一步的整理,可能比较混乱. 首先,2dx的socket库由BSSocket组成.可跨平台,在windows上已验证. 1 ...
- Android Widget(窗口小部件)
Android Widget简介 应用程序窗口小部件(Widget)是微型的应用程序视图,它可以被嵌入到其它应用程序中(比如桌面)并接收周期性的更新.你可以通过一个App Widget Provide ...
- Exchange 2016 创建日程,远程服务器返回错误: (401) 未经授权
背景:在测试环境中一切正常,使用的是Exchange2013,上生产环境Exchange2016,部署后产生如下错误: Microsoft.Exchange.WebServices.Data.Serv ...