uva 705Slash Maze

来源:互联网 发布:知乎 探险 编辑:程序博客网 时间:2024/05/16 11:49

用1, 0表示,/ 为001  \ 表示为100, 然后找回路;

                            010                010

                            100                001

#include <iostream>#include <cmath>#include <cstdio>#include <algorithm>#include <cstring>using std::cin;using std::cout;using std::endl;int num=0,f,sum,max,pos,h,w;int move[4][2]= {1,0,-1,0,0,-1,0,1},maze[400][400];void dfs(int x,int y){    int px,py,i;    for (i=0; i<4; i++)    {        px=x+move[i][0];        py=y+move[i][1];        if ((px>=0)&&(px<h*3)&&(py>=0)&&(py<w*3))        {            if (maze[px][py]==0)            {                ++pos;                maze[px][py]=1;                dfs(px, py);            }        }        else            f=0;;    }};int main(){    int i,j;    char s[200];    while (cin >> w >> h, w+h)    {        getchar();        ++num;        sum=0;        max=0;        for (i=0; i<h*3; i++)            for (j=0; j<w*3; j++)                maze[i][j]=0;        for (i=0; i<h; i++)        {            gets(s);            for (j=0; j<w; j++)                if (s[j]=='\\')                {                    maze[3*i][3*j]=1;                    maze[3*i+1][3*j+1]=1;                    maze[3*i+2][3*j+2]=1;                }                else                {                    maze[3*i+2][3*j]=1;                    maze[3*i+1][3*j+1]=1;                    maze[3*i][3*j+2]=1;                }        }        for (i=0; i<3*h; i++)            for (j=0; j<3*w; j++)            {                if (maze[i][j]==0)                {                    pos=1;                    f=1;                    maze[i][j]=1;                    dfs(i,j);                    if (f)                    {                        ++sum;                        if (pos>max) max=pos;                    }                }            }        if (sum==0) printf("Maze #%d:\nThere are no cycles.\n\n",num);        else printf("Maze #%d:\n%d Cycles; the longest has length %d.\n\n",num,sum,max/3);    }    return 0;}