fork download
  1. //Lib
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<cstdlib>
  5. #include<cmath>
  6. #include<ctime>
  7.  
  8. #include<iostream>
  9. #include<algorithm>
  10. #include<vector>
  11. #include<string>
  12. #include<queue>
  13. #include<set>
  14. #include<map>
  15. using namespace std;
  16. //Macro
  17. #define rep(i,a,b) for(int i=a,tt=b;i<=tt;++i)
  18. #define drep(i,a,b) for(int i=a,tt=b;i>=tt;--i)
  19. #define erep(i,e,x) for(int i=x;i;i=e[i].next)
  20. #define irep(i,x) for(__typeof(x.begin()) i=x.begin();i!=x.end();i++)
  21. #define read() (strtol(ipos,&ipos,10))
  22. #define sqr(x) ((x)*(x))
  23. #define pb push_back
  24. #define PS system("pause");
  25. typedef long long ll;
  26. typedef pair<int,int> pii;
  27. const int oo=~0U>>2;
  28. const double inf=1e100;
  29. const double eps=1e-6;
  30. string name="", in=".in", out=".out";
  31. //Var
  32. struct MonoQueue
  33. {
  34. deque<pii> s;
  35. void clear(){s.clear();}
  36. void Insert(int x,int y)
  37. {
  38. while(!s.empty()&&s.back().second<y)s.pop_back();
  39. s.push_back(pii(x,y));
  40. }
  41. int Get(int time,int limit)
  42. {
  43. while(!s.empty()&&time-s.front().first>limit)s.pop_front();
  44. return s.front().second;
  45. }
  46. }Q;
  47. struct S
  48. {
  49. int st,t,dir;
  50. bool operator <(const S &o)const{return st<o.st;}
  51. }seg[208];
  52. int f[2][208][208];
  53. bool forbid[208][208];
  54. int n,m,X,Y,K,ans;
  55. void Init()
  56. {
  57. scanf("%d%d%d%d%d",&n,&m,&X,&Y,&K);
  58. char ch;
  59. rep(i,1,n)rep(j,1,m)
  60. {
  61. cin>>ch;forbid[i][j]=ch=='x';
  62. f[0][i][j]=-oo;
  63. }
  64. f[0][X][Y]=0;
  65. rep(i,1,K)
  66. scanf("%d%d%d",&seg[i].st,&seg[i].t,&seg[i].dir),seg[i].t-=seg[i].st-1;
  67. sort(seg+1,seg+1+K);
  68. }
  69. void Update(int time,int now,int x,int y,int k)
  70. {
  71. if(forbid[x][y]){f[now][x][y]=-oo;Q.clear();return;}
  72. Q.Insert(time,f[now^1][x][y]-time);
  73. f[now][x][y]=Q.Get(time,seg[k].t)+time;
  74. ans=max(ans,f[now][x][y]);
  75. }
  76. void Work()
  77. {
  78. int now=0;
  79. rep(i,1,K)
  80. {
  81. now^=1;
  82. if(seg[i].dir==1)
  83. rep(y,1,m)
  84. {
  85. Q.clear();
  86. drep(x,n,1)
  87. Update(n-x,now,x,y,i);
  88. }
  89. else if(seg[i].dir==2)
  90. rep(y,1,m)
  91. {
  92. Q.clear();
  93. rep(x,1,n)
  94. Update(x-1,now,x,y,i);
  95. }
  96. else if(seg[i].dir==3)
  97. rep(x,1,n)
  98. {
  99. Q.clear();
  100. drep(y,m,1)
  101. Update(m-y,now,x,y,i);
  102. }
  103. else
  104. rep(x,1,n)
  105. {
  106. Q.clear();
  107. rep(y,1,m)
  108. Update(y-1,now,x,y,i);
  109. }
  110. }
  111. cout<<ans<<endl;
  112. }
  113. int main()
  114. {
  115. // freopen((name+in).c_str(),"r",stdin);
  116. // freopen((name+out).c_str(),"w",stdout);
  117. Init();
  118. Work();
  119. return 0;
  120. }
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty