Light OJ 1012

来源:互联网 发布:淘宝代购是不退换的吗 编辑:程序博客网 时间:2024/05/22 07:04

   题比较水,就直接上代码了。

#include<cstdio>#include<iostream>#include<cstring>#include<queue>using namespace std;int ans,W,H;typedef struct position{   int x,y;}pos;pos S;queue<pos> Q;char G[22][22];bool vis[22][22];int dx[4]={-1,0,1,0};int dy[4]={0,-1,0,1};bool is_not(int x,int y){    if(x<=0 || y <=0 || x >H|| y >W || vis[x][y] || G[x][y]=='#')        return true;    else  return false;}void bfs(){    int i;    memset(vis,0,sizeof(vis));    Q.push(S);    vis[S.x][S.y]=1;    pos now,tmp;    while(!Q.empty())    {        now=Q.front();        Q.pop();        for(i=0;i<4;i++)        {            tmp.x=now.x+dx[i],tmp.y=now.y+dy[i];            if(is_not(tmp.x,tmp.y)) continue;            vis[tmp.x][tmp.y]=1;            Q.push(tmp);            ans++;        }        //printf("%d\n",ans);    }}int main(){    int T,t,i,j,k;    scanf("%d",&T);    for(t=1;t<=T;t++)    {        scanf("%d%d",&W,&H);        getchar();        char ch;        for(i=1;i<=H;i++)        {            for(j=1;j<=W;j++)            {                scanf("%c",&G[i][j]);                if(G[i][j]=='@')  S.x=i,S.y=j;            }            getchar();        }        /*        for(i=1;i<=H;i++)        {            for(j=1;j<=W;j++)             printf("%c",G[i][j]);            printf("\n");        }        printf("%d %d",S.x,S.y);*/        ans=0;        bfs();        printf("Case %d: %d\n",t,ans+1);    }    return 0;}


0 0
原创粉丝点击