hdu4255 Sieve prime number + wide search

Source: Internet
Author: User

Mr. 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 INPUT1 49 3210 12

Sample outputcase 1:1case 2:7case 3:impossible #include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace Std;
int book[401][401];
int a[401][401];
int b[401][401];
int vis[160005];
int prime[160005];
struct node
{
int x;
int y;
int s;
} que[160005];
void Make1 ()
{
for (int i=1;i<=160001;i++)
Prime[i]=1;
PRIME[1] = 0;
for (int i = 2; I <= 160001; i++)
{
if (Prime[i])
{
for (int j = 2*i; J <= 160001; j+=i)
PRIME[J] = 0;
}
}


int x, y;
int n=400;
int tot=160000;
a[0][0]=160000;
x=0,y=0;
while (tot>1)
{
while (Y+1<n&&!a[x][y+1])
{
A[x][++y]=--tot;
}
while (X+1<n&&!a[x+1][y])
{
A[++x][y]=--tot;
}

while (Y-1>=0&&!a[x][y-1])
{
A[x][--y]=--tot;
}
while (X-1>=0&&!a[x-1][y])
{
A[--x][y]=--tot;
}
}
for (int i=0; i<400; i++)

for (int j=0; j<400; j + +)
{
if (prime[a[i][j]]==1)
B[i][j]=1;
Else
b[i][j]=0;
}
}
int main ()
{
int t1,t2;
int ans=0;
Make1 ();
while (scanf ("%d%d", &t1,&t2)!=eof)
{

int next[4][2]= {0,1,1,0,0,-1,-1,0};
memset (book,0,sizeof (book));
if (T1==T2)
printf ("Case%d:0\n", ++ans);
Else
{
int Startx,starty,endx,endy;
for (int i=0; i<=399; i++)
for (int j=0; j<=399; j + +)
{
if (A[I][J]==T1)
{
Startx=i;
Starty=j;
}
if (A[I][J]==T2)
{
Endx=i;
Endy=j;
}
}
int head=1,tail=1;
Que[head].x=startx;
Que[head].y=starty;
tail++;
Book[startx][starty]=1;
int flag=0;
while (Head<tail)
{
for (int k=0; k<4; k++)
{
int tx=que[head].x+next[k][0];
int ty=que[head].y+next[k][1];
if (tx<0| | tx>399| | ty<0| | ty>399)
Continue
if (b[tx][ty]==0&&book[tx][ty]==0)
{
Book[tx][ty]=1;
QUE[TAIL].X=TX;
Que[tail].y=ty;
que[tail].s=que[head].s+1;
tail++;
}
if (Tx==endx&&ty==endy)
{
flag=1;
Break
}
}
if (flag==1)
Break
head++;
}
if (flag==1)
printf ("Case%d:%d\n", ++ANS,QUE[TAIL-1].S);
Else
printf ("Case%d:impossible\n", ++ans);
}
}
return 0;

}

hdu4255 Sieve prime number + wide search

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.