"Extremum problem" "cf1063b" Labyrinth

Source: Internet
Author: User

Transmission Door

Description

give you a \ (n~\times~m\) matrix, at the beginning you are in section \ ( r\) row ( c\) column. Your up and down moves are unrestricted, move up \ (x\) times to the left, and move up to the right (y\) times. How many points can I ask you? Includes the starting point.

Input

The first line is \ (n\) and \ (m\), which represents the size of the matrix.

The second line is \ (r\) and \ (c\), which represents your location

The third line is \ (x\) and \ (y\), which represents the move limit

The following \ (n\) lines per line \ (m\) characters, with and only '. ' and '' two kinds. If the section \ (i\) ( j\) column is "' means you cannot pass this point.

Output

One line of output indicates the maximum number of points that can be reached

Sample Input
4 53 21 2......***....***....
Sample Output
10
Hint

\ (for~all:\)

\ (0~\leq~n,m,r,c~\leq~2000\),\ (0~\leq~x,y~\leq~10^9\)

Solution

Plain \ (bfs\) is obviously right, can not save too much state.

Consider if from \ ((Sx,sy) \) point to a point \ ((x, y) \) , assume that the total right is gone span class= "Math inline" >\ (r\) step, go left \ (l\) step, apparently \ (r-l\) is a fixed value. Specifically, \ (r-l~=~y-sy\) . So, for any target \ ((x, y) \) , Discover \ (l\) in fact with \ (r\) linear positive correlation. For a point, it is obvious to the point that the \ (r\) is as small as possible, as well as \ (l\) and \ (r\) linear positive correlation, so minimize \ (r\) , \ (l\) The has been minimized. So you can directly build the shortest way, all the right Benquan is 1, the other side right is 0. After running, scan the entire map to determine the legitimacy.

One of the new poses here is \ (0/1bfs\). When the Benquan only \ (0/1\) , you can use a double-ended queue for BFS, specifically, when the front of the weight (0\) , the end of the pressure to join the first, or press the tail. Consider the correctness of this: the dist difference of the points in the queue is not more than 1 at any one time. So the correctness is obvious. The complexity of \ (0/1bfs\) is \ (O (v+e) \). A log is missing compared to the DIJ.

Code
#include <queue> #include <cstdio> #include <cstring> #define RG register#define ci const INT#DEFINE CL Const long long inttypedef long long int ll;namespace io{char buf[110];}    Template <typename t>inline void qr (T &x) {RG char Ch=getchar (), lst= '; while (Ch > ' 9 ') | | (Ch < ' 0 '))    Lst=ch,ch=getchar ();    while (Ch >= ' 0 ') && (ch <= ' 9 ')) x= (x<<1) + (x<<3) + (ch^48), Ch=getchar (); if (lst = = '-') x=-x;}    Template <typename t>inline void qw (T x,const char aft,const bool PT) {if (x < 0) {Putchar ('-'); x=-x;}    RG int top=0;    Do {io::buf[++top]=x%10+ ' 0 ';    } while (x/=10);    while (top) Putchar (io::buf[top--]); if (PT) putchar (aft);} Template <typename t>inline t Mmax (const T a,const t b) {return a > B? a:b;} Template <typename t>inline t mmin (const T a,const t b) {return a < b? A:b;} Template <typename t>inline t mabs (const t a) {return a < 0?-a:a;} Template <typename T&Gt;inline void Mswap (T &a,t &b) {T _temp=a;a=b;b=_temp;} const int MAXN = 2010;const int fx[]={0,-1,0,1};const int fy[]={1,0,-1,0};const int Fv[]={1,0,0,0};int n,m,sx,sy,x,y;int m    U[maxn][maxn];char MP[MAXN][MAXN];STD::d eque<int>qx,qy;int Main () {QR (n); QR (m); QR (SX); QR (sy); QR (x); QR (y);    for (RG int i=1;i<=n;++i) scanf ("%s", mp[i]+1);    memset (mu,0x3f,sizeof MU); mu[sx][sy]=0; Qx.push_front (SX);    Qy.push_front (SY); while (! Qx.empty ()) {int Hx=qx.front (), Hy=qy.front (); Qx.pop_front ();        Qy.pop_front ();            for (RG int i=0;i<4;++i) {int dx=hx+fx[i],dy=hy+fy[i]; if (dx > N) | | (Dy > m) | | (!DX) | | (!dy) | | (Mp[dx][dy] = = ' * ') | | (Mu[dx][dy] <= mu[hx][hy]+fv[i]))            Continue            Mu[dx][dy]=mu[hx][hy]+fv[i]; if (i) {qx.push_front (dx);            Qy.push_front (dy);} else {qx.push_back (dx);        Qy.push_back (dy);}    }} RG int _ans=0; for (RG int i=1;i<=n;++i) {for (RG int j=1;j<=m;++j) if (Mp[i][j]! = ' * ') {if ((Mu[i][j] <= y) && ((Mu[i][j]-j+sy) <= x)) ++_ans;    }} QW (_ans, ' \ n ', true); return 0;}
Summary

When the problem setting needs to minimize multiple variables, consider the correlation between variables, and then convert to the extremum problem of univariate.

When Benquan only \ (0\ ) and \ (1\) , you can consider using \ (0/1bfs\), eliminating the Dij log.

"Extremum problem" "cf1063b" Labyrinth

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.