题意:题目超长 看完题就感觉心力交瘁了
你处在一片树林中 要从始点走到终点 树林中还有其他的人 他们有的只是一个人 有的是一堆人 会在你的路上拦截你并和你打架 你永远是赢家 问最少与几个人打架才能出树林
思路:在路上能碰到的敌人 一定都可以到达终点 所以可以把敌人都看做朝终点跑 在终点的地方等着拦截你
所以可以用反向搜索 从终点开始搜索 每次搜到敌人记录下步数和人数 然后推进vector里 最后遍历一遍vector 所有步数小于到达始点的人数都加进来
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#include <stack>
#include <map>
#include <cmath>
#include <vector>
#define max_ 1010
#define inf 0x3f3f3f3f
#define ll long long
using namespace std;
struct node
{
int x,y,w;
int step;
}zz,u;
int n,m,f=-1;
int sx,sy,ex,ey;
int dir[4][2]={0,1,0,-1,1,0,-1,0};
char mp[max_][max_];
bool vis[max_][max_];
queue<struct node>q;
vector<struct node>v;
void bfs()
{
u.x=ex;
u.y=ey;
u.step=0;
vis[ex][ey]=true;
q.push(u);
while(!q.empty())
{
u=q.front();
q.pop();
for(int k=0;k<4;k++)
{
int tx=u.x+dir[k][0];
int ty=u.y+dir[k][1];
if(tx>=1&&ty>=1&&tx<=n&&ty<=m)
{
if(vis[tx][ty]==false&&mp[tx][ty]!='T')
{
vis[tx][ty]=true;
zz.x=tx;
zz.y=ty;
zz.step=u.step+1;
q.push(zz);
else if(mp[tx][ty]>='1'&&mp[tx][ty]<='9')
{
zz.w=mp[tx][ty]-'0';
v.push_back(zz);
}
else
f=zz.step;
}
}
}
}
}
int main(int argc, char const *argv[])
{
scanf("%d%d",&n,&m);
int i,j;
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
scanf(" %c",&mp[i][j]);
if(mp[i][j]=='S')
sx=i,sy=j;
else if(mp[i][j]=='E')
ex=i,ey=j;
}
}
bfs();
int l=v.size(),cnt=0;
for(i=0;i<l;i++)
{
if(v[i].step<=f)
cnt+=v[i].w;
}
printf("%d\n",cnt );
return 0;
}