HDOJ2579,BFS(三维数组标记)

三维数组对于我这个萌萌的新手来说真是酷酷的,帅到不行,,,

三维数组前面还看到空间的哪一题HDOJ1240,这一题时间上的标记,更酷了!!!

不说废话了,贴一发代码;

#include <stdio.h>
#include <iostream>
#include <sstream>
#include <string.h>
#include <math.h>
#include<stdlib.h>
#include <queue>
#include <set>
#include <algorithm>
using namespace std;
#define N 110
char a[N][N];
int flag[N][N][110];
int dx[4]={0,0,-1,1};
int dy[4]={1,-1,0,0};
struct asd{
    int x,y,step;
}now,ne;
queue<asd>q;
int r,c,k;
int ax,ay,bx,by;

void bfs()
{
    int i,s;
    memset(flag,0,sizeof(flag));
    while(!q.empty())
        q.pop();
    now.x=ax;
    now.y=ay;
    now.step=0;
    flag[ax][ay][0]=1;
    q.push(now);
    while(!q.empty())
    {
        now=q.front();
        q.pop();
        for(i=0;i<4;i++)
        {
            int aa=dx[i]+now.x;
            int bb=dy[i]+now.y;
            if(a[aa][bb]=='G')
            {
                printf("%d\n",now.step+1);
                return;
            }
            s=(now.step+1)%k;
            if(aa<0||aa>=r||bb<0||bb>=c||flag[aa][bb][s]||(a[aa][bb]=='#'&&s))
                continue;
            ne.x=aa;
            ne.y=bb;
            ne.step=now.step+1;
            flag[aa][bb][s]=1;
            q.push(ne);
        }
    }
    printf("Please give me another chance!\n");
}

int main()
{
    int i,j,T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d%d",&r,&c,&k);
        for(i=0;i<r;i++)
        {
            scanf("%s",a[i]);
        }
        int f1,f2;
        f1=f2=0;
        for(i=0;i<r;i++)
        {
            for(j=0;j<c;j++)
            {
                if(a[i][j]=='Y')
                {
                    ax=i;
                    ay=j;
                    f2=1;
                }
                if(a[i][j]=='G')
                {
                    bx=i;
                    by=j;
                    f1=1;
                }
                if(f1&&f2)
                    break;
            }
            if(f1&&f2)
                break;
        }
        bfs();
    }
    return 0;
}


posted @ 2016-02-24 10:40  see_you_later  阅读(165)  评论(0编辑  收藏  举报