题目连接

http://acm.hdu.edu.cn/showproblem.php?pid=4255

A Famous Grid

Description

Mr. B has recently discovered the grid named "spiral grid".
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.

Input

Each test case is described by a line of input containing two nonprime integer 1 <=x, y<=10,000.

Output

For each test case, display its case number followed by the length of the shortest path or "impossible" (without quotes) in one line.

Sample Input

1 4
9 32
10 12

Sample Output

Case 1: 1
Case 2: 7
Case 3: impossible

蛇形填数+bfs。。

#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<map>
using std::map;
using std::min;
using std::find;
using std::pair;
using std::vector;
using std::multimap;
using std::priority_queue;
#define pb(e) push_back(e)
#define sz(c) (int)(c).size()
#define mp(a, b) make_pair(a, b)
#define all(c) (c).begin(), (c).end()
#define iter(c) __typeof((c).begin())
#define cls(arr, val) memset(arr, val, sizeof(arr))
#define cpresent(c, e) (find(all(c), (e)) != (c).end())
#define rep(i, n) for(int i = 0; i < (int)n; i++)
#define tr(c, i) for(iter(c) i = (c).begin(); i != (c).end(); ++i)
const int N = 110;
int Sx, Sy, G[N][N];
bool prime[N * N + 10], vis[N][N];
const int dx[] = { 0, 0, -1, 1}, dy[] = { -1, 1, 0, 0 };
struct Node {
int x, y, s;
Node(int i = 0, int j = 0, int k = 0) :x(i), y(j), s(k) {}
inline bool operator<(const Node &x) const {
return s > x.s;
}
};
bool isPrime(int n) {
for(int i = 2; i * i <= n; i++) {
if(n % i == 0) return false;
}
return n != 1;
}
void init() {
int x = 0, y = 0, tot = N * N;
G[0][0] = N * N;
while(tot > 1) {
while(y + 1 < N && !G[x][y + 1]) G[x][++y] = --tot;
while(x + 1 < N && !G[x + 1][y]) G[++x][y] = --tot;
while(y - 1 >= 0 && !G[x][y - 1]) G[x][--y] = --tot;
while(x - 1 >= 0 && !G[x - 1][y]) G[--x][y] = --tot;
}
for(int i = 1; i <= N * N; i++) {
prime[i] = isPrime(i);
}
}
void bfs(int tar) {
cls(vis, false);
priority_queue<Node> q;
q.push(Node(Sx, Sy, 0));
vis[Sx][Sy] = true;
while(!q.empty()) {
Node t = q.top(); q.pop();
rep(i, 4) {
int x = dx[i] + t.x, y = dy[i] + t.y;
if(x < 0 || x >= N || y < 0 || y >= N) continue;
if(prime[G[x][y]] || vis[x][y]) continue;
if(G[x][y] == tar) { printf("%d\n", t.s + 1); return; }
q.push(Node(x, y, t.s + 1));
vis[x][y] = true;
}
}
puts("impossible");
}
void solve(int n, int m, int &k) {
rep(i, N) {
rep(j, N) {
if(G[i][j] == n) Sx = i, Sy = j;
}
}
printf("Case %d: ", k++);
bfs(m);
}
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w+", stdout);
#endif
init();
int n, m, k = 1;
while(~scanf("%d %d", &n, &m)) {
solve(n, m, k);
}
return 0;
}

hdu 4255 A Famous Grid的更多相关文章

  1. HDU 4256 The Famous Clock

    The Famous Clock Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  2. HDU 4253 Two Famous Companies

    Two Famous Companies Time Limit: 15000ms Memory Limit: 32768KB This problem will be judged on HDU. O ...

  3. HDU 4251 The Famous ICPC Team Again 主席树

    The Famous ICPC Team Again Problem Description   When Mr. B, Mr. G and Mr. M were preparing for the ...

  4. HDU 4294 A Famous Equation(DP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4249 题目大意:给一个a+b=c的表达式,但是a.b.c中部分位的数字丢失,并用?代替,问有多少种方案 ...

  5. HDU 4248 A Famous Stone Collector 组合数学dp ****

    A Famous Stone Collector Time Limit: 30000/15000 MS (Java/Others)    Memory Limit: 32768/32768 K (Ja ...

  6. HDU 4251 The Famous ICPC Team Again(划分树)

    The Famous ICPC Team Again Time Limit: 30000/15000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

  7. hdu 4251 The Famous ICPC Team Again划分树入门题

    The Famous ICPC Team Again Time Limit: 30000/15000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

  8. [ACM] hdu 4248 A Famous Stone Collector (DP+组合)

    A Famous Stone Collector Problem Description Mr. B loves to play with colorful stones. There are n c ...

  9. hdu 4253 Two Famous Companies BZOJ 2654 tree

    [题意]:给出n个点,m条边,边分为两种,一种是A公司的,一种是B公司的.边上有权值,问用n-1条边把n个点连起来的最小费用是多少,其中A公司的边刚好有k条.题目保证有解. 思路:我们发现,如果我们给 ...

随机推荐

  1. Flash Builder 4.6 BUG 远程访问受阻

    今天调试项目的时候,惊讶的发现在使用RemoteObject进行远程访问时出现奇怪现象,只能在服务器本地实现访问,在其他客户机上提示2048错误,send failed,差点没把我吓死,记得之前测试过 ...

  2. MSP430F149学习之路——按键与LED

    代码 /*********************************** 程序功能:用按键控制LED状态 缺点:按键按下后无法改变LED灯的状态,可以利用板上的复位键^~^ ********** ...

  3. 解决wamp mysql数据库出现乱码的问题。

    一般的乱码情况: 如果在控制台上出现這样的乱码,一般在phpmysqladmin上也会出现乱码,因为他们都一样 一个在控制台出现,一个在页面出现. 首先在mysql.exe上输出 mysql>S ...

  4. HTML DOM(学习笔记二)

    嗯,在HTML DOM(学习笔记一)中简单描述了一下HTML DOM 是什么,这一篇将记录下来有关HTML DOM的内容! 1:DOM节点 首先,再来看一下HTML DOM的树状结构,如下图所示: 这 ...

  5. vimdiff vimmerge 配置及使用

    1 Set up vimdiff The vimdiff as a merge tool will display several buffers to show the yours/theirs/o ...

  6. 华为OJ平台——输出最小的k个数

    输入n个整数,输出其中最小的k个. 详细描述: 接口说明 原型: bool GetMinK(unsignedint uiInputNum, int *pInputArray, unsignedint ...

  7. linux下的tomcat自动退出的问题

    1,环境:mysql+tomcat+linux 2,发现问题:连接池断开连接 No operations allowed after connection closed 连接池断开了,再进行连接就报错 ...

  8. ajax contenttype

    jquery ajax基本形式: $.ajax({ type: 'POST/get', url: '', data: {"n":n,"m":m}, dataTy ...

  9. dell ipmi sol

    http://blog.arnoudvermeer.nl/post/52375062605/howto-setup-ipmi-sol-on-a-dell-r-series-server http:// ...

  10. ANTLR4权威参考手册

    ANTLR4权威参考手册(一) - 活在当下 乐在其中 - 博客频道 - CSDN.NET ANTLR4权威参考手册