您的位置:首页 > 产品设计 > UI/UE

ZOJ_1649_HDU_1242_Rescue

2015-07-24 17:26 393 查看
这个题目由于守卫的存在时间不再是根据bfs的层数增加的。

这个问题可以用优先队列,以时间为优先级进行。

以后补充优先队列的代码。

个人又开了一个队列单独记录守卫的格子。

问题就在于顺序

1.遍历到A。A的路入队,守卫入队2。

2.遍历A的路。A的路的路入队,守卫入队2。

3此时“第一次”入队2的守卫入队。(麻烦就麻烦在这个地方计数很烦,详见代码)

4………….

以后还是优先队列吧

//#.#####.
//#.a#..r.
//#..#x...
//..#..#.#
//#...##..
//.#......
//........
#include <iostream>
#include <stdio.h>
#include <queue>
using namespace std;

const int M=205;

char ma[M][M];
int isu[M][M];

int dx[4]={0,0,-1,1};
int dy[4]={1,-1,0,0};

queue<int> re;
queue<int> ki;    //杀死守卫时使用

int f;

void setwall()              //设置外包围
{
for(int i=0;i<M;i++)
for(int j=0;j<M;j++)
{
ma[i][j]='#';
isu[i][j]=0;           //顺便重置
}
}

void bfs(int sx,int sy,int n,int m)
{
//cout<<"bfs"<<endl;
while(!re.empty())
re.pop();
while(!ki.empty())
ki.pop();

re.push(sx*m+sy);
isu[sx][sy]=1;
int i=1,j=0;
int ina=0,ink=0,ink2=0;                  //复杂的计数机制
while(!re.empty()||!ki.empty())
{

while(i--)
{
int t=re.front();
re.pop();
int cx=t/m,cy=t%m;
for(int i=0;i<4;i++)
{
int x=cx+dx[i],y=cy+dy[i];
if(!isu[x][y]&&ma[x][y]=='.')
{
isu[x][y]=isu[cx][cy]+1;
re.push(x*m+y);
ina++;
}
else if(!isu[x][y]&&ma[x][y]=='x')
{
isu[x][y]=isu[cx][cy]+2;
ki.push(x*m+y);
ink++;
}
else if(!isu[x][y]&&ma[x][y]=='a')
{
f=isu[cx][cy];
return;
}
}
//cout<<i<<" "<<cx<<" "<<cy<<ink<<" "<<ink2<<endl;
}
j=ink2;ink2=ink;
while(j--)
{
re.push(ki.front());
ki.pop();
ina++;
}
i=ina;
ina=0;ink=0;
}
}

int main()
{
int n,m;
int sx,sy;
while(scanf("%d%d",&n,&m)!=EOF)
{
f=0;
setwall();
for(int i=1;i<=n;i++)
{
scanf("%s",ma[i]+1);
for(int j=1;j<=m;j++)
{
if(ma[i][j]=='r')
{
sx=i;
sy=j;
}
}
}
bfs(sx,sy,n,m+1);
if(f)
printf("%d\n",f);
else
printf("Poor ANGEL has to stay in the prison all his life.\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: