連續1的個數演算法----C版

abcbbc發表於2007-05-17
微軟的筆試題目
答案是studenthe提供的[@more@]

#include
#include

#define MAXCOL 10
#define MAXROW 10

int staticpoint=0;
int count[MAXROW*MAXCOL];

struct point
{
int x;
int y;
}point[MAXCOL*MAXROW][MAXCOL*MAXROW];

int number[MAXROW][MAXCOL]=
{
0,1,0,1,0,1,0,0,0,1,
0,1,1,1,0,1,0,1,0,0,
1,1,0,1,0,1,1,0,0,0,
0,0,1,0,1,0,1,0,1,0,
1,1,0,1,1,0,0,0,1,0,
1,0,0,0,1,0,0,0,0,0,
1,1,0,0,0,1,0,1,0,0,
1,1,1,1,0,1,0,1,0,1,
0,1,0,1,0,1,0,1,0,1,
1,0,1,0,1,0,1,1,0,1
};

void check(int row,int col)
{
if(number[row][col]==0)
return;
else
{
point[staticpoint][count[staticpoint]].x=row;
point[staticpoint][count[staticpoint]].y=col;
count[staticpoint]+=1;
number[row][col]=0;
if(col<=MAXCOL-2)
check(row,col+1);
if(col>=1)
check(row,col-1);
if(row>=1)
check(row-1,col);
if(row<=MAXROW-2)
check(row+1,col);
}
}

void main()
{
for(int i=0;i {
for(int j=0;j {
if(number[i][j]==1)
{
staticpoint+=1;
count[staticpoint]=0;
check(i,j);
}
}
}

for(i=1;i<=staticpoint;i++)
{
cout< for(int j=0;j cout< cout< }

}

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/219138/viewspace-915318/,如需轉載,請註明出處,否則將追究法律責任。

相關文章