//回溯法
//8*8的格子,每个颜色由1--5组成,给定任一一个格子,求出所有与格子相邻且颜色相同的格子的个数。
int map[8][8] = {
{1,1,2,1,},
{1,1,2,1},
{1,2,1,2},
{1,1,1,1},
{1,1,2,1},
};
int dir[4][2] = { { 1, 0 }, { 0, 1 }, { -1, 0 }, { 0, -1 } };
int count = 1;
bool check[8][8] = {false};
void func(int (*maps)[8], int row, int col)
{
check[row][col] = true;
for (int i = 0; i < 3; i++) //每一步有四种选法
{
int neighborRow = row + dir[i][0];
int neighborCol = col + dir[i][1];
if (neighborRow<=7 && neighborRow >= 0 && neighborCol <=7 && neighborCol >= 0
&& check[neighborRow][neighborCol] == false) //减枝
{
if (maps[row][col] == maps[neighborRow][neighborCol]) //如果颜色相同
{
count++;
func(maps, neighborRow, neighborCol); //继续下一步
}
}
}
}
int _tmain(int argc, _TCHAR* argv[])
{
int row, col;
scanf_s("%d,%d", &row, &col);
func(map, row, col);
printf("%d", count);
getchar();
getchar();
return 0;
}