原始效果32*32
00000000000000222222222220000000
00000000000000111111111110000000
00000000000000111111111110000000
00000000000000111111111110000000
00000000000000111111111110000000
00000000000000111111111110000000
00000000000000000111111110000000
00000000000000000111111110000000
21110000000000000000000000000000
21110000000000000000000000000000
21111100000000000000000000000000
21111111100000000000000000000000
21111111100000000000000000000000
21111111100000111111110000000002
21111111100000111111110000011112
21111111100001111111110000011112
21111111100001111111110000011112
21111111100111111111110000011112
21111111100111111111110000011112
21110000000111111111110000111112
00000000000111111111110000111112
00000000000111111111100000111112
00000000000111111111100000111112
00000000000111111110000000111112
00000000000000000000000000111112
00000000000000000000000000111112
00000000000011111111000000111112
00001111111111111111000000011112
00001111111111111111000000011110
00001111111111111111000000011110
00001111111111111111000000000000
00000022220200000000000000000000想要达到的效果32*32
00000000000000222222222220000000
00000000000000222222222220000000
00000000000000222222222220000000
00000000000000222222222220000000
00000000000000222222222220000000
00000000000000222222222220000000
00000000000000000222222220000000
00000000000000000222222220000000
22220000000000000000000000000000
22220000000000000000000000000000
22222200000000000000000000000000
22222222200000000000000000000000
22222222200000000000000000000000
22222222200000111111110000000002
22222222200000111111110000022222
22222222200001111111110000022222
22222222200001111111110000022222
22222222200111111111110000022222
22222222200111111111110000022222
22220000000111111111110000222222
00000000000111111111110000222222
00000000000111111111100000222222
00000000000111111111100000222222
00000000000111111110000000222222
00000000000000000000000000222222
00000000000000000000000000222222
00000000000022222222000000222222
00002222222222222222000000022222
00002222222222222222000000022220
00002222222222222222000000022220
00002222222222222222000000000000
00000022220200000000000000000000如上图所例,已知1个32*32的排序
如何把与四条边的2相接的1全变成2
不与四条边的2相接1,也就是不与边上2相接不变
搞了一个晚上了,没搞出来
头脑彻底短路了
向数学高手求解VC实现?for (i=1;i<=32;i++)
for (j=1;j<32;j++)
{ CODE???
}
dfs依次扩展每个2的四周
int bufferDes[32][32] = buffer;
for (i=1;i <=32;i++)
for (j=1;j <32;j++)
{
//判断左上是否为1
int n,k;
n = i - 1;
k = j -1;
if(n >= 0)
{
if(k >= 0)
{
if(buffer[i][j] == 1)
{
bufferDes[i][j] = 2;
continue;
}
}
} // 判断同行左是否为1
....
}
末行末列末做处理那么从1行开始
for(i=1; i<height-1; i++) //行
for(j=1; j<width-1; j++) //列
//上边(i,j-1)左边(i-1,j)左上(i-1,j-1)右上(i+1,j+1)右边(i+1,j)
if (Label(i,j-1)==2|| label(i-1,j)==2 || Label(i-1,j-1)==2 || Label(i+1,j+1)==2 || Label(i+1,j)==2)
Label(i,j)=2;居然是这样,末行末列结果不要看00000000000000222222222220000000
00000000000000111111111110000000
00000000000000111111111110000000
00000000000000111111111110000000
00000000000000111111111110000000
00000000000000111111111110000000
00000000000000000111111110000000
00000000000000000111111110000020
22222222222222222222222222222220
22222222222222222222222222222220
22222222222222222222222222222220
22222222222222222222222222222220
22222222222222222222222222222220
22222222222222222222222222222220
22222222222222222222222222222220
22222222222222222222222222222220
22222222222222222222222222222220
22222222222222222222222222222220
22222222222222222222222222222220
22222222222222222222222222222220
00000000000111111111110000111110
00000000000111111111100000111110
00000000000111111111100000111110
00000000000111111110000000111110
00000000000111111110000000111110
00000000000000000000000000111110
00000000000011111111000000111110
00001111111111111111000000011110
00001111111111111111000000011110
00001111111111111111000000011110
00001111111111111111000000000000
00000000000000000000000000000000
// [email protected]#include "stdafx.h"
#include <malloc.h>void fill(char *des, int x, int y)
{
des[x*33+y] = '2'; if(des[x*33+y-1] == '1')
fill(des, x, y-1);
if(des[x*33+y+1] == '1')
fill(des, x, y+1);
if(des[(x-1)*33+y] == '1')
fill(des, x-1, y);
if(des[(x+1)*33+y+1] == '1')
fill(des, x+1, y);
}
int _tmain(int argc, _TCHAR* argv[])
{
FILE *fp = NULL; if( NULL == (fp = fopen("src.txt", "r")) )
return 0; char *pbuffer = NULL;
pbuffer = (char *)malloc(32*33); printf("Src Data:\n");
for(int i = 0;i < 32;i ++)
{
fscanf(fp, "%s", pbuffer);
printf("%s\n", pbuffer);
pbuffer += 32; if(31 != i)
*pbuffer = '\n';
else
*pbuffer = '\0';
pbuffer ++;
}
pbuffer -= 32*33; //primary code
for(int i = 1; i < 31;i ++)
{
for(int j = 1;j < 31;j ++)
{
if( '1' == pbuffer[i*33+j])
{
if( '2' == pbuffer[i*33+j-1] ||
'2' == pbuffer[i*33+j+1] ||
'2' == pbuffer[(i-1)*33+j] ||
'2' == pbuffer[(i+1)*33+j] )
fill(pbuffer, i, j);
}
}
} printf("\nResult:\n");
printf("%s\n", pbuffer);
free(pbuffer);
pbuffer = NULL; fclose(fp); printf("\nPress any key to exit!\n");
getchar();
return 0;
}