RT,第 16 个点 WA 了。
思路是差分判断一个点是否能被看到,然后 bfs
#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
const int MAXN = 360;
const int INF = 0x3f3f3f3f;
int n, m, hide, quick, d;
int a[MAXN][MAXN], b[MAXN][MAXN], c[MAXN][MAXN], sx, sy, tx, ty;
string in;
inline void watch(int x, int y, int dis)
{
--dis;
for(int i=0;i<=dis;i++)
{
int minx = max(1, x-i), maxx = min(n, x+i);
int miny = max(1, y-dis+i), maxy = min(n, y+dis-i);
++b[minx][miny]; --b[minx][maxy+1];
++b[maxx][miny]; --b[maxx][maxy+1];
}
}
int dx[] = {0, 1, 0, -1, 1, -1, 1, -1};
int dy[] = {1, 0, -1, 0, -1, -1, 1, 1};
struct node{
int nx, ny, step, nowh, nowq;
node(int X=0,int Y=0,int S=INF,int H=INF,int Q=INF):nx(X),ny(Y),step(S),nowh(H),nowq(Q)
{};
bool operator<(const node &o)
{
if(step != o.step) return step<o.step;
else if(nowh+nowq != o.nowh+o.nowq) return nowh+nowq<o.nowh+o.nowq;
else return nowh<o.nowh;
}
};
node q[20000005], ans;
int h = 1, t = 0;
bool vis[MAXN][MAXN][20][20];
inline bool inside(int x, int y)
{
return x >= 1 && x <= n && y >= 1 && y <= m;
}
inline void bfs()
{
vis[sx][sy][0][0] = 1;
q[++t] = node(sx, sy, 0, 0, 0);
while(h <= t)
{
node now = q[h++];
if(now.step > ans.step) continue;
if(now.nx == tx && now.ny == ty)
{
if(now < ans) ans = now;
}
for(int i=0;i<8;i++)
{
int xx = now.nx+dx[i], yy = now.ny+dy[i];
if(inside(xx, yy) && a[xx][yy] <= 0)
{
if(c[xx][yy])
{
if(now.nowh+1 <= hide && !vis[xx][yy][now.nowh+1][now.nowq])
{
vis[xx][yy][now.nowh+1][now.nowq] = 1;
q[++t] = node(xx, yy, now.step+1, now.nowh+1, now.nowq);
}
}
else
{
if(!vis[xx][yy][now.nowh][now.nowq])
{
vis[xx][yy][now.nowh][now.nowq] = 1;
q[++t] = node(xx, yy, now.step+1, now.nowh, now.nowq);
}
}
}
}
if(now.nowq+1 <= quick)
{
for(int i=0;i<4;i++)
{
int xx = now.nx + dx[i]*d, yy = now.ny + dy[i]*d;
if(inside(xx, yy) && a[xx][yy] <= 0)
{
if(c[xx][yy])
{
if(now.nowh+1 <= hide && !vis[xx][yy][now.nowh+1][now.nowq+1])
{
vis[xx][yy][now.nowh+1][now.nowq+1] = 1;
q[++t] = node(xx, yy, now.step+1, now.nowh+1, now.nowq+1);
}
}
else
{
if(!vis[xx][yy][now.nowh][now.nowq+1])
{
vis[xx][yy][now.nowh][now.nowq+1] = 1;
q[++t] = node(xx, yy, now.step+1, now.nowh, now.nowq+1);
}
}
}
}
}
}
}
int main()
{
scanf("%d%d%d%d%d",&n,&m,&hide,&quick,&d);
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
cin>>in;
if(in[0] == 'S')
{
a[i][j] = -1;
sx = i; sy = j;
}
else if(in[0] == 'T')
{
a[i][j] = -2;
tx = i; ty = j;
}
else if(in[0] == '.') a[i][j] = 0;
else
{
int now = 0;
for(int k=0;k<in.size();k++)
now = (now<<3)+(now<<1)+in[k]-'0';
a[i][j] = now;
watch(i, j, now);
}
}
}
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
{
b[i][j] += b[i][j-1];
if(b[i][j] > 0) c[i][j] = 1;
}
bfs();
if(ans.step == INF) puts("-1");
else printf("%d %d %d\n",ans.step, ans.nowh, ans.nowq);
return 0;
}
希望大佬们看得惯我的码风 /kel
但是大括号换行的人似乎不多
另外我英语不好有些变量名可能很奇怪QAQ