| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
TLE ,有谁帮我看看#include <iostream>
using namespace std;
char a[202][202];
bool sign[202][202];
int main(int argc, char* argv[])
{
int n,m,i,j,k,num[202][202],x[202],y[202];
bool a[202][202],sign[202][202];
while(scanf("%d%d",&n,&m)&&n+m)
{
memset(sign,0,sizeof(sign));
memset(a,'0',sizeof(a));
memset(num,0,sizeof(num));
k=0;
for(i=1;i<=n;i++)
{
getchar();
for(j=1;j<=m;j++)
scanf("%c",&a[i][j]);
}
for(i=1;i<=n;i++)
for(j=1;j<=m;j++)
{
num[i][j]=(a[i-1][j]=='1')+(a[i][j-1]=='1')+(a[i+1][j]=='1')+(a[i][j+1]=='1');
//四周1的个数
if(a[i][j]=='1'&&num[i][j]<=1)//蛇的端点
{
x[k]=i;
y[k++]=j;
}
}
int count=0;//MAX蛇的个数
while(k--)
{
if(!sign[x[k]][y[k]])
{
sign[x[k]][y[k]]=1;//是否访问
i=x[k];
j=y[k];
while(1)
{
if(num[x[k]][y[k]]==0)//蛇长为1
break;
if(a[i-1][j]=='1'&&!sign[i-1][j])
i--;
else
{
if(a[i][j-1]=='1'&&!sign[i][j-1])
j--;
else
{
if(a[i+1][j]=='1'&&!sign[i+1][j])
i++;
else
if(a[i][j+1]=='1'&&!sign[i][j+1])
j++;
}
}
sign[i][j]=1;
if(num[i][j]!=2)
break;
}
if(num[i][j]>2)//有分支,不是蛇
{
sign[i][j]=0;
continue;
}
if(num[x[k]-1][y[k]]==1||num[x[k]][y[k]-1]==1||
num[x[k]+1][y[k]]==1||num[x[k]][y[k]+1]==1)
continue;
if(num[i-1][j]==1||num[i][j-1]==1||
num[i+1][j]==1||num[i][j+1]==1)
continue;
count++;
}
}
printf("%d\n",count);
}
return 0;
}
Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator