正题
题目大意
n∗mn*mn∗m的有障碍物的网格,开始在(xs,ys)(x_s,y_s)(xs,ys)。有kkk段时间网格会倾斜,对于倾斜的方向可以选择移动或者不移动,求最长移动距离。
解题思路
因为每段时间方向唯一,所以我们对于每一列或每一行分开计算,有转移fi=fj+i−j(i−j≤t)f_i=f_j+i-j(i-j\leq t)fi=fj+i−j(i−j≤t)
然后单调队列转移即可,时间复杂度O(nmk)O(nmk)O(nmk)
codecodecode
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
const int N=210;
struct node{int s,t,w;
}a[N];
int n,m,sx,sy,k,f[N][N],s[N];
char v[N][N];
deque<int> q;
bool cmp(node x,node y)
{return x.s<y.s;}
int main()
{scanf("%d%d%d%d%d",&n,&m,&sx,&sy,&k);for(int i=1;i<=n;i++)scanf("%s",v[i]+1);for(int i=1;i<=k;i++)scanf("%d%d%d",&a[i].s,&a[i].t,&a[i].w);memset(f,0xcf,sizeof(f));s[0]=s[n+1]=-2147483647/3; sort(a+1,a+1+k,cmp);f[sx][sy]=0;for(int p=1;p<=k;p++){int l=a[p].t-a[p].s+1;if(a[p].w==1){for(int j=1;j<=m;j++){while(!q.empty())q.pop_back();q.push_back(0);for(int i=n,fr=0;i>=1;i--,fr++){if(v[i][j]=='x'){while(!q.empty())q.pop_back();continue;}while(!q.empty()&&s[q.back()]+fr<=f[i][j])q.pop_back();s[i]=f[i][j]-fr;q.push_back(i);while(q.front()-i>l)q.pop_front();f[i][j]=s[q.front()]+fr;}}}else if(a[p].w==2){for(int j=1;j<=m;j++){while(!q.empty())q.pop_back();q.push_back(n+1);for(int i=1,fr=0;i<=n;i++,fr++){if(v[i][j]=='x'){while(!q.empty())q.pop_back();continue;}while(!q.empty()&&s[q.back()]+fr<=f[i][j])q.pop_back();s[i]=f[i][j]-fr;q.push_back(i);while(i-q.front()>l)q.pop_front();f[i][j]=s[q.front()]+fr;}}}else if(a[p].w==3){for(int i=1;i<=n;i++){while(!q.empty())q.pop_back();q.push_back(0);for(int j=m,fr=0;j>=1;j--,fr++){if(v[i][j]=='x'){while(!q.empty())q.pop_back();continue;}while(!q.empty()&&s[q.back()]+fr<=f[i][j])q.pop_back();s[j]=f[i][j]-fr;q.push_back(j);while(q.front()-j>l)q.pop_front();f[i][j]=s[q.front()]+fr;}}}else if(a[p].w==4){for(int i=1;i<=n;i++){while(!q.empty())q.pop_back();q.push_back(n+1);for(int j=1,fr=0;j<=m;j++,fr++){if(v[i][j]=='x'){while(!q.empty())q.pop_back();continue;}while(!q.empty()&&s[q.back()]+fr<=f[i][j])q.pop_back();s[j]=f[i][j]-fr;q.push_back(j);while(j-q.front()>l)q.pop_front();f[i][j]=s[q.front()]+fr;}}}}int ans=0;for(int i=1;i<=n;i++)for(int j=1;j<=m;j++)ans=max(ans,f[i][j]);printf("%d",ans);
}