Http://codeforces.com/contest/1064/problem/D
Up/down plus 0, left/right plus 1,
Step = 0,1,......
The minimum number of steps is required, so use BFS.
Step=k-Step=k+1
1.step=k use once left/right to reach step=k+1
2.step=k+1 Unlimited use up and down, get all the step=k+1 state
Use dijkstra+ heap optimization/SPFA timeout.
1#include <bits/stdc++.h>2 using namespacestd;3 #definell Long Long4 #defineMINV 1e-65 #defineINF 1e96 #definePi 3.14159265367 #defineNL 2.71828182848 Constll mod=1e9+7;//9982443539 Const intMaxn= (2e3+Ten) * (2e3+Ten);Ten One intQ[MAXN],STEP[MAXN]; A CharS[MAXN]; - intn,m,bx,by,gl,gr,head,tail,phead,i,sum=0, l,r,d; - the voidAddintDintPosintk) - { -tail++; -q[tail]=D; +s[d]='*'; -step[tail]=step[pos]+K; +R= (D%m-by+step[tail])/2; Al=step[tail]-R; at if(L<=gl && r<=gr) -sum++; - } - - intMain () - { inscanf"%d%d",&n,&m); -scanf"%d%d",&bx,&by ); toscanf"%d%d",&gl,&gr); +BX--, by--, sum++; -q[1]=bx*m+by ; the for(i=1; i<=n;i++) *scanf"%s", S+ (i-1)*m); $s[q[1]]='*';Panax NotoginsengHead=1, tail=1; - while(head<=tail) the { +Phead=head; A while(head<=tail) the { +d=q[head]-m; - if(d>=0&& s[d]=='.') $Add (D,head,0); $d=q[head]+m; - if(D<n*m && s[d]=='.') -Add (D,head,0); thehead++; - }Wuyi //[Phead,tail] (head-1) the while(phead<head) - { Wud=q[phead]-1; - if(d%m!=m-1&& s[d]=='.') AboutAdd (D,phead,1); $d=q[phead]+1; - if(d%m!=0&& s[d]=='.') -Add (D,phead,1); -phead++; A } + } theprintf"%d", sum); - return 0; $}
Codeforces Round #516 (Div. 2, by Moscow Team Olympiad) d. Labyrinth