// uva 705 - Slash Maze
// 题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=105&page=show_problem&problem=646
// 题目大意:求构成的环数以及最长环的长度
// 第一次做斜线的搜索,想了挺久,一开始想的是用折射的思想,把每个点分左右两边搜索,就是下面第一个代码,后来学长给了另一种思路,把每条斜线分成4块,然后搜索,后者写起来明显容易不少
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int LEFT = 1;
const int RIGHT = 2;
const int UP = 3;
const int DOWN = 4;
struct MapMark{
bool left, right;
} visit[100][100];
char map[100][100];
int cnt_case;
int row, column;
int cycles, length, tmp_length;
int target_x, target_y, target_section;
bool Inmap(int, int);
void Init();
void FindCycles(int, int, int);
void Print();
int main(){
while(scanf("%d %d", &column, &row), row || column){
getchar();
Init();
for(int i = 0; i < row; i++){
gets(map[i]);
}
for(int i = 0; i < row; i++){
for (int j = 0; j < column; j++){
target_x = i;
target_y = j;
if(!visit[i][j].left){
target_section = LEFT;
if(map[i][j] == '\\'){
tmp_length = 0;
FindCycles(i, j - 1, LEFT);
if(!visit[i][j].left){
tmp_length = 0;
FindCycles(i + 1, j, DOWN);
}
} else {
tmp_length = 0;
FindCycles(i, j - 1, LEFT);
if(!visit[i][j].left){
tmp_length = 0;
FindCycles(i - 1, j, UP);
}
}
visit[i][j].left = true;
}
if(!visit[i][j].right){
target_section = RIGHT;
if(map[i][j] == '\\'){
tmp_length = 0;
FindCycles(i, j + 1, RIGHT);
if(!visit[i][j].right){
tmp_length = 0;
FindCycles(i - 1, j, UP);
}
} else {
tmp_length = 0;
FindCycles(i, j + 1, RIGHT);
if(!visit[i][j].right){
tmp_length = 0;
FindCycles(i + 1, j, DOWN);
}
}
visit[i][j].right = true;
}
}
}
Print();
}
return 0;
}
void Init(){
memset(map, 0, sizeof(map));
memset(visit, 0, sizeof(visit));
cycles = 0;
length = 0;
}
void FindCycles(int now_x, int now_y, int direction){
if(!Inmap(now_x, now_y)){
return;
}
++tmp_length;
if(now_x == target_x && now_y == target_y){
if(map[now_x][now_y] == '\\'){
if(target_section == RIGHT && (direction == LEFT || direction == DOWN)) {
cycles++;
visit[now_x][now_y].right = true;
if(length < tmp_length)
length = tmp_length;
}
if(target_section == LEFT && (direction == RIGHT || direction == UP)){
cycles++;
visit[now_x][now_y].left = true;
if(length < tmp_length)
length = tmp_length;
}
} else {
if(target_section == LEFT && (direction == RIGHT || direction == DOWN)){
cycles++;
visit[now_x][now_y].left = true;
if(length < tmp_length)
length = tmp_length;
}
if(target_section == RIGHT && (direction == LEFT || direction == UP)){
cycles++;
visit[now_x][now_y].right = true;
if(length < tmp_length)
length = tmp_length;
}
}
return;
}
if(map[now_x][now_y] == '\\'){
if(direction == LEFT) {
visit[now_x][now_y].right = true;
FindCycles(now_x - 1, now_y, UP);
} else
if(direction == DOWN){
visit[now_x][now_y].right = true;
FindCycles(now_x, now_y + 1, RIGHT);
} else
if(direction == RIGHT){
visit[now_x][now_y].left = true;
FindCycles(now_x + 1, now_y, DOWN);
} else
if(direction == UP){
visit[now_x][now_y].left = true;
FindCycles(now_x, now_y - 1, LEFT);
}
} else {
if(direction == RIGHT) {
visit[now_x][now_y].left = true;
FindCycles(now_x - 1, now_y, UP);
} else
if(direction == UP){
visit[now_x][now_y].right = true;
FindCycles(now_x, now_y + 1, RIGHT);
} else
if(direction == LEFT){
visit[now_x][now_y].right = true;
FindCycles(now_x + 1, now_y, DOWN);
} else
if(direction == DOWN){
visit[now_x][now_y].left = true;
FindCycles(now_x, now_y - 1, LEFT);
}
}
}
bool Inmap(int x, int y){
return x >= 0 && y >= 0 && x < row && y < column;
}
void Print(){
cout << "Maze #" << ++cnt_case << ":" << endl;
if(cycles){
printf("%d Cycles; the longest has length %d.\n\n", cycles, length);
} else
printf("There are no cycles.\n\n");
}
#include <cstdio>
#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
struct Node{
int x, y;
} now_node;
queue<Node> node_que;
int length, cycles;
int map[200][200];
char buf;
int cnt_case;
int direction[8][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}, {-1, -1}, {1, 1}, {-1, 1}, {1, -1}};
int row, column;
void BFS();
int main(){
while(scanf("%d %d", &column, &row) ,column || row){
getchar();
memset(map, 0, sizeof(map));
for(int i = 0; i < row; i++){
for(int j = 0; j <= column; j++){
buf = getchar();
if(buf == '\\'){
map[i * 2][j * 2] = map[i * 2 + 1][j * 2 + 1]= 2;
} else
if(buf == '/') {
map[i * 2][j * 2 + 1] = map[i * 2 + 1][j * 2] = 1;
}
}
}
row *= 2;
column *= 2;
for(int i = 0; i < row; i++){
if(map[i][0] == 0){
now_node.x = i;
now_node.y = 0;
BFS();
}
if(map[i][column - 1] == 0){
now_node.x = i;
now_node.y = column - 1;
BFS();
}
}
for (int j = 0; j< column; j++){
if(map[0][j] == 0){
now_node.x = 0;
now_node.y = j;
BFS();
}
if(map[row - 1][j] == 0){
now_node.x = row - 1;
now_node.y = j;
BFS();
}
}
length = cycles = 0;
for(int i = 1; i < row - 1; i++){
for(int j = 1; j < column; j++){
if(map[i][j] == 0){
now_node.x = i;
now_node.y = j;
BFS();
}
}
}
printf("Maze #%d:\n", ++cnt_case);
if (cycles){
printf("%d Cycles; the longest has length %d.\n\n", cycles, length);
} else
printf("There are no cycles.\n\n");
}
return 0;
}
bool Inmap(Node node){
return node.x >= 0 && node.x < row && node.y >= 0 && node.y < column;
}
void BFS(){
map[now_node.x][now_node.y] = 3;
node_que.push(now_node);
Node tmp_node;
++cycles;
int tmp_length = 1;
while(node_que.size()){
now_node = node_que.front();
node_que.pop();
for(int i = 0; i < 8; i++){
tmp_node.x = now_node.x + direction[i][0];
tmp_node.y = now_node.y + direction[i][1];
if(Inmap(tmp_node) && map[tmp_node.x][tmp_node.y] == 0){
if((i == 0 || i == 1 || i == 2 || i == 3)
|| (i == 4 && map[now_node.x][now_node.y - 1] != 1)
|| (i == 5 && map[now_node.x][now_node.y + 1] != 1)
|| (i == 6 && map[now_node.x][now_node.y + 1] != 2)
|| (i == 7 && map[now_node.x][now_node.y - 1] != 2)){
map[tmp_node.x][tmp_node.y] = 3;
++tmp_length;
node_que.push(tmp_node);
}
}
}
}
if(tmp_length > length)
length = tmp_length;
}