回复 2楼 炎天
把二楼的代码改进了一下~
感觉学多一点优化代码的能力就提升了一点

~

程序代码:
#include <stdio.h>
#include <stdlib.h>
#define SEARCH(str,x,y) str##[x][y]=='.'||str##[x][y]=='T'
int count = 0;
int t = 999999;
void search(char str[][11], int x, int y)
{
if (str[x][y] == 'T')
{
t = t < count ? t : count;
return;
}
str[x][y] = '#';
count++;
if (SEARCH(str,x-1,y))
search(str, x-1, y);
if (SEARCH(str,x+1,y))
search(str, x+1, y);
if (SEARCH(str,x,y-1))
search(str,x,y-1);
if (SEARCH(str,x,y+1))
search(str,x,y+1);
count--;
str[x][y] = '.';
}
void main()
{
int i = 0;
char str[8][11] = {
{ "..####.#.#" },
{ "..#..#...#" },
{ "..#T##.#.#" },
{ ".........." },
{ "..##.#####" },
{ ".........." },
{ "#####...##" },
{ "###....S##" }
};
search(str, 7, 7);
printf("%d\n", t);
system("pause");
return;
}
[此贴子已经被作者于2017-10-4 11:23编辑过]