HDU-4255
A Famous Grid
Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1496 Accepted Submission(s): 567
Construct the grid like the following figure. (The grid is actually infinite. The figure is only a small part of it.)
Considering
traveling in it, you are free to any cell containing a composite number
or 1, but traveling to any cell containing a prime number is
disallowed. You can travel up, down, left or right, but not diagonally.
Write a program to find the length of the shortest path between pairs of
nonprime numbers, or report it's impossible.
each test case, display its case number followed by the length of the
shortest path or "impossible" (without quotes) in one line.
/**
题意:给出两个数,问两点之间的最短距离
做法:蛇形矩阵 + bfs + 优先队列
**/
#include <iostream>
#include <stdio.h>
#include <cmath>
#include <algorithm>
#include <string.h>
#include <queue>
#define maxn 40000
using namespace std;
int mmap[][];
int a[][];
int vis[][];
int dx[] = {,,-,};
int dy[] = {,-,,};
int n,m;
bool num[maxn];
void is_prime()
{
int tot = ;
memset(num,false,sizeof(num));
num[] = true;
for(long long i=; i<maxn; i++)
{
if(!num[i])
{
for(long long j=i*i; j<=maxn; j+=i)
{
num[j] = true;
}
}
}
}
struct Node
{
int x;
int y;
int step;
Node() {}
Node(int _x,int _y,int _step)
{
x = ;
y = ;
step =;
}
} start,endd;
struct cmp
{
bool operator () (const Node &a,const Node &b)
{
return a.step>b.step;
}
};
int check(int x,int y)
{
if(x>= && x < && y >= && y < && num[a[x][y]] == true&& !vis[x][y]) return ;
return ;
}
priority_queue<Node,vector<Node>,cmp >que;
bool bfs()
{
memset(vis,,sizeof(vis));
Node tmp,now;
while(!que.empty()) que.pop();
que.push(start);
vis[start.x][start.y] = ;
start.step = ;
while(!que.empty())
{
now = que.top();
que.pop();
//cout<<now.x<<" "<<now.y<<" "<<now.step<<endl;
if(now.x == endd.x && now.y == endd.y)
{
endd.step = now.step;
return true;
}
for(int i=; i<; i++)
{
tmp.x = now.x + dx[i];
tmp.y = now.y + dy[i];
tmp.step = now.step + ;
if(check(tmp.x,tmp.y))
{
vis[tmp.x][tmp.y] = ;
que.push(tmp);
}
}
}
return false;
}
void init()
{
int x = ;
int y = ;
int nn = ;
int num=a[][]=;
while(num>)
{
while((y+)<nn&&!a[x][y+]) a[x][++y]= --num;
while((x+)<nn&&!a[x+][y]) a[++x][y]= --num;
while((y-)>=&&!a[x][y-]) a[x][--y]= --num;
while((x-)>=&&!a[x-][y]) a[--x][y]= --num; }
}
int main()
{
//freopen("in.txt","r",stdin);
init();
is_prime();
int Case = ;
while(~scanf("%d %d",&n,&m))
{
for(int i=; i<; i++)
{
for(int j=; j<; j++)
{
if(a[i][j] == n)
{
start.x = i;
start.y = j;
}
if(a[i][j] == m)
{
endd.x= i;
endd.y = j;
}
}
}
bool prime = false;
prime = bfs();
printf("Case %d: ",Case++);
if(prime) printf("%d\n",endd.step);
else printf("impossible\n");
}
return ;
}
HDU-4255的更多相关文章
- hdu 4255 A Famous Grid
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4255 A Famous Grid Description Mr. B has recently dis ...
- HDOJ 2111. Saving HDU 贪心 结构体排序
Saving HDU Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- 【HDU 3037】Saving Beans Lucas定理模板
http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...
- hdu 4859 海岸线 Bestcoder Round 1
http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...
- HDU 4569 Special equations(取模)
Special equations Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
- HDU 4006The kth great number(K大数 +小顶堆)
The kth great number Time Limit:1000MS Memory Limit:65768KB 64bit IO Format:%I64d & %I64 ...
- HDU 1796How many integers can you find(容斥原理)
How many integers can you find Time Limit:5000MS Memory Limit:32768KB 64bit IO Format:%I64d ...
- hdu 4481 Time travel(高斯求期望)(转)
(转)http://blog.csdn.net/u013081425/article/details/39240021 http://acm.hdu.edu.cn/showproblem.php?pi ...
- HDU 3791二叉搜索树解题(解题报告)
1.题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=3791 2.参考解题 http://blog.csdn.net/u013447865/articl ...
- hdu 4329
problem:http://acm.hdu.edu.cn/showproblem.php?pid=4329 题意:模拟 a. p(r)= R'/i rel(r)=(1||0) R ...
随机推荐
- 洛谷 P1979 华容道 解题报告
P1979 华容道 题目描述 小\(B\)最近迷上了华容道,可是他总是要花很长的时间才能完成一次.于是,他想到用编程来完成华容道:给定一种局面, 华容道是否根本就无法完成,如果能完成, 最少需要多少时 ...
- Java的各种中文乱码解决方法
一.Servlet输出乱码 1. 用servlet.getOutStream字节流输出中文,假设要输出的是String str ="钓鱼岛是中国的,无耻才是日本的". 1.1 若是 ...
- 【图论】tarjan的离线LCA算法
百度百科 Definition&Solution 对于求树上\(u\)和\(v\)两点的LCA,使用在线倍增可以做到\(O(nlogn)\)的复杂度.在NOIP这种毒瘤卡常比赛中,为了代码的效 ...
- @RequestParam 注解的使用
@RequestParam 注解的使用 前言 在SpringMvc后台进行获取数据,一般是两种. 1.request.getParameter(“参数名”) 2.用@RequestParam注解获取 ...
- BI在连锁零售业应用
BI案例:BI在连锁零售业应用(ZT) Posted on 2015-08-25 09:31 xuzhengzhu 阅读(42) 评论(0) 编辑 收藏 第一部分:连锁零售企业上BI的必要性. 目前国 ...
- SQL Server作业没有执行的解决方法
SQL Server作业没有执行的解决方法 确保SQL Agent服务启动,并设置为自动启动,否则你的作业不会被执行 设置方法: 我的电脑--控制面板--管理工具--服务--右键 SQLSE ...
- SPOJ 104 HIGH - Highways
HIGH - Highways http://www.spoj.com/problems/HIGH/ In some countries building highways takes a lot o ...
- UVA 1363 Joseph's Problem
https://vjudge.net/problem/UVA-1363 n 题意:求 Σ k%i i=1 除法分块 如果 k/i==k/(i+1)=p 那么 k%(i+1)=k-(i+1)*p= k ...
- poj 3376 Finding Palindromes
Finding Palindromes http://poj.org/problem?id=3376 Time Limit: 10000MS Memory Limit: 262144K ...
- 51Nod 1014 X^2 Mod P
注意潜在范围 x*x用long long #include <bits/stdc++.h> using namespace std; typedef long long LL; #defi ...