#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<Windows.h>
#include<graphics.h>
#include<conio.h>
const int NUM = 50;
struct map
{
int link[NUM][NUM];
int prison;
int all;
int s_num[NUM];
};
struct x_map
{
int x;
int y;
};
IMAGE img1;
IMAGE img2;
int n = 0;
double jisuan(int end, int days, struct map* df, int near_1[NUM])
{
if (days == 0)
{
if (end == df->prison)
{
return (1);
}
else
return (0);
}
double result = 0.0;
for (int i = 0; i < df->all; ++i)
{
double result1 = 0.0;
if ((df->link[end][i]) == 1)
{
result1 = jisuan(i, days - 1, df, near_1);
result = result + result1 / near_1[i];
}
if (result1)
{
df->s_num[n] = i;
n++;
}
}
return result;
}
void drawp(struct x_map df_1[], struct map* df, double res1, int end, int day)
{
initgraph(1200, 800);
loadimage(&img1, _T("./村庄图片.png"), 60, 60);
loadimage(&img2, _T("./监狱.png"), 60, 60);
loadimage(NULL, _T("./山林俯瞰图.jpg"), 1200, 800);
for (int i = 0; i < df->all; i++)
{
if (i == df->prison)
{
putimage(df_1[i].x, df_1[i].y, &img2);
}
else
{
putimage(df_1[i].x, df_1[i].y, &img1);
}
}
for (int i = 0; i < df->all; i++)
{
TCHAR s[5];
_stprintf(s, _T("%d"), i);
settextstyle(20, 16, _T("宋体"));
outtextxy(df_1[i].x, df_1[i].y, s);
}
char buf[100] = {};
char car[100] = {};
sprintf(car, "逃狱第%d天疑犯在%d村的概率是", day, end);
sprintf(buf, "%.8lf", res1);
setlinestyle(PS_SOLID | PS_JOIN_BEVEL, 3);
int j = 0;
for (int i = 0; i < n; i++)
{
int x1, y1, x2, y2;
x1 = df_1[df->s_num[i]].x;
y1 = df_1[df->s_num[i]].y;
if (i == n - 1 || df->s_num[i + 1] == df->prison)
{
x2 = df_1[end].x;
y2 = df_1[end].y;
}
else
{
x2 = df_1[df->s_num[i + 1]].x;
y2 = df_1[df->s_num[i + 1]].y;
}
if (df->s_num[i] == df->prison)
{
j++;
}
switch (j)
{
case 1:setlinecolor(RED); break;
case 2:setlinecolor(BLUE); break;
case 3:setlinecolor(BLACK); break;
case 4:setlinecolor(YELLOW); break;
case 5:setlinecolor(WHITE); break;
}
line(x1, y1, x2, y2);
}
if (MessageBoxA(NULL, buf, car, MB_OK) == IDOK)
{
closegraph();
}
}
void setmap()
{
system("color F8");
printf("*************让我们一起寻找汉尼拔博士吧!!!***************\n");
int num;
printf("请输入测试用例个数:\n");
scanf("%d", &num);
while (num--)
{
struct x_map df1[20] = {};
srand(time(NULL));
int days;
int near_1[NUM];
struct map* df;
df = (map*)malloc(sizeof(map));
printf("请输入村庄的个数:\n");
scanf("%d", &df->all);
printf("请输入逃狱的天数:\n");
scanf("%d", &days);
printf("请输入监狱所在地:\n");
scanf("%d", &df->prison);
int i = 0;
while (1)
{
int res1 = 0;
int res2 = 0;
res1 = rand() % 600;
res2 = rand() % 700;
if (res1 != res2)
{
df1[i].x = res1;
df1[i].y = res2;
i++;
}
else
{
continue;
}
if (i == df->all)
{
break;
}
}
printf("请输入村庄之间的通路:\n");
for (int i = 0; i < df->all; i++)
for (int j = 0; j < df->all; j++)
{
scanf("%d", &df->link[i][j]);
}
int k = 0;
for (int i = 0; i < df->all; i++)
{
int count = 0;
for (int j = 0; j < df->all; j++)
{
if (df->link[i][j])
{
count++;
near_1[k] = count;
}
}
k++;
}
int numbers;
printf("请输入要查找村庄的个数:\n");
scanf("%d", &numbers);
while (numbers--)
{
n = 0;
int end;
printf("请输入要查找的村庄:\n");
scanf("%d", &end);
double res = jisuan(end, days, df, near_1);
drawp(df1, df, res, end, days);
}
free(df);
}
}
int main()
{
setmap();
system("pause");
return 0;
}
|