HDU 4255 a famous grid-analog-sieve prime number-BFS

Source: Internet
Author: User
Tags inif
# Include <stdio. h> # include <string. h ># include <queue> using namespace STD; int S, T; struct node {int X, Y;} Shu [40001]; node SS, TT; int Suy [40001]; int map [201] [201]; int Fang [201]; void inif () // calculates the number of workers {for (INT I = 1; I <= 200; I ++) Fang [I] = I * I;} void Xu (int I) // calculates the position of the digit I {int L, R; for (r = 2; r <= 200; ++ R) if (I <Fang [R]) break; L = R-1; If (L % 2) {If (I-L * l) <= (R * r-l * l)/2) {Shu [I]. X = Shu [L * l]. x-(I-L * l) + 1; Shu [I]. y = Shu [L * l]. Y + 1 ;} Else {Shu [I]. X = Shu [R * r]. x; Shu [I]. y = Shu [R * r]. Y + R * r-I;} else {If (I-L * l) <= (R * r-l * l)/2) {Shu [I]. X = Shu [L * l]. X + I-l * l-1; Shu [I]. y = Shu [L * l]. y-1;} else {Shu [I]. X = Shu [R * r]. x; Shu [I]. y = Shu [R * r]. y-(R * r-I) ;}} void INI () // initialize {inif (); int I, Ju; Shu [1]. X = 101; Shu [1]. y= 100; Shu [4]. X = 100; Shu [4]. y = 100; for (I = 3; I <= 200; I ++) {if (I % 2) {ju = (I-1)/2; shu [I * I]. X = Shu [1]. X + Ju; Shu [I * I]. y = Shu [1]. Y + Ju;} else {ju = (I-2)/2; Shu [I * I]. X = Shu [4]. x-ju; Shu [I * I]. y = Shu [4]. y-ju ;}for (I = 2; I <= 40000; ++ I) if (Shu [I]. X = 0) XU (I);} void SU () // screen prime number {int I, j; memset (Suy, 0, sizeof (Suy )); suy [1] = 1; for (I = 2; I <= 200; I ++) {If (Suy [I] = 0) {for (j = I + I; j <= 40000; j = J + I) Suy [J] = 1; // The number is 1} memset (MAP, 0, sizeof (MAP);} struct qnode {int X, Y, N ;}; int V [200] [200]; int dir [] [2] =, 0, 1, 0, 0,-1}; int DFS () // find the shortest path {int K; queue <qnode> q; qnode Cur, next; cur. X = ss. x; cur. y = ss. y; cur. n = 0; memset (v, 0, sizeof (v); V [cur. x] [cur. y] = 1; q. push (cur); While (! Q. empty () {cur = Q. front (); q. pop (); For (k = 0; k <4; k ++) {next. X = cur. X + dir [k] [0]; next. y = cur. Y + dir [k] [1]; next. N = cur. n + 1; if (next. X = TT. X & next. y = TT. y) return next. n; If (next. x> = 1 & next. X <=200 & next. y> = 1 & next. Y <= 200 & map [next. x] [next. y] = 0 & V [next. x] [next. y] = 0) {v [next. x] [next. y] = 1; q. push (next) ;}}return-1;} void biaosu () // mark the prime number {int I, X, Y; for (I = 1; I <= 40000; ++ I) {If (! Suy [I]) {x = Shu [I]. x; y = Shu [I]. y; Map [x] [Y] = 1 ;}} int main () {int ret, CAS = 1; INI (); SU (); biaosu (); while (scanf ("% d", & S, & T )! = EOF) {Ss = Shu [s]; TT = Shu [T]; ret = DFS (); If (ret =-1) printf ("case % d: impossible \ n ", CAS ++); else printf (" case % d: % d \ n ", CAS ++, RET);} return 0 ;}

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.