705 - Slash Maze

来源:互联网 发布:农村淘宝的官网 编辑:程序博客网 时间:2024/05/21 07:14

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#define MAX 160

using namespace std;

int line,row;
int Vis[MAX][MAX];
int Count;//the number of circle
int Route;

void Initial()
{
    memset(Vis,-1,sizeof(Vis));

    for(int i = 1;i <= 2*row;i++)
     for(int j = 1;j <= 2*line;j++)
        Vis[i][j] = 0;

    for(int i = 0;i <= 2*row;i++)
     for(int j = 0;j <= 2*line;j++)
      if( (i%2 == 0) && ( j%2 == 0) )
       Vis[i][j] = 3;//biao shi wei jing guo
    Count = 0;
}

void input_deal()
{
    char c;
    for(int i = 1;i <= 2*row;i+=2)
     for(int k = 1;k <= 2*line;k+=2)
     {
            cin>>c;
            if(c == '/')
                Vis[i][k] = 1;//biaoshi
            else if(c == 92)
                Vis[i][k] = 2;//biaoshi
    }
}

/*void print()
{
    for(int i = 0;i <= 2*row;i++)
    {
        for(int j = 0;j <= 2*line;j++)
          printf("%4d",Vis[i][j]);


        cout<<endl;
    }
}
*/
void dfs(int x,int y)
{
    //cout<<"x = "<<x<<" y = "<<y<<endl;
   // if(Route == -1) return;

    if(Vis[x][y] == -1){
        Route = -1;
        return;
    }

    if(Vis[x][y]) return;

    //cout<<"in"<<endl;
    Route++;
    Vis[x][y] = 4;

    if( x > 0 && y > 0 && x <= 2*row && y<= 2*line && Vis[x+1][y] != 2 && Vis[x][y-1] != 2 && (!Vis[x+1][y-1] || Vis[x+1][y-1] == -1))
     dfs(x+1,y-1);
    if(x > 0 && y > 0 && Vis[x+1][y] != 1&& x <= 2*row && y<= 2*line && Vis[x][y+1] != 1 && (!Vis[x+1][y+1] || Vis[x+1][y+1] == -1))
     dfs(x+1,y+1);
    if(x > 0 && y > 0 && Vis[x-1][y] != 2 && x <= 2*row && y<= 2*line && Vis[x][y+1] != 2 && (!Vis[x-1][y+1] || Vis[x-1][y+1] == -1))
     dfs(x-1,y+1);
    if(x > 0 && y > 0 && Vis[x-1][y] != 1 && x <= 2*row && y<= 2*line&& Vis[x][y-1] != 1 && (!Vis[x-1][y-1] || Vis[x-1][y-1] == -1))
     dfs(x-1,y-1);

}

int Find_Max()
{
    int max = 0;
    for(int i = 1;i <= 2*row;i++)
     for(int j = 1;j <= 2*line;j++)
      if(!Vis[i][j])
      {
          Route = 0;

          dfs(i,j);

          if(Route > 3){
          //  cout<<"____________________________\n";
             // cout<<i<<"  "<<j<<" route = "<<Route<<endl;
              Count++;
              if(max < Route)
               max = Route;
          }
      }

    return max;
}

int main()
{
    int Cas = 0;
    while(cin>>line>>row)
    {

        if(!line && !row) break;

        printf("Maze #%d:\n",++Cas);

        Initial();

        input_deal();

      //  print();

        int temp = 0;
        temp = Find_Max();

        if(temp > 3)
         printf("%d Cycles; the longest has length %d.\n",Count,temp);
        else
         cout<<"There are no cycles."<<endl;

        cout<<endl;

    }
    return 0;
}