解题思路:
1、走迷宫最短路(BFS)。2、敌人一开始初始化成墙,但是需要注意:敌人替换成的这个“墙”不会阻碍其他敌人的“墙”(可以穿透)。
AC 代码
#include
#include
#define mem(a,b) memset(a,b,sizeof a);
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
int n,m;
const int dir[4][2]={1,0,-1,0,0,1,0,-1}; // UDRL:(i from 0 to 3)
struct node
{
int x,y,sp;
};
node nd_s;
char g[300][300];
void fillMaze(int x,int y,int &flag)
{
g[x][y]='#';
for(int i=x-1;i>=0;i--)
{
if(g[i][y]!='.')
{
if(g[i][y]=='@') flag=1;
else if(g[i][y]=='#') continue;
break;
}
else if(g[i][y]=='.') g[i][y]='#';
}
for(int i=x+1;i=0;j--)
{
if(g[x][j]!='.')
{
if(g[x][j]=='@') flag=1;
else if(g[x][j]=='#') continue;
break;
}
else if(g[x][j]=='.') g[x][j]='#';
}
for(int j=y+1;j tp;
node u,t;
g[s.x][s.y]='x';
tp.push(s);
while(!tp.empty())
{
u=tp.front(); tp.pop();
if(u.x+1>=n || u.x-1<0 || u.y+1>=m || u.y-1<0)
return 1+u.sp;
for(int i=0;i<4;i++)
{
t.x=u.x+dir[i][0];
t.y=u.y+dir[i][1];
if(g[t.x][t.y]=='.')
{
t.sp=u.sp+1;
g[t.x][t.y]='x';
tp.push(t);
}
}
}
return -1;
}
//void showG()
//{
// puts("----------------");
// for(int i=0;i