30 50
01010101001011001001010110010110100100001000101010
00001000100000101010010000100000001001100110100101
01111011010010001000001101001011100011000000010000
01000000001010100011010000101000001010101011001011
00011111000000101000010010100010100000101100000000
11001000110101000010101100011010011010101011110111
00011011010101001001001010000001000101001110000000
10100000101000100110101010111110011000010000111010
00111000001010100001100010000001000101001100001001
11000110100001110010001001010101010101010001101000
00010000100100000101001010101110100010101010000101
11100100101001001000010000010101010100100100010100
00000010000000101011001111010001100000101010100011
10101010011100001000011000010110011110110100001000
10101010100001101010100101000010100000111011101001
10000000101100010000101100101101001011100000000100
10101001000000010100100001000100000100011110101001
00101001010101101001010100011010101101110000110101
11001010000100001100000010100101000001000111000010
00001000110000110101101000000100101001001000011101
10100101000101000000001110110010110101101010100001
00101000010000110101010000100010001001000100010101
10100001000110010001000010101001010101011111010010
00000100101000000110010100101001000001000000000010
11010000001001110111001001000011101001011011101000
00000110100010001000100000001000011101000000110011
10101000101000100010001111100010101001010000001000
10000010100101001010110000000100101010001011101000
00111100001000010000000110111000000001000000001011
10000001100111010111010001000110111010101101111000
思路一: 深搜
(当然,最短路径用广搜,深搜就是提供一种找出口的思路,不只限于本题)
- 把文本内容转为(int)二维数组
- 寻找路线(上右下左,判断是否越界,是否已走过,是否可以走,如果不可以走,变0为1,返回上一步)
- r[400] 存每一步的行,l[400]存每一步的列,f[400]存每一步的方向,xh表示走的总步数
#include"stdio.h"
main()
{
// 30 行 50列
// 4 hang 6 lie
freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int a[31][51]={0};
int b[31][51]={0};
int i=0,j=0,fx=0;
char s[60];
int x=0,y=0,row=30, lie = 50;
int r[400]={0};
int l[400]={0};
int f[400]={0};
int xh=0;
int f1=0,f2=0,f3=0,f4=0;
/***************************
*
*fun : 转换为 (int) 二维数组
*
***************************/
for(i=0;i<row;i++)
{
gets(s);
for(j=0;j<lie;j++)
{
a[i][j]=s[j]-'0';
}
//puts(s);
}
/***************************
*
*fun : 打印 (int) 二维数组 a[30][50]
*
***************************/
for(i=0;i<row;i++)
{
for(j=0;j<lie;j++)
printf("%d",a[i][j]);
printf("\n");
}
/***************************
*
*fun : 寻找路线
*
***************************/
x=0;y=0;xh=0;
f1=0,f2=0,f3=0,f4=0;
while(1)
{
/**************
*
* 基线条件
*
**************/
if( x==row-1 && y== lie-1)
break;
/**************
*
* fun : 前进
*
**************/
// 判断是否走过
f1=0,f2=0,f3=0,f4=0;
for(i=0;i<xh+1;i++)
{
// up
if( x-1 ==r[i] && y==l[i] && f1!=1)
{
f1=1;
}
// right
if( x ==r[i] && y+1 ==l[i] && f2!=1 )
{
f2=1;
}
// down
if( x+1 ==r[i] && y==l[i] &&f3!=1 )
{
f3=1;
}
// left
if( x ==r[i] && y-1 ==l[i] && f4!=1)
{
f4=1;
}
}
// up
if(x-1>=0 && a[x-1][y]==0 && f[xh]!=3 && f1==0)
{
x=x-1;
y=y;
fx=1;
xh++;
r[xh]=x;
l[xh]=y;
f[xh]=fx;
printf("x=%d,y=%d,step=%d,xh=%d\n",r[xh],l[xh],f[xh],xh);
}
// right
else if(y+1<lie && a[x][y+1]==0 && f[xh]!=4 && f2==0)
{
x=x;
y=y+1;
fx=2;
xh++;
r[xh]=x;
l[xh]=y;
f[xh]=fx;
printf("x=%d,y=%d,step=%d,xh=%d\n",r[xh],l[xh],f[xh],xh);
}
// down
else if(x+1<row && a[x+1][y]==0 && f[xh]!=1 && f3==0)
{
x=x+1;
y=y;
fx=3;
xh++;
r[xh]=x;
l[xh]=y;
f[xh]=fx;
printf("x=%d,y=%d,step=%d,xh=%d\n",r[xh],l[xh],f[xh],xh);
}
// left
else if(y-1>=0 && a[x][y-1]==0 && f[xh]!=2 && f4==0)
{
x=x;
y=y-1;
fx=4;
xh++;
r[xh]=x;
l[xh]=y;
f[xh]=fx;
printf("x=%d,y=%d,step=%d,xh=%d\n",r[xh],l[xh],f[xh],xh);
}
else
{
f[xh] = -f[xh];
r[xh]=0;
l[xh]=0;
printf("原路返回\n");
}
/***************
*
* fun : 原路返回
*
***************/
if(f[xh]<0)
{
a[x][y]=1;
switch (f[xh])
{
case -1: x=x+1;y=y;break;
case -2: x=x;y=y-1;break;
case -3: x=x-1;y=y;break;
case -4: x=x;y=y+1;break;
}
f[xh]=0;
xh--;
}
}
/***************************
*
* 输出路线
*
***************************/
printf("\n****正确路线*****\n");
for(i=0;i<xh+1;i++)
{
printf("x=%d,y=%d,fx=%d , step=%d\n",r[i],l[i],f[i],i);
b[r[i]][l[i]]=1;
}
for(i=0;i<row;i++)
{
for(j=0;j<lie;j++)
{
if(b[i][j]==0)
printf(" ");
else
printf(".");
//printf("%2d",b[i][j]);
}
printf("\n");
}
fclose(stdin);
return 0;
}