您的位置:首页 > 其它

uva 705 Slash Maze

2014-08-03 11:08 337 查看
用3阶对角矩阵表示\/,数0的个数即可。
#include <iostream>
#include <cstdio>
#include <fstream>
#include <cstring>
#include <stdlib.h>
#include <string.h>
#include <queue>
using namespace std;
int height,width,cnt;
int map[240][240];
int ans,times=0;
int step_x[]={1,-1,0,0};
int step_y[]={0,0,1,-1};
struct fuckT{int posi_x,posi_y;};
void dps(int x,int y){
int cnt_0=0,newx,newy;
queue<fuckT>que;
fuckT temp,fuc;
bool flag=true;//如果结束的时候还是true ,就说明是封闭的。
if(map[x][y]==0)
{
temp.posi_x=x;temp.posi_y=y;
que.push(temp);
while(!que.empty()){
fuc = que.front();
que.pop();
if(map[fuc.posi_x][fuc.posi_y]==2){flag=false;}
else if(map[fuc.posi_x][fuc.posi_y]==0){
map[fuc.posi_x][fuc.posi_y]=1;cnt_0++;
for(int i=0;i<4;i++)
{
newx=fuc.posi_x+step_x[i];
newy=fuc.posi_y+step_y[i];
//if(newx<0||newy<0||newx>height*3+1||newy>width*3+1)
if(map[newx][newy]==0||map[newx][newy]==2)
{
temp.posi_x=newx;temp.posi_y=newy;
que.push(temp);
}
}
}
}
if(flag){cnt++;if(cnt_0>ans)ans=cnt_0;}
}
}
int main()
{
//ifstream cin("ha.txt");
int width,height;
char s;
while(cin>>width>>height&&height&&width){
times++;
memset(map,0,sizeof(map));
cnt=0;ans=-1;
for(int i=0;i<height;i++)
{
for(int j=0;j<width;j++)
{
cin>>s;
if(s=='\\')
{
map[1+i*3][1+j*3]=1;
map[1+i*3+1][1+j*3+1]=1;
map[1+i*3+2][1+j*3+2]=1;
}
else
{
map[1+i*3][1+j*3+2]=1;
map[1+i*3+1][1+j*3+1]=1;
map[1+i*3+2][1+j*3]=1;
}
}
}
for(int j=0;j<=width*3+1;j++){map[0][j]=2;map[height*3+1][j]=2;}
for(int j=0;j<=height*3+1;j++){map[j][0]=2;map[j][width*3+1]=2;}
//输出
/*
for(int i=0;i<=height*3+1;i++)
{
for(int j=0;j<=width*3+1;j++)
{cout<<map[i][j]<<" ";}
cout<<endl;
}*/
for(int i=0;i<=height*3+1;i++)
{
for(int j=0;j<=width*3+1;j++)
{if(map[i][j]==0){dps(i,j);}}

}
cout<<"Maze #"<<times<<":"<<endl;
if(cnt>0)
{cout<<cnt<<" Cycles; the longest has length "<<ans/3<<"."<<endl;}
else{cout<<"There are no cycles."<<endl;}
cout<<endl;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: