题目不难,但是有点繁琐,需要仔细分析处理。
要点:将/\对应的图转换为一般迷宫问题,可以将整个图顺时针选择45度
坐标对应的法则:不管是‘/’ 还是 ‘\’ 只关注其中点的坐标即可
令x(i,j)表示第i行第j列的slash的中点x坐标
y(i,j)表示第i行第j列的slash的中点y坐标
则 x(i , j + 1) = x(i , j) + 1;
y(i , j + 1) = y(i , j) + 1;
x(i+1 , j) = x(i , j) + 1;
y(i+1 , j) =y(i , j) - 1;
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int size = 100 + 110;
char s[size][size];
int pos[size][size][2];
int u[size * size][4]; //第一维对应 row * nw + col ,第二维对应上下左右
int d[][2] = { {-1 , -1} , {-1 , 0} , {0 , -1} , {0, 0}};
int v[size*size];
int nw , nh;
int dfs(int x , int y , int len)
{
v[x] = 1;
for(int i = 0 ; i < 4 ; ++i)
{
if(u[x][i] < 0) continue;
if(v[u[x][i]]) continue;
int r = dfs(u[x][i] , y , len + 1);
if(r) return 1 + r;
else{
u[x][0] = u[x][1] = u[x][2] = u[x][3] = -1;
return 0;
}
}
int a = 0 , b = 0;
for(int i = 0 ; i < 4 ; ++i)
{
if(u[x][i]>=0 && u[u[x][i]][0] == -1) a = 1;
if(u[x][i] == -1) b = 1;
}
if(!a && !b) return 1;
else{
u[x][0] = u[x][1] = u[x][2] = u[x][3] = -1;
return 0;
}
}
int d2[][2] = {{-1 , 0} , { 1 , 0} , {0 , -1} , {0 , 1}};
void f(int x , int y)
{
if(v[x*nw + y]) return;
v[x*nw + y] = 1;
int t = x * nw + y;
for(int i = 0 ; i < 4; ++i)
{
int tx = d2[i][0] + x;
int ty = d2[i][1] + y;
if(tx < 0 || ty < 0) continue;
if(tx >= nh || ty >= nw) continue;
u[t][i] = tx * nw + ty;
}
}
int main()
{
int w , h;
int nloop = 1;
while(cin >> w >> h && w && h)
{
for(int i = 0 ; i < h ; ++i)
cin >> s[i];
pos[0][0][0] = pos[0][0][1] = 0;
for(int i = 1; i < w ; ++i)
{
pos[0][i][0] = pos[0][i-1][0] + 1;
pos[0][i][1] = pos[0][i-1][1] + 1;
}
for(int i = 1 ; i < h ; ++i)
{
for(int j = 0 ; j < w ; ++j)
{
pos[i][j][0] = pos[i-1][j][0] + 1;
pos[i][j][1] = pos[i-1][j][1] - 1;
}
}
int minh , maxh;
int minw , maxw;
if(s[0][0] == '/') minh = 0;
else minh = -1;
if(s[h-1][w-1] == '/') maxh = pos[h-1][w-1][0];
else maxh = pos[h-1][w-1][0] + 1;
if(s[0][w-1] == '/') maxw = pos[0][w-1][1] + 1;
else maxw = pos[0][w-1][1];
if(s[h-1][0] == '/') minw = pos[h-1][0][1] - 1;
else minw = pos[h-1][0][1];
nh = maxh - minh;
nw = maxw - minw;
memset(u , -1 , sizeof(u));
memset(v , 0 , sizeof(v));
for(int i = 0 ; i < h ; ++i)
{
for(int j = 0 ; j < w ; ++j)
{
int x = pos[i][j][0] - minh;
int y = pos[i][j][1] - minw;
pos[i][j][0] = x;
pos[i][j][1] = y;
if(s[i][j] == '/')
{
for(int k = 0 ; k < 4 ; ++k)
{
int tx = x + d[k][0];
int ty = y + d[k][1];
if(tx < 0 || ty < 0) continue;
if(tx >= nh || ty >= nw) continue;
f(tx , ty);
int t = tx * nw + ty;
u[t][1 - (k > 1)] = -2;
}
}
else
{
for(int k = 0 ; k < 4 ; ++k)
{
int tx = x + d[k][0];
int ty = y + d[k][1];
if(tx < 0 || ty < 0) continue;
if(tx >= nh || ty >= nw) continue;
f(tx ,ty);
int t = tx * nw + ty;
u[t][3 - k % 2] = -2;
}
}
}
}
int m = -1 , cnt = 0;
memset(v , 0 , sizeof(v));
for(int i = 0 ; i < nh ; ++i)
{
for(int j = 0 ; j < nw ; ++j)
{
int t = i * nw + j;
if(!v[t])
{
int r = dfs(t , t , 0);
if(r) { ++cnt , m = m < r ? r : m; }
}
}
}
cout << "Maze #" << nloop++ << ":" << endl;
if(cnt) cout << cnt << " Cycles; the longest has length " << m << "." << endl << endl;
else cout << "There are no cycles." << endl << endl;
}
return 0;
}