poj3083Children of the Candy Corn

来源:互联网 发布:手机摇骰子软件 编辑:程序博客网 时间:2024/05/17 08:03

http://poj.org/problem?id=3083

#include <stdio.h>#include <string.h>#define Max 50int w,h;char maze[Max][Max];int start_x,start_y,end_x,end_y;int dir[4][2]={{0,-1},{-1,0},{0,1},{1,0}};//左上右下int visited[Max][Max];typedef struct node{    int x,y,dis;}node;node now,temp,queue[100000];void initial(){    int i,j;    scanf("%d %d",&w,&h);    for(i=0;i<h;i++)    {        getchar();        for(j=0;j<w;j++)        {            scanf("%c",&maze[i][j]);            if(maze[i][j]=='S')            {                start_x=i;                start_y=j;            }            if(maze[i][j]=='E')            {                end_x=i;                end_y=j;            }        }    }}int Depth_FirstSearch(int x1,int y1,int d,int x2,int y2){    int new_x,new_y,i,count=1;    if(x1==x2&&y1==y2)        return count;    d=(d+3)%4;    i=d;    for(i=d;i<d+4;i++)    {        new_x=x1+dir[i%4][0];        new_y=x2+dir[i%4][1];        if(new_x>=0&&new_x<h&&new_y>=0&&new_y<w&&maze[new_x][new_y]!='#')        {            count++;            d=i;//记录当前方向            Depth_FirstSearch(new_x,new_y,d,x2,y2);        }    }}int Breadth_FirstSearch(){    int front,rear,i;    front=rear=0;    now.x=start_x;    now.y=start_y;    now.dis=1;    queue[rear++]=now;    while(front<rear)    {        now=queue[front++];//取对头元素now        for(i=0;i<4;i++)        {            temp.x=now.x+dir[i][0];            temp.y=now.y+dir[i][1];            temp.dis=now.dis+1;            if(temp.x>=0&&temp.x<h&&temp.y>=0&&temp.y<w&&maze[temp.x][temp.y]!='#'&&!visited[temp.x][temp.y])            {                if(maze[temp.x][temp.y]=='E')                    return temp.dis;                visited[temp.x][temp.y]=1;                queue[rear++]=temp;            }        }    }}int main(){    int n,count1,count2,count3;    scanf("%d",&n);    while(n--)    {        initial();        count1=Depth_FirstSearch(start_x,start_y,0,end_x,end_y);        count2=Depth_FirstSearch(end_x,end_y,0,start_x,start_y);        count3=Breadth_FirstSearch();        printf("%d %d %d\n",count1,count2,count3);    }    return 0;}


0 0