HDU 2579/BFS/ Dating with girls(2)

來源:互聯網
上載者:User

題目連結

/*題意是是傳統的迷宮加上一個條件,牆壁在k的整倍數時刻會消失,那麼求到達出口的最短時間。關鍵點在於某個點最多被走k次,標記vis[x][y][time%k]即可。*/#include<queue>#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;const int maxn=100+5;int vis[maxn][maxn][15];int dir[4][2]={0,1,1,0,0,-1,-1,0};char maze[maxn][maxn];int sx,sy,ex,ey;int n,m,k;void init(){    memset(vis,0,sizeof(vis));}struct node{    int x;    int y;    int time;};int BFS(){    node now;    now.x=sx;    now.y=sy;    now.time=0;    queue<node>que;    que.push(now);    while(!que.empty())    {        now=que.front();        que.pop();        if(now.x==ex&&now.y==ey)            return now.time;        for(int i=0;i<4;i++)        {            int x=now.x+dir[i][0];            int y=now.y+dir[i][1];            int t=now.time+1;            int mod=t%k;            if(x>=1&&x<=n&&y>=1&&y<=m)            {                if(maze[x][y]!='#'&&vis[x][y][mod]==0)                {                    vis[x][y][mod]=t;                    node next=(node){x,y,t};                    que.push(next);                }                else if(maze[x][y]=='#'&&mod==0&&vis[x][y][mod]==0)                {                    vis[x][y][mod]=t;                    node next=(node){x,y,t};                    que.push(next);                }            }        }    }    return -1;}int main (){    int T;scanf("%d",&T);    while(T--)    {        scanf("%d%d%d",&n,&m,&k);        init();        for(int i=1;i<=n;i++)        {            scanf("%s",maze[i]+1);            for(int j=1;j<=m;j++)                if(maze[i][j]=='Y')                    sx=i,sy=j;                else if(maze[i][j]=='G')                    ex=i,ey=j;        }        int ans=BFS();        if(ans==-1)            printf("Please give me another chance!\n");        else            printf("%d\n",ans);    }    return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.