HDU 3533 Escape

来源:互联网 发布:项目数据分析师认证 编辑:程序博客网 时间:2024/05/21 06:59

这题之前用的地图预处理,然后超时了。其实应该是自己的预处理的效率太低。

然后在网上看了一份结题报告。

居然连样例都过不了也能AC。。。不过倒是让我想到了预处理的优化。(明天再写吧)

先说说他的代码。

先也是预处理地图。记录下每一颗子弹第一次出现的 位置 以及到   此时的时间    还有这颗    子弹所属的炮台的发射周期 。

这样以后这颗子弹再出现在此处的时间就是 time + per...

然后BFS一下 大致也好理解。但是他没有考虑到可以呆在原地不动的情况。只能说后台数据弱了。


想要AC 可以贴任何一份代码    第二份代码是自己将两个结合以后的正确思路。

还是一个3维的MAP 记录每一个地点子弹出现的时间,然后用一个三维的VIS判重。分别是位置和时间。

#include <cstdio>#include <iostream>#include <algorithm>#include <queue>using namespace std;int dx[] = {0,0,1,-1,0};//E W S Nint dy[] = {1,-1,0,0,0};//0 1 2 3struct node{    int posx,posy,v,per,d;}gun[105];struct people{    int x,y,step;};int map[105][105][5];bool vis[105][105];int n,m,k,lit;void debug(people t){    for(int i=0;i<=n;i++)    {        for(int j=0;j<=m;j++)        {            if(t.x==i && t.y==j)printf(" @ ");            else printf("%2d ",map[i][j][0]);        }        puts("");    }    puts("");}bool ok(int x,int y){    if(x>=0 && x<=n && y>=0 && y<=m)return true;    return false;}void mark(node t){    int x=t.posx,y=t.posy,i;    int time=0;    while(1)    {        for(i=1;i<=t.v;i++)        {            x+=dx[t.d];            y+=dy[t.d];            if(ok(x,y) && map[x][y][0]!=-1);            else break;        }        if(i<=t.v)break;        time++;        map[x][y][0]++;        map[x][y][map[x][y][0]]=time*100+t.per;    }}void bfs(){    int j;    people w,e;    queue<people>Q;    w.x=w.y=0;    w.step=0;    Q.push(w);    while(!Q.empty())    {        w=Q.front();        Q.pop();        //debug(w);        if(w.x==n && w.y==m)        {            printf("%d\n",w.step);            return;        }        if(w.step>=lit)continue;        for(int kk=0;kk<5;kk++)        {            e=w;            e.x+=dx[kk];            e.y+=dy[kk];            int xx=e.x;            int yy=e.y;            if(map[xx][yy][0]!=-1 && ok(xx,yy) && !vis[xx][yy])            {                e.step++;                if(kk!=4)vis[xx][yy]=true;                if(map[xx][yy][0]==0)                {                    Q.push(e);                }                else                {                    for(j=1;j<=map[xx][yy][0];j++)                    {                        if(e.step<map[xx][yy][j]/100)continue;                        int tmp1=e.step-map[xx][yy][j]/100;                        int tmp2=map[xx][yy][j]%100;                        if(tmp1%tmp2==0)break;                    }                    if(j>map[xx][yy][0])                    {                        Q.push(e);                    }                }            }        }    }    printf("Bad luck!\n");}int main(){    while(scanf("%d%d%d%d%*c",&n,&m,&k,&lit)!=EOF)    {        memset(map,0,sizeof(map));        memset(vis,false,sizeof(vis));        for(int i=1;i<=k;i++)        {            char ch;            scanf("%c%d%d%d%d%*c",&ch,&gun[i].per, &gun[i].v,&gun[i].posx,&gun[i].posy);            if(ch=='N')gun[i].d=3;            else if(ch=='S')gun[i].d=2;            else if(ch=='E')gun[i].d=0;            else if(ch=='W')gun[i].d=1;            map[gun[i].posx][gun[i].posy][0]=-1;        }        for(int i=1;i<=k;i++)        mark(gun[i]);               bfs();    }    return 0;}


#include <cstdio>#include <iostream>#include <algorithm>#include <queue>using namespace std;int dx[] = {0,0,1,-1,0};//E W S Nint dy[] = {1,-1,0,0,0};//0 1 2 3struct node{    int posx,posy,v,per,d;}gun[105];struct people{    int x,y,step;};int map[105][105][5];bool vis[105][105][1005];int n,m,k,lit;void debug(people t){    for(int i=0;i<=n;i++)    {        for(int j=0;j<=m;j++)        {            if(t.x==i && t.y==j)printf(" @ ");            else printf("%2d ",map[i][j][0]);        }        puts("");    }    puts("");    system("pause");}bool ok(int x,int y){    if(x>=0 && x<=n && y>=0 && y<=m)return true;    return false;}void mark(node t){    int x=t.posx,y=t.posy,i;    int time=0;    while(1)    {        for(i=1;i<=t.v;i++)        {            x+=dx[t.d];            y+=dy[t.d];            if(ok(x,y) && map[x][y][0]!=-1);            else break;        }        if(i<=t.v)break;        time++;        map[x][y][0]++;        map[x][y][map[x][y][0]]=time*100+t.per;    }}void bfs(){    queue<people>Q;    people e,w;    w.x=0;    w.y=0;    w.step=0;    Q.push(w);    while(!Q.empty())    {        w=Q.front();        Q.pop();        if(w.x==n && w.y==m)        {            printf("%d\n",w.step);            return;        }        if(w.step>=lit)continue;        for(int k=0;k<5;k++)        {            e=w;            e.step++;            e.x+=dx[k];            e.y+=dy[k];            if(!ok(e.x,e.y))continue;            if(!vis[e.x][e.y][e.step] && map[e.x][e.y][0]!=-1)            {                vis[e.x][e.y][e.step]=true;                if(map[e.x][e.y][0]==0)                {                    Q.push(e);                }                else                {                    int tk;                    for(tk=1;tk<=map[e.x][e.y][0];tk++)                    {                        if(map[e.x][e.y][tk]/100>e.step)continue;                        int tmp1=e.step-map[e.x][e.y][tk]/100;                        int tmp2=map[e.x][e.y][tk]%100;                        if(tmp1%tmp2==0)break;                    }                    if(tk>map[e.x][e.y][0])                    {                        Q.push(e);                    }                }            }        }    }    printf("Bad luck!\n");}int main(){    while(scanf("%d%d%d%d%*c",&n,&m,&k,&lit)!=EOF)    {        memset(map,0,sizeof(map));        memset(vis,false,sizeof(vis));        for(int i=1;i<=k;i++)        {            char ch;            scanf("%c%d%d%d%d%*c",&ch,&gun[i].per, &gun[i].v,&gun[i].posx,&gun[i].posy);            if(ch=='N')gun[i].d=3;            else if(ch=='S')gun[i].d=2;            else if(ch=='E')gun[i].d=0;            else if(ch=='W')gun[i].d=1;            map[gun[i].posx][gun[i].posy][0]=-1;        }        for(int i=1;i<=k;i++)        mark(gun[i]);        bfs();    }    return 0;}


原创粉丝点击