程序代码:
#include <stdio.h> #include <stdlib.h> 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 (str[x - 1][y] == '.' || str[x - 1][y] == 'T' ) search(str, x - 1, y); if (str[x + 1][y] == '.' || str[x + 1][y] == 'T') search(str, x + 1, y); if (str[x][y - 1] == '.' || str[x][y - 1] == 'T') search(str, x, y - 1); if (str[x][y + 1] == '.' || str[x][y + 1] == 'T') 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", t); system("pause"); return; }