嗯嗯,这个不是我自己写,是引用某人的~看看大神们对这个的看法,没人回就算了~~~
附加出处~
https://bbs.bccn.net/thread-335497-1-2.html
其实就是论坛上以前的一个贴~
程序代码:
#include<stdio.h>
#include<stdlib.h>
typedef struct Migongg{
int x;
int y;
int fangxiang;
char ch;
}Migong;
Migong MiG[7][25];
Migong CC[7][25];
int max = 1110;
int count = 0;
Migong start[300];
int i, j;
void Begin(char a[][25])
{
for(i = 0; i < 7; i++)
{
for(j = 0; j < 25; j++)
{
MiG[i][j].ch = a[i][j];
}
}
}
void go()
{
int fang=0, found = 0, tpp = 0;
start[tpp].x = 1, start[tpp].y = 1, start[tpp].fangxiang = 0;
while(tpp > -1)
{
i = start[tpp].x, j= start[tpp].y , fang = start[tpp].fangxiang;
if(MiG[start[tpp].x][start[tpp].y].ch == ' ')
{
MiG[start[tpp].x][start[tpp].y].ch = '@';
count++;
}
if(start[tpp].x == 5 && start[tpp].y == 22)
{
if(count < max)
{
max = count;
for(i = 0; i < 7; i++)
{
for(j = 0; j < 25; j++)
{
CC[i][j].ch = MiG[i][j].ch;
}
}
}
count = 0;
MiG[start[tpp].x][start[tpp].y].ch = ' ';
tpp--;
i = start[tpp].x, j = start[tpp].y, fang = start[tpp].fangxiang;
}
found = 0;
while(found==0 && fang < 5)
{
fang++;
switch(fang)
{
case 1: i=start[tpp].x-1, j=start[tpp].y; break;
case 2: i=start[tpp].x, j=start[tpp].y+1; break;
case 3: i=start[tpp].x+1, j=start[tpp].y; break;
case 4: i=start[tpp].x, j=start[tpp].y-1; break;
}
if(MiG[i][j].ch == ' ')
{
found=1;
}
}
if(found == 1)
{
start[tpp].fangxiang = fang;
tpp++;
start[tpp].x = i;
start[tpp].y = j;
start[tpp].fangxiang = 0;
}
else
{
MiG[start[tpp].x][start[tpp].y].ch = ' ';
tpp--;
}
}
}
int main(void)
{
char mg[7][25] = {{"************************"},
{"* ********* **** * ** *"},
{"* ******** *** * *"},
{"* ** ** ******* **"},
{"* *** *** ****** *"},
{"* ***** **** *"},
{"************************"}};
Begin(mg);
go();
for(i = 0; i < 7; i++ ,printf("\n"))
{
for(j = 0; j < 25; j++)
{
printf("%c", CC[i][j].ch);
}
}
return 0;
}
[此贴子已经被作者于2016-12-27 12:53编辑过]