1. 程式人生 > >Codeforces Round #516 (Div. 2, by Moscow Team Olympiad) D. Labyrinth

Codeforces Round #516 (Div. 2, by Moscow Team Olympiad) D. Labyrinth

所有 pre 超時 %s ble force com tail nth

http://codeforces.com/contest/1064/problem/D

向上/向下加0,向左/右加1,

step = 0,1,……

求的是最少的步數,所以使用bfs。

step=k -> step=k+1

1.step=k 使用一次左/右 到達 step=k+1

2.step=k+1 無限使用上下,得到所有 step=k+1 的狀態

用dijkstra+堆優化 / spfa 超時。

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 #define ll long long
 4 #define minv 1e-6
 5
#define inf 1e9 6 #define pi 3.1415926536 7 #define nl 2.7182818284 8 const ll mod=1e9+7;//998244353 9 const int maxn=(2e3+10)*(2e3+10); 10 11 int q[maxn],step[maxn]; 12 char s[maxn]; 13 int n,m,bx,by,gl,gr,head,tail,phead,i,sum=0,l,r,d; 14 15 void add(int d,int pos,int k) 16 { 17 tail++; 18 q[tail]=d;
19 s[d]=*; 20 step[tail]=step[pos]+k; 21 r=(d%m-by+step[tail])/2; 22 l=step[tail]-r; 23 if (l<=gl && r<=gr) 24 sum++; 25 } 26 27 int main() 28 { 29 scanf("%d%d",&n,&m); 30 scanf("%d%d",&bx,&by); 31 scanf("%d%d",&gl,&gr); 32
bx--,by--,sum++; 33 q[1]=bx*m+by; 34 for (i=1;i<=n;i++) 35 scanf("%s",s+(i-1)*m); 36 s[q[1]]=*; 37 head=1,tail=1; 38 while (head<=tail) 39 { 40 phead=head; 41 while (head<=tail) 42 { 43 d=q[head]-m; 44 if (d>=0 && s[d]==.) 45 add(d,head,0); 46 d=q[head]+m; 47 if (d<n*m && s[d]==.) 48 add(d,head,0); 49 head++; 50 } 51 //[phead,tail] (head-1) 52 while (phead<head) 53 { 54 d=q[phead]-1; 55 if (d%m!=m-1 && s[d]==.) 56 add(d,phead,1); 57 d=q[phead]+1; 58 if (d%m!=0 && s[d]==.) 59 add(d,phead,1); 60 phead++; 61 } 62 } 63 printf("%d",sum); 64 return 0; 65 }

Codeforces Round #516 (Div. 2, by Moscow Team Olympiad) D. Labyrinth