Hdus ACM 4255 A Famous Grid

Source: Internet
Author: User

Hdus ACM 4255 A Famous Grid
A Famous GridTime 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 49 3210 12

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

Source Fudan Local Programming Contest 2012 What we got today is amazing! In the afternoon, I had two more complex search questions. My shoulder hurts and my neck hurts. I ran a few laps on the playground in the lab class at night! Let's talk about this question. At that time, I knew exactly how to do it during the training competition, but after all, I was not very competent in my code and could not write it out! From that day, it was clear that there were too few questions to be done, and the time should not be wasted on debug. This week is about to save the competition ~~ I thought that acm should be much better at the beginning of last semester. I still have to take online courses after I write my blog, and my online courses are about to end. Okay ~, Put the code here ~~ If you have any questions, please contact us ~~
# Include
 
  
# Include
  
   
# Include
   
    
Using namespace std; const int M = 110; // It is very convenient to set a global constant int cnt [M] [M]; int primer [M * M]; int vis [M] [M]; int s, e, t = 0; int dir [] [2] = {0, 1 }, {0,-1 }}; // control the four direction struct node {int x, y, step ;}; void prime () // print the prime number 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 square, set all prime numbers to 0, and store non-prime numbers by value {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
    
     
= 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
     
      
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; // mark the first vertex that has been accessed. Int OK = 1; while (! Dict. empty () {cur = dict. front (); dict. pop (); // when debugging the bug, it is found that pulling this next time leads to 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
      
     
    
   
  
 

Related Article

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.