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 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

Source

Fudan Local Programming Contest 2012

今天收获还算非常棒!下午独立a了两个稍微复杂点儿的搜索题。累的我肩旁脖子疼啊,晚上上完实验课操场跑了好几圈没啥用,腰酸背疼啊 !

说说这个题,当时训练赛的时候很明确知道怎么做,但毕竟自己代码能力不强,写不出来干着急啊 !从那天就很明确,做的题目太少,时间不能浪费在debug上。

这周就要省赛了~~心想上学期要开始acm 应该会比现在好的多,写完博客还得挂网课 ,马上网课就要结束了 ,一节还没上过。好了~,把代码放这了~~有问题大家交流~~

<span style="font-size:18px;">#include<iostream>
#include<string.h>
#include<queue>
using namespace std;

const int  M=110;       //  设定一个全局常量非常方便
int cnt[M][M];
int primer[M*M];
int vis[M][M];
int s,e,t=0;
int dir[][2]= {{0,1},{1,0},{-1,0},{0,-1}};//控制四个方向

struct node
{
    int x,y,step;
};

void prime()           //  打印素数表
{
    memset(primer,0,sizeof(primer));
    for(int i=2; i<=M; i++)
    {
        if(!primer[i])
            for(int j=i*i; j<=M*M; j+=i)
            {
                primer[j]=1;
            }
    }
    primer[1]=1;
}

void inti()                 //初始化方格,把素数全都置为0,非素数按值存储
{

    memset(cnt,0,sizeof(cnt));
    int tot=M*M;
    int x,y;
    x=0;
    y=0;
    cnt[0][0]=tot;
    while(tot>1)
    {

        while(y+1<M&&!cnt[x][y+1])
        {
            if(!primer[--tot])
            {
                cnt[x][++y]=0;

            }
            else
            {
                y++;
                cnt[x][y]=tot;

            }
        }

        while(x+1<M&&!cnt[x+1][y])
        {

            if(!primer[--tot])
            {
                cnt[++x][y]=0;
            }
            else
            {
                x++;
                cnt[x][y]=tot;
            }

        }

        while(y-1>=0&&!cnt[x][y-1])
        {
            if(!primer[--tot])
            {
                cnt[x][--y]=0;
            }
            else
            {
                y--;
                cnt[x][y]=tot;
            }
        }

        while(x-1>=0&&!cnt[x-1][y])
        {

            if(!primer[--tot])
            {
                cnt[--x][y]=0;
            }
            else
            {
                x--;
                cnt[x][y]=tot;
            }
        }

    }
}

void bfs(int x,int y)
{
    queue<node>dict;
    node cur,next;
    cur.x=x;
    cur.y=y;
    cur.step=0;
    dict.push(cur);
    memset(vis,0,sizeof(vis)); // 初始化vis;
    vis[x][y]=1;                 //第一个点标记已经访问过。
    int ok=1;

    while(!dict.empty())
    {
        cur=dict.front();
        dict.pop();               //调bug时发现拉下一次这个,结果导致无限循环
        for(int i=0; i<4; i++)
        {
            int tx=cur.x+dir[i][0];
            int ty=cur.y+dir[i][1];
            if(tx>=0&&ty>=0&&tx<M&&ty<M&&cnt[tx][ty]&&!vis[tx][ty])//当前节点的邻接结点是否满足访问条件若满足更新邻接结点属性
            {
                vis[tx][ty]=1;
                next.x=tx;
                next.y=ty;
                next.step=cur.step+1;
                if(cnt[next.x][next.y]==e)
                {
                    ok=0;                  // 用ok标记成功找到 不再进行循环
                    cout<<"Case "<<++t<<": "<<next.step<<endl;
                    break;
                }

                dict.push(next);

            }

        }
        if(!ok)
            break;
    }
    if(ok)//当队列为空的时候还是没有找到,那么……
        cout<<"Case "<<++t<<": "<<"impossible"<<endl;
}

int main()
{
    int si,sj;
    prime();
    inti();
    while(cin>>s>>e)
    {
        if(s==e)     //这是一个致命的错误,最后一个bug出在这里
        {
            cout<<0<<endl;
            continue;
        }
        int flag=1;
        for(int i=0; ; i++)        // 搜锁起始位置
        {
            for(int j=0; j<M; j++)
            {
                if(cnt[i][j]==s)
                {
                    flag=0;
                    si=i;
                    sj=j;
                    break;
                }
            }
            if(!flag)
                break;
        }

        bfs(si,sj);

    }
    return 0;
}
</span>
时间: 2024-12-19 20:42:47

HDU ACM 4255 A Famous Grid的相关文章

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 par

杭电HDU ACM Uncle Tom&#39;s Inherited Land*(二分图匹配 建模)

Uncle Tom's Inherited Land* Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 2496    Accepted Submission(s): 1028 Special Judge Problem Description Your old uncle Tom inherited a piece of land f

HDU ACM 1103 Flo&#39;s Restaurant

分析:借助STL的min_element实现.每次更新最先被占用的桌子,具体见注释. #include<iostream> #include<algorithm> using namespace std; int main() { int A,B,C; char s[10]; int a[102],b[102],c[102]; int curtime,count,ans; int *p; //桌子最先空闲时间 while(cin>>A>>B>>C

hdu acm 1425 sort(哈希表思想)

sort Time Limit: 6000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 25803    Accepted Submission(s): 7764 Problem Description 给你n个整数,请按从大到小的顺序输出其中前m大的数. Input 每组测试数据有两行,第一行有两个数n,m(0<n,m<1000000),第二行包含n个各不相同,且

HDU ACM 1005 Number Sequence

Number Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 119732    Accepted Submission(s): 29072 Problem Description A number sequence is defined as follows:f(1) = 1, f(2) = 1, f(n) = (A

hdu acm 1166 敌兵布阵 (线段树)

敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 37903    Accepted Submission(s): 15985 Problem Description C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了.A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任务

HDU ACM 1025 Constructing Roads In JGShining&amp;#39;s Kingdom-&amp;gt;二分求解LIS+O(NlogN)

#include<iostream> using namespace std; //BFS+优先队列(打印路径) #define N 500005 int c[N]; int dp[N]; //dp[i]保存的是长度为i的最长不降子序列的最小尾元素 int BS(int n,int x) //二分查找下标,当x比全部元素小时下标为1,比全部元素大时下标为n+1. { int low,high,mid; low=1,high=n; while(low<=high) { mid=(low+h

杭电 HDU ACM 圆桌会议

圆桌会议 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3356    Accepted Submission(s): 2351 Problem Description HDU ACM集训队的队员在暑假集训时经常要讨论自己在做题中遇到的问题.每当面临自己解决不了的问题时,他们就会围坐在一张圆形的桌子旁进行交流,经过大家的讨论后一般没有

杭电OJ(HDU)-ACM Steps-Chapter Two-《Biker&#39;s Trip Odometer》《Climbing Worm》《hide handkerchief》《Nasty Hac》

1.2.1 Biker's Trip Odometer #include<stdio.h> #include<math.h> const double PI=acos(-1.0); /* 计算题,根据公式做就行,PI*d*r/(12*5280);res1/t*3600; Sample Input 26 1000 5 27.25 873234 3000 26 0 1000 Sample Output Trip #1: 1.29 928.20 Trip #2: 1179.86 1415