hdu 4255 A Famous Grid

题目连接

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;
}
时间: 2024-08-07 14:58:43

hdu 4255 A Famous Grid的相关文章

HDU ACM 4255 A Famous Grid

A Famous Grid Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1562    Accepted Submission(s): 603 Problem Description Mr. B has recently discovered the grid named "spiral grid". Construct

hdu 1705 Count the grid(皮克定理)

题目链接:hdu 1705 Count the grid 题意: 给定一个三角形三点坐标,问三角形内有多少个坐标均为整数的点. 题解: 给定顶点坐标均是整点(或正方形格点)的简单多边形,皮克定理说明了其面积 S 和内部格点数目 n.边上格点数目 s 的关系:S = n +s/2+1 三角形两向量叉积/2=面积. 向量上整数点数为gcd(v.x,v.y)(此公式对于一条边上的结果不准确,但是三条边加在一起的和是准确的) 1 #include<bits/stdc++.h> 2 #define F(

ZOJ 2994 &amp;&amp; HDU 1992 Tiling a Grid With Dominoes (状压DP)

题目链接:HDU 1992 Tiling a Grid With Dominoes 题意:一个4*N的矩形,用1*2的小矩形铺满的方法数是多少. 思路:4*N.只有4行想到状压,dp[i][j]表示前i行状态j的方法数,影响当前行的只有上一行!0成对出现表示横着放,1表示竖着放,所以第一行的状态0.3.9.12.15五种,并且只要上一行是0状态.当前行的状态就为0.3.9.12.15五种可能.还有当前行s1和上一行s2匹配仅当s1==0 || s1==(s2取反),特别注意上一行状态是0110(

[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 colors of stones in his collection. Two stones with the same color are indistinguishable. Mr. B would like to select some stones and arrange them in li

HDU 4256 The Famous Clock

The Famous Clock Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1399    Accepted Submission(s): 940 Problem Description Mr. B, Mr. G and Mr. M are now in Warsaw, Poland, for the 2012's ACM-ICPC

HDU 4253 Two Famous Companies

Two Famous Companies Time Limit: 15000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 425364-bit integer IO format: %I64d      Java class name: Main In China, there are two companies offering the Internet service for the peo

HDU 4252 A Famous City

A Famous City Time Limit: 3000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 425264-bit integer IO format: %I64d      Java class name: Main After Mr. B arrived in Warsaw, he was shocked by the skyscrapers and took several p

HDU 1992 Tiling a Grid With Dominoes (状压 dp)

Problem Description We wish to tile a grid 4 units high and N units long with rectangles (dominoes) 2 units by one unit (in either orientation). For example, the figure shows the five different ways that a grid 4 units high and 2 units wide may be ti

HDU 4247 A Famous ICPC Team

Problem Description Mr. B, Mr. G, Mr. M and their coach Professor S are planning their way to Warsaw for the ACM-ICPC World Finals. Each of the four has a square-shaped suitcase with side length Ai (1<=i<=4) respectively. They want to pack their sui