#include <cstdio>
#include <iostream>
#include <stack>
#include <cstring>
using namespace std;
char str[55][55];
char A[55][55];
int visited[55][55];
int ans;
int record;
int length;
int counter;
void dfs(int x,int y)
{
visited[x][y]=1;
if (A[x+1][y]<A[x][y] || A[x-1][y]<A[x][y] || A[x][y+1]<A[x][y] || A[x][y-1]<A[x][y])
record=0;
if (!visited[x+1][y] && x+1<=counter && A[x+1][y]==A[x][y])
dfs(x+1,y);
if (!visited[x-1][y] && x-1>=1 && A[x-1][y]==A[x][y])
dfs(x-1,y);
if (!visited[x][y+1] && y+1<=length && A[x][y+1]==A[x][y])
dfs(x,y+1);
if (!visited[x][y-1] && y-1>=1 && A[x][y-1]==A[x][y])
dfs(x,y-1);
}
int main()
{
while (scanf("%s",str[0])!=EOF)
{
ans=0;
memset(visited,0,sizeof(visited));
int i,j;
length=strlen(str[0]);
counter=1;
while (1)
{
scanf("%s",str[counter]);
if (!strcmp(str[counter],"*****"))
break;
counter++;
}
for (i=0;i<=length+1;i++)
A[0][i]=A[counter+1][i]='~';
for (i=0;i<=counter+1;i++)
A[i][0]=A[i][length+1]='~';
for (i=0;i<=counter-1;i++)
for (j=0;j<=length-1;j++)
A[i+1][j+1]=str[i][j];
/*for (i=0;i<=counter+1;i++)
{
for (j=0;j<=length+1;j++)
printf("%c",A[i][j]);
printf("\n");
}*/
for (i=1;i<=counter;i++)
for (j=1;j<=length;j++)
if (!visited[i][j])
{
record=1;
dfs(i,j);
if (record)
{
ans++;
//printf("x:%d y:%d\n",i,j);
}
}
printf("%d\n",ans);
}
return 0;
}
Sicily1428(DFS)
最新推荐文章于 2017-07-06 19:56:17 发布