HDU 4255 A Famous Grid 素数+BFS
打素数表,构造矩阵,然后BFS可得答案
#include <set>
#include <map>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <iostream>
#include <algorithm>
using namespace std;
int max(int a,int b){return a<b?b:a;}
int min(int a,int b){return a>b?b:a;}
const int maxn=200;
bool p[maxn*maxn*2];
int x[maxn+5][maxn+5];
int y[maxn+5][maxn+5];
bool way[maxn][maxn];
int xx[4]={0,0,1,-1};
int yy[4]={1,-1,0,0};
struct node
{int x,y,dis;node(int a,int b,int c){x=a;y=b;dis=c;}
};
void prime()
{int i=0,j=0,a=maxn,k=a*a;p[1]=1;for(i=2;i<=maxn*maxn;i++){for(j=2*i;j<=maxn*maxn;j+=i){if(p[i]==0)p[j]=1;}}i=-1,j=0;while(k!=0){for(i++;i<a && x[j][i]==0;i++){x[j][i]=k--;}i--;for(j++;j<a && x[j][i]==0;j++){x[j][i]=k--;}j--;for(i--;i>=0 && x[j][i]==0;i--){x[j][i]=k--;}i++;for(j--;j>=0 && x[j][i]==0;j--){x[j][i]=k--;}j++;}for(j=0;j<a;j++)// i 是 x , j 是 y{for(i=0;i<a;i++){if(p[x[j][i]]==1){y[j][i]=1;}}}
}int main()
{prime();int i,j,a,b,t=1;while(cin>>a>>b){memset(way,0,sizeof(way));int sx,sy,ex,ey;for(i=0;i<maxn;i++){for(j=0;j<maxn;j++){if(x[j][i]==a){sx=i;sy=j;}if(x[j][i]==b){ex=i;ey=j;}}}bool flag=true;queue<node> q;while(!q.empty())q.pop();q.push(node(sx,sy,0));way[sy][sx]=true;while(!q.empty()) { if(q.front().x==ex && q.front().y==ey){flag=false;break;}if(q.front().x >= 0 && q.front().y >=0) { for(i=0;i<4;i++) { if(y[q.front().y + yy[i] ][q.front().x + xx[i] ] == 1 && way[q.front().y + yy[i] ][q.front().x + xx[i]] == false) { way[q.front().y + yy[i] ][q.front().x + xx[i]] = true; q.push(node( q.front().x + xx[i],q.front().y + yy[i],q.front().dis+1)); } } q.pop(); }else q.pop(); }if(flag)cout<<"Case "<<t<<": impossible"<<endl;else cout<<"Case "<<t<<": "<<q.front().dis<<endl;t++;}return 0;
}
转载于:https://www.cnblogs.com/Felix-F/archive/2012/12/22/3223647.html
HDU 4255 A Famous Grid 素数+BFS相关推荐
- 【HDU 2612 Find a Way(BFS)】(兼BFS入门笔记)
[HDU 2612 Find a Way(BFS)](兼BFS入门笔记) 原题入口: http://acm.hdu.edu.cn/showproblem.php?pid=2612 第一篇在CSDN的博 ...
- HDU 6386 Age of Moyu DFS+BFS
/** HDU 6386 Age of Moyu DFS+BFS 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6386题意:1-->n的最小换乘次数 ...
- HDU-4255 A Famous Grid BFS
先生成蛇型矩阵,然后再筛选出素数进行标记,最后bfs.这里要注意题目要求的1-10000的之间的数路径,但是并不代表我们只要打印到这个范围的素数,因为很可能边沿的点需要走到外面的图形来完成对短路,外围 ...
- HDU 1429 胜利大逃亡(续) (BFS+位压缩)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1429 胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others) ...
- HDU - 5637 Transform (思维、bfs预处理)
HDU - 5637 题目大意: 给出n个数的序列a,对于一个整数x,有两种操作: 1.改变x二进制中任一位 2.将x变为x^a[i] m次查询,每次查询输入两个整数x和y,问x最少经过多少次操作可以 ...
- HDU 2612 Find a way(BFS)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2612 题目大意:给你一张n*m的图,图上有两个点Y.M,和若干个点@,找出某个点@使Y.M到这里的距离 ...
- HDU Problem - 3085 Nightmare Ⅱ(双向BFS)
题目链接 Problem Description Last night, little erriyue had a horrible nightmare. He dreamed that he and ...
- HDU多校7 - 6853 Jogging(bfs+结论)
题目链接:点击查看 题目大意:在二维平面中有一个点 ( x , y ) ,规定 " 好点 " 的定义是,gcd( x , y ) > 1 ,现在从点 ( x , y ) 开始 ...
- HDU 4069 Squiggly Sudoku 【DLX+BFS】
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4069 ★这题一开始题都看不懂,后来发现还是挺有意思的 题意: 给你一个9x9的矩阵, 矩阵里面有一些墙 ...
最新文章
- MyBatis原理分析之四:一次SQL查询的源码分析
- python 多线程爬虫 实例
- Xposed源码剖析——Xposed初始化
- MySQL的基本语法
- jQuery的三种bind/One/Live事件绑定使用方法
- python3 与 Django 连接数据库:Error loading MySQLdb module: No module named 'MySQLdb'
- poj 3080 Blue Jeans
- C# WPF MVVM项目实战(进阶①)
- “兼职”运维的常用命令
- 启动redis闪退/失败
- LAMP平台架构浅析
- php计算器如何保留输入数字,php如何实现计算器代码
- 【HDU 6020】 MG loves apple (乱搞?)
- 华为服务器系统启动项,服务器设置开机启动项
- HttpClient下载图片
- flashfxp链接Linux
- matlab 直流无刷电机,无刷直流电机的matlab仿真.pdf
- H.264区分NALU startCode和NALU 内部和startCode相同的内容
- [文献精读] Summit:A Simulator for Urban Driving
- 三种数据库的 SQL 注入详解