poj 3038 Children of the Candy Corn bfs dfs

 入口S,出口E,分别求由入口 到出口靠左走,靠右走,和最短路三种走法各自的步数。入口和出口在边界处,并且不会在四个角上,入口和出口至少隔着一个阻碍。
本来是想找一个广搜和深搜结合的题目,百度一下找到的这题,后来发现这题所谓的广搜加深搜只是用深搜找一条路径,而用广搜找另外一条路径,poj 3038 Children of the Candy Corn bfs dfs - 某年某月 - zxj015的博客。根本不是我希望的广搜和深搜的结合。
     不过既然题目已经看了,还是做一做吧。求最短路的没什么好说的,用一下广搜就可以解决问题了。主要是靠左走和靠右走的部分,这里用到了两个数组,dirx[],diry[],共同来确定方向。dirx[],diry[],数组的下标增1对4求余代表顺时针方向,来处理向左走。同样下标增3对4求余代表逆时针方向,来处理向右走。
 
#include <stdio.h>
#include <string.h>
#include<stdlib.h>
#include <iostream>
#include<queue>
using namespace std;
int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
int dirx[4]={0,-1,0,1};
int diry[4]={-1,0,1,0};//dirx,diry数组下标:0左,1上,2右,3下;
int co,ro,ansr,ansl,ansm;
char map[45][45];
struct coor
{
    int x,y,z;
    int time;
};
bool visited[45][45];
void dfsl(int,int,int,int);
void dfsr(int,int,int,int);
void dfsl(int a,int b,int time,int c)
{
     int i,x,y;
    
     if(map[a][b]=='E')
     {
          ansl=time;
          return;
     }
     c=(c+3)%4;  //靠左走;这样c总是原来方向的左面一个方向,
     for(i=0;i<4;i++)
     {
             x=a+dirx[c];
             y=b+diry[c];
             if(!(x<0||y<0||x>=co||y>=ro||map[x][y]=='#'))
             {
                  dfsl(x,y,time+1,c);
                  break;
             }
             c=(c+1)%4; //顺时针方向
     }
             
}
void dfsr(int a,int b,int time,int c)
{
     int i,x,y;
     if(map[a][b]=='E')
     {
          ansr=time;
          return;
     }
     c=(c+1)%4;// 靠右走;这样c总是原来方向的右面一个方向。
     for(i=0;i<4;i++)
     {
             x=a+dirx[c];
             y=b+diry[c];
             if(!(x<0||y<0||x>=co||y>=ro||map[x][y]=='#'))
             {
                  dfsr(x,y,time+1,c);
                  break;
             }
             c=(c+3)%4; //逆时针方向
     }
             
}
 bool check(int x,int y)
{
    if(x<0 || y<0 || x>=co || y>=ro) return false;
    if(map[x][y]=='#' || visited[x][y]) return false;
    return true;
}
void bfs(int a,int b)
{
    int i,j,k,l,front=-1,rear=-1;
    coor out,in;
    queue <coor> que;
    in.x=a;
    in.y=b;
    in.time=1;
    que.push(in);
    visited[a][b]=true;
    while(!que.empty())
    {
        out=que.front();
        que.pop();
        if(map[out.x][out.y]=='E')
        {
            ansm=out.time;
            return;
        }
        for(l=0;l<4;l++)
        {
            i=out.x+dir[l][0];
            j=out.y+dir[l][1];
            if(!check(i,j)) continue;
            visited[i][j]=true;
            in.x=i;
            in.y=j;
            in.time=out.time+1;
            que.push(in);
        }
    }
}
int main()
{
   int cas,i,j,a,b,c;
   scanf("%d",&cas);
   while(cas--)
   {
        scanf("%d%d",&ro,&co);
        //getchar();
        for(i=0;i<co;i++)
        scanf("%s",map[i]);
        for(i=0;i<co;i++)
        for(j=0;j<ro;j++)
             if(map[i][j]=='S')
             {
                a=i;
                b=j;
             }
        map[a][b]='#';
        if(b==ro-1)
            c=0;
        else if(a==co-1)
            c=1;
        else if(b==0)
            c=2;
        else if(a==0)
            c=3;
        dfsl(a,b,1,c);
        dfsr(a,b,1,c);
        memset(visited,false,sizeof(visited));
        bfs(a,b);
        printf("%d %d %d\n",ansl,ansr,ansm);
   }
  system("pause");
   return 0;
}
           
原文地址:https://www.cnblogs.com/zxj015/p/2740246.html