uva 705 Slash Maze 斜杠构图统计图中回路

还是斜杠构图,这里统计环的个数和最长环的长度,在bfs中记录长度,加上判断边界不成环的条件即可

  Slash Maze 

By filling a rectangle with slashes (/) and backslashes ( $\backslash$), you can generate nice little mazes. Here is an example:

As you can see, paths in the maze cannot branch, so the whole maze only contains cyclic paths and paths entering somewhere and leaving somewhere else. We are only interested in the cycles. In our example, there are two of them.

Your task is to write a program that counts the cycles and finds the length of the longest one. The length is defined as the number of small squares the cycle consists of (the ones bordered by gray lines in the picture). In this example, the long cycle has length 16 and the short one length 4.

Input 

The input contains several maze descriptions. Each description begins with one line containing two integersw and h ( $1 \le w, h \le 75$), the width and the height of the maze. The next h lines represent the maze itself, and contain w characters each; all these characters will be either ``/" or ``\".

The input is terminated by a test case beginning with w = h = 0. This case should not be processed.

Output 

For each maze, first output the line ``Maze #n:'', where n is the number of the maze. Then, output the line ``kCycles; the longest has length l.'', where k is the number of cycles in the maze and l the length of the longest of the cycles. If the maze does not contain any cycles, output the line ``There are no cycles.".

Output a blank line after each test case.

Sample Input 

6 4
\//\\/
\///\/
//\\/\
\/\///
3 3
///
\//
\\\
0 0

Sample Output 

Maze #1:
2 Cycles; the longest has length 16.

Maze #2:
There are no cycles.


#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
#define max 510
int mo[4][2]={ {-1,0}, {0,1}, {1,0}, {0,-1} };
struct node{
	int ge[4];
	int vis[4];
	int fuhao;
}map[max][max];
struct data{
	int x,y,g;
}d[max][max][4];
int m,n,color,white;
int flag,jishu,zuida,qs;
void bfs(int x,int y,int r)      //染色
{
	queue <data> q;
	int i,j,k;
	if(map[x][y].vis[r])
		return;
	data t;
	t.x=x;t.y=y;t.g=r;
	q.push(t);
	map[x][y].vis[r]=1;
	map[x][y].ge[r]=color;
	flag=0;
	jishu=0;
	while(!q.empty())
	{
		data p;
		p=q.front();
		q.pop();
		jishu++;
		i=p.x;
		j=p.y;
		k=p.g;
		if( (p.x==0&&k==0) || (p.x==m-1&&k==2) || (j==0&&k==3) || (j==n-1&&k==1))
			flag=1;
		if(map[i][j].fuhao=='\\')
		{
			if(map[i][j].vis[(5-k)%4]==0)  //本格
			{
				q.push(d[i][j][(5-k)%4]);
				map[i][j].vis[(5-k)%4]=1;
				map[i][j].ge[(5-k)%4]=color;
			}
			if(0<=i+mo[k][0]&&i+mo[k][0]<m && 0<=j+mo[k][1]&&j+mo[k][1]<n)
			{
				if(map[i+mo[k][0]][j+mo[k][1]].vis[(k+2)%4]==0)
				{
					map[i+mo[k][0]][j+mo[k][1]].vis[(k+2)%4]=1;
					map[i+mo[k][0]][j+mo[k][1]].ge[(k+2)%4]=color;
					q.push(d[i+mo[k][0]][j+mo[k][1]][(k+2)%4]);
				}
			}
		}
		else
		{
			if(map[i][j].vis[3-k]==0)  //本格
			{
				q.push(d[i][j][3-k]);
				map[i][j].vis[3-k]=1;
				map[i][j].ge[3-k]=color;
			}
			if(0<=i+mo[k][0]&&i+mo[k][0]<m && 0<=j+mo[k][1]&&j+mo[k][1]<n)
			{
				if(map[i+mo[k][0]][j+mo[k][1]].vis[(k+2)%4]==0)
				{
					map[i+mo[k][0]][j+mo[k][1]].vis[(k+2)%4]=1;
					map[i+mo[k][0]][j+mo[k][1]].ge[(k+2)%4]=color;
					q.push(d[i+mo[k][0]][j+mo[k][1]][(k+2)%4]);
				}
			}
		}
	}
}
void init()
{
	int i,j,k;
	for(i=0;i<m;i++)
	{
		for(j=0;j<n;j++)
			for(k=0;k<4;k++)
			{
				map[i][j].vis[k]=0;
				map[i][j].fuhao=0;
				map[i][j].ge[k]=0;
			}
	}
}
int main()
{
	int i,j,k,l;
	l=1;
	while(scanf("%d %d",&n,&m)!=EOF&&n+m!=0)
	{
		getchar();
		
		//memset(map,0,sizeof(map));
		init();
		color=0;
		white=0;
		for(i=0;i<m;i++)
		{
			for(j=0;j<n;j++)
			{
				scanf("%c",&map[i][j].fuhao);
				for(k=0;k<4;k++)
				{
					d[i][j][k].x=i;
					d[i][j][k].y=j;
					d[i][j][k].g=k;
				}
			}
			
			getchar();
		}
		zuida=-1;qs=0;
		for(i=0;i<m;i++)
		{
			for(j=0;j<n;j++)
			{
				for(k=0;k<4;k++)
				{
					if(map[i][j].vis[k]==0)
					{
						color++;
						bfs(i,j,k);
						if(flag==0)
						{
							qs++;
							if(jishu>zuida)
								zuida=jishu;
						}
					}
				}
			}
		}
		printf("Maze #%d:\n",l);
		if(zuida!=-1)
			printf("%d Cycles; the longest has length %d.\n",qs,zuida/2);
		else
			printf("There are no cycles.\n");
		printf("\n");
		l++;
	}
	return 0;
}



  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值