HDU ACM 4255 A Famous Grid

Source: Internet
Author: User
Tags value store

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 descriptionmr. B has recently discovered the grid named "Spiral Grid".
Construct the grid like the following figure. (The grid is actually infinite. The figure was only a small part of it.)


Considering traveling in it, you is free to any cell containing a composite number or 1, but traveling to any cell contai Ning a prime number is disallowed. You can travel up, down, left or right, and not diagonally. Write a program to find the length of the shortest path between pairs of nonprime numbers, or report it ' s impossible.


Inputeach test case was described by a line of input containing the nonprime integer 1 <=x, y<=10,000.

Outputfor Each test case, display its case number followed by the length of the shortest path or "impossible" (without quo TES) in one line.

Sample Input
1 49 3210 12

Sample Output
Case 1:1case 2:7case 3:impossible

Sourcefudan Local Programming Contest 2012 today's harvest is very good! This afternoon independent A has two more complicated search questions. Tired of my shoulder neck pain ah, at the end of the night Experiment class playground ran a few laps no use, backache hurt Ah! talk about this problem, at that time training game very clearly know how to do, but after all, their code ability is not strong, do not write out to worry Ah! From that day is very clear, do the problem too little, time can not be wasted on debug. this week is going to save the game ~ ~ thought that last semester to start ACM should be better than now, after writing blog also have to hang net class, immediately net lesson will end, a section has not been. OK ~, put the code here ~ ~ There is a problem everybody exchanges ~ ~
<span style= "FONT-SIZE:18PX;"       > #include <iostream> #include <string.h> #include <queue>using namespace std;const int m=110; Setting a global constant is very convenient 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}};// Control four direction of struct node{int x,y,step;};    void prime ()//print prime Table {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 ()//Initialize the squares, place the primes all at 0, and the non-primes by value store {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));                 initialize Vis; vis[x][y]=1;    The first point mark has been visited.    int ok=1;        while (!dict.empty ()) {Cur=dict.front ();           Dict.pop ();    When the bug was adjusted, it was found to pull the next time, resulting in an infinite loop 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])//                Whether the adjacency nodes of the current node satisfy the access condition if the update adjacency node attribute {vis[tx][ty]=1 is satisfied;                NEXT.X=TX;                Next.y=ty;                next.step=cur.step+1;                  if (cnt[next.x][next.y]==e) {ok=0;                    Successfully found with OK tag no longer loops cout<< "case" <<++t<< ":" <<next.step<<endl;                Break            } dict.push (next);    }} if (!ok) break; if (OK)//When the queue is empty or not found, then ... cout<< "case" <<++t<< ":" << "impossible" &LT;&LT;ENDL;}    int main () {int si,sj;    Prime ();    Inti (); while (cin>>s>>e) {if (s==e)//This is a fatal mistakeError, the last bug is here {cout<<0<<endl;        Continue        } int flag=1; for (int i=0;; i++)//Search lock start position {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>

HDU ACM 4255 A Famous Grid

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.