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 ...
随机推荐
- Apple Swift中英文开发资源
Apple Swift中英文开发资源集锦[apple swift resources] 找到的一些Apple Swift中英文资源原文链接,希望对大家有所帮助.欢迎大家补充,原始资源链接最好! The ...
- MVC框架的插件与拦截器基础
自制MVC框架的插件与拦截器基础 上篇谈到我自己写的MVC框架,接下来讲讲插件及拦截器! 在处理一些通用的逻辑最好把它封装一个插件或者拦截器,以便日后可以直接拿过来直接使用.在我的框架中可以通过继承以 ...
- CompareValues标签对Model中的属性进行验证
在Asp.Net MVC中实现CompareValues标签对Model中的属性进行验证 在Asp.Net MVC中可以用继承ValidationAttribute的方式,自定制实现Model两个 ...
- EF Code First Migrations, 支持MySQL
1.Enable-Migrations 2. Add-Migration TagName 3. Update-Database 注意,每次更改cs后,注意Build,再运行命令. MySql参考: h ...
- Android系统源码导入到eclipse
1.把eclipse工程配置文件复制到android源码根目录下 cp development/ide/eclipse/.classpath ./ 2. 在android源码根目录下新建文 ...
- 关于UITextfield弹出键盘解决方案
解决的问题:当你点击一个UITextfield时,不想让其弹出键盘,如果你觉得不就是取消其第一响应者嘛,resignRespond一下不就行了嘛,确实,如果你只是在其编辑完成后让其键盘消失,那这个就够 ...
- Cracking the Coding Interview(Stacks and Queues)
Cracking the Coding Interview(Stacks and Queues) 1.Describe how you could use a single array to impl ...
- TCP为什么是个可靠的协议
一直以来,我们都被告知TCP是可靠的.但为什么是可靠的,很多人都会说“三次握手.四次挥手”.然后我们就进入一个误区:TCP可靠是因为它在建立链路时进行了“多次”地确认.然后又有人问,“多次确认就可靠了 ...
- picker控件详解与使用,(实现省市的二级联动)
picker控件详解与使用,(实现省市的二级联动) 第一步:新建一个单视图(single view)的工程, 命名为pickerTest,不要勾选下面两个选项,第一个是新版本里面的,第二个是单元测试, ...
- Mac下chrome的webapp hostadmin 快速切换host
首先是安装 app ,https://chrome.google.com/webstore/detail/hostadmin-app/mfoaclfeiefiehgaojbmncmefhdnikeg ...