poj 1103 & zoj 1142 Maze (图形模拟+简单搜索)
2013-12-22 15:38
375 查看
Maze
By filling a rectangle with slashes (/) and backslashes (\), 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 integers w and h (1 <= w,h <= 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 "k Cycles; 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. Source Mid-Central European Regional Contest 1999 |
给一个地图,找出有多少个封闭的图形,这样的图形最大的一个占多少格。
思路:
其实这题就是难在图形模拟上面,我是用一个3*3的格子模拟的一个 ‘×’ ,
如 ‘\' 就可以表示为
100
010
001
然后就是解决哪些位置为格子,这样处理后会发现格子是有规律的,然后我预处理了格子的位置,就剩下简单的搜索计数的问题了,可以先用dfs(0,0)跑一遍,剩下的未标记的都是环的成员了。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define maxn 305
#define MAXN 200005
#define mod 1000000007
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-6
typedef long long ll;
using namespace std;
int n,m,k,cnt,flag;
double ans,tot;
bool mp[maxn][maxn];
bool vis[maxn][maxn];
double val[maxn][maxn];
int dx[]= {-1,1,0,0};
int dy[]= {0,0,-1,1};
char s[maxn];
void show()
{
int i,j,t;
for(i=1; i<=3*n; i++)
{
for(j=1; j<=3*m; j++)
{
printf("%d",mp[i][j]);
}
printf("\n");
}
}
void dfs(int x,int y)
{
int i,j,t,tx,ty;
for(i=0; i<4; i++)
{
tx=x+dx[i];
ty=y+dy[i];
if(tx<0||tx>3*n+1||ty<0||ty>3*m+1||vis[tx][ty]||mp[tx][ty]) continue ;
tot+=val[tx][ty];
vis[tx][ty]=1;
dfs(tx,ty);
}
}
void solve()
{
int i,j,t;
memset(vis,0,sizeof(vis));
vis[0][0]=1;
dfs(0,0);
k=ans=0;
for(i=1; i<=3*n; i++)
{
for(j=1; j<=3*m; j++)
{
if(!vis[i][j]&&!mp[i][j])
{
k++;
vis[i][j]=1;
tot=val[i][j];
dfs(i,j);
ans=max(ans,tot);
}
}
}
}
int main()
{
int i,j,t,nx,ny,test=0;
memset(val,0,sizeof(val));
for(i=1; i<=100; i++)
{
for(j=1; j<=100; j++)
{
nx=3*i-1,ny=3*j-1;
mp[nx][ny]=1;
mp[nx-1][ny-1]=1;
mp[nx+1][ny+1]=1;
mp[nx-1][ny+1]=1;
mp[nx+1][ny-1]=1;
}
}
for(i=1;i<=300;i++)
{
for(j=1;j<=300;j++)
{
if(!mp[i][j]) val[i][j]=0.5;
}
}
while(scanf("%d%d",&m,&n),n||m)
{
memset(mp,0,sizeof(mp));
for(i=1; i<=n; i++)
{
scanf("%s",s);
for(j=1; j<=m; j++)
{
nx=3*i-1,ny=3*j-1;
mp[nx][ny]=1;
if(s[j-1]=='\\')
{
mp[nx-1][ny-1]=1;
mp[nx+1][ny+1]=1;
}
else
{
mp[nx-1][ny+1]=1;
mp[nx+1][ny-1]=1;
}
}
}
// show();
solve();
printf("Maze #%d:\n",++test);
if(k) printf("%d Cycles; the longest has length %d.\n",k,int(ans));
else printf("There are no cycles.\n");
printf("\n");
}
return 0;
}
相关文章推荐
- POJ3107&&POJ1655【树形DP/简单搜索】
- POJ3087 Shuffle'm Up 简单模拟
- poj 3087 Shuffle'm Up (模拟搜索)
- POJ 3087 Shuffle'm Up(kuangbin带你飞 专题一:简单搜索)专题一完结
- poj 1218&&hdu 1337 The Drunk Jailer【简单模拟】
- POJ 1573 && hdu 1035(简单模拟)
- [kuangbin带你飞]专题一 简单搜索G - Shuffle'm Up(POJ 3087)
- POJ 1573 && hdu 1035 Robot Motion【简单模拟】
- [kuangbin带你飞]专题一 简单搜索 G - Shuffle'm Up poj 3087
- POJ 1573 && hdu 1035 Robot Motion(简单模拟)
- poj 3087 Shuffle'm Up (kuangbin带你飞--简单搜索)
- POJ 1573 && hdu 1035 Robot Motion【简单模拟】
- POJ 3505 && HDU 2341 Tower Parking(简单模拟)
- POJ 3087 Shuffle'm Up(简单模拟)
- POJ 3984 & [kuangbin带你飞]专题一 简单搜索 K
- G - Shuffle'm Up POJ 3087 模拟洗牌的过程,算作暴力搜索也不为过
- [kuangbin]专题一 简单搜索 K - 迷宫问题 POJ - 3984
- (简单模拟2.4.3)POJ 2864 Pascal Library(某一个环节的值决定全局的值)
- poj 3268 简单的SPFA算法运用
- POJ3087 Shuffle'm Up 简单模拟