Topcoder SRM 345 DIV1 250

Source: Internet
Author: User

Test instructions: At first (0,0), to go to (x, y), each time only horizontal or vertical movement. Transverse movement, if the line y is even, then only the positive direction of the x-axis movement, if it is odd, can only move in the opposite direction of the x-axis, when the vertical movement, if the line x is even, then only the y-axis positive direction, if it is odd, can only move to the y-axis in the opposite direction. Q. What is the shortest distance from the starting point to the end point?

X, y range is [ -1e6, 1e6]

Solution: At first think of BFS (think very naturally), will (0, 0), (x, y), (x, 0), (0, Y) These 4 points respectively around 9 points (including themselves) as the point of accessibility. The BFS handles the shortest distance (0, 0) to (x, y). It was later found that there was a direct method of enumeration (x, y) in which quadrant, the parity of X, y to get the answer.

The second method of code

#include <cstdio>#include <iostream>#include <cstring>#include <cmath>using namespace STD;classpathfinding{ Public:intGetdirections (intXintY) {intAns =0;if(x >=0&& y >=0)        {if((x&1)==0|| (y&1)==0) ans =ABS(x) +ABS(y);ElseAns =ABS(x) +ABS(y) +2; }Else if(x <=0&& y <=0)        {if((x&1)==1|| (y&1)==1) ans =ABS(x) +ABS(y) +2;ElseAns =ABS(x) +ABS(y) +4; }Else if(X >0&& y <0)        {if((x&1)==1|| (y&1)==0) ans =ABS(x) +ABS(y);ElseAns =ABS(x) +ABS(y) +2; }Else if(X <0&& y >0)        {if((x&1)==0|| (y&1)==1) ans =ABS(x) +ABS(y);ElseAns =ABS(x) +ABS(y) +2; }returnAns }};

The first method of code

#include <cstring>#include <iostream>#include <queue>#include <algorithm>using namespace STD;structdata{intx, y;intIdintD;} d[100010];intnd queue<Data>QintTintminidis[100010];voidPushit (intX1,intY1) { for(inti =-1; I <=1; i++) { for(intj =-1; J <=1;            J + +) {++nd;            d[nd].x = x1 + I, D[nd].y = y1 + j; D[ND].D =1e9; }    }}BOOLCanget (data d1, data D2) {if(d1.x = = d2.x) {if((D1.x &1) && d1.y >= d2.y)return true;if((D1.x &1) ==0&& d1.y <= d2.y)return true; }if(d1.y = = d2.y) {if((D1.y &1) && d1.x >= d2.x)return true;if((D1.y &1) ==0&& d1.x <= d2.x)return true; }return false;}classpathfinding{ Public:intGetdirections (intXintY) {nd =0;        Pushit (x, y); Pushit (0,0); Pushit (x,0); Pushit (0, y);        Data Nowd; Nowd.x =0, Nowd.y =0, Nowd.d =0; Q.push (NOWD); while(!q.empty ())            {nowd = Q.front (); Q.pop (); for(inti =1; I <= nd; i++) {if(Canget (Nowd, D[i])) {if(Nowd.d +ABS(nowd.x-d[i].x) +ABS(NOWD.Y-D[I].Y) < D[I].D) {D[I].D = Nowd.d +ABS(nowd.x-d[i].x) +ABS(NOWD.Y-D[I].Y);                    Q.push (D[i]); }                }            }        }intAns =1e9; for(inti =1; I <= nd; i++) {if(d[i].x = = x && d[i].y = = y)            {ans = min (ans, d[i].d); }        }returnAns }};

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Topcoder SRM 345 DIV1 250

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.