您的位置:首页 > 其它

哈理工软件学院"兆方美迪"杯第六届程序设计大赛【高年级组】--决赛 Problem G:逃脱 By Assassin

2016-11-21 11:05 429 查看
思路:需要主要注意到几个关键点

1.正常的小孩是走不了墙壁的。

2.火焰是可以烧掉墙壁的,换句话说火焰必定每次可以向八个方向走一格。

3.注意小朋友先走,火焰后烧过来,就是小朋友行动比火焰快(这个会影响最后一步,小孩的极限逃脱)

那么怎么实现,我的思路是

1.一个数组bfs记录一下火焰第几步烧到某个位置。(这里不这么做也行,因为直接是可以算出来的)

2.小孩儿走,注意小孩的步数应该小于火焰的步数!否则被烧到了!但是存在一种特判,就是最后一步小孩走到了出口,但是火焰刚刚烧到出口,这个情况是可以允许的!需要特判!然后就是限定条件的bfs了,没啥技术难度,主要是细节复杂。

上我的丑代码:

#include<bits/stdc++.h>
#define input freopen("input.txt","r",stdin)
using namespace std;
int n,m;
char c[32][32];
int fire[32][32],road[32][32],v[32][32];
int firedir[8][2]={-1,-1,-1,0,-1,1,0,-1,0,1,1,-1,1,0,1,1},poepledir[4][2]={-1,0,0,-1,0,1,1,0};
queue<int>t,x,y;

void clearn(){
while(!t.empty()){
t.pop();
}
while(!x.empty()){
x.pop();
}
while(!y.empty()){
y.pop();
}
}
int inrange(int xxx,int yyy){
if(xxx>0&&xxx<=n&&yyy>0&&yyy<=m)return 1;
return 0;
}

void makefire(int xxx,int yyy,int ttt){
int i,j;
for(i=0;i<8;i++){
if(inrange(xxx+firedir[i][0],yyy+firedir[i][1])&&fire[xxx+firedir[i][0]][yyy+firedir[i][1]]==0){
x.push(xxx+firedir[i][0]);
y.push(yyy+firedir[i][1]);
t.push(ttt+1);
fire[xxx+firedir[i][0]][yyy+firedir[i][1]]=ttt+1;
}
}
int xx,yy,tt;
while(!x.empty()){
xx=x.front();x.pop();
yy=y.front();y.pop();
tt=t.front();t.pop();
makefire(xx,yy,tt);
}
}

void walk(int xxx,int yyy,int ttt){
int i,j;
for(i=0;i<4;i++){
if(inrange(xxx+poepledir[i][0],yyy+poepledir[i][1])&&road[xxx+poepledir[i][0]][yyy+poepledir[i][1]]==0&&(ttt+1<fire[xxx+poepledir[i][0]][yyy+poepledir[i][1]]||(c[xxx+poepledir[i][0]][yyy+poepledir[i][1]]=='E'&&ttt+1==fire[xxx+poepledir[i][0]][yyy+poepledir[i][1]]))&&c[xxx+poepledir[i][0]][yyy+poepledir[i][1]]!='#'){
x.push(xxx+poepledir[i][0]);
y.push(yyy+poepledir[i][1]);
t.push(ttt+1);
road[xxx+poepledir[i][0]][yyy+poepledir[i][1]]=ttt+1;
}
}
int xx,yy,tt;
while(!x.empty()){
xx=x.front();x.pop();
yy=y.front();y.pop();
tt=t.front();t.pop();
walk(xx,yy,tt);
}
}

int main(){
//input;
int aaa,i,j,startx,starty,outx,outy,firex,firey;
scanf("%d",&aaa);
while(aaa--){
memset(fire,0,sizeof(fire));
memset(road,0,sizeof(road));
scanf("%d%d",&n,&m);
getchar();
for(i=1;i<=n;i++){
for(j=1;j<=m;j++){
scanf("%c",&c[i][j]);
if(c[i][j]=='S'){
startx=i;
starty=j;
}
else if(c[i][j]=='*'){
firex=i;
firey=j;
}
else if(c[i][j]=='E'){
outx=i;
outy=j;
}
}
getchar();
}
memset(v,0,sizeof(v));
clearn();
fire[firex][firey]=-1;
makefire(firex,firey,0);
clearn();
road[startx][starty]=-1;
walk(startx,starty,0);
if(road[outx][outy]>0){
cout<<road[outx][outy]<<endl;
}
else{
cout<<"T_T"<<endl;
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐