终于AC了,WA了一下午,各种错误,人品确实有问题,最终看了题解,对着题解又找了好久的错,在此要感谢http://blog.csdn.net/lyy289065406/article/details/6647668的详细题解。
题目大意:
在一个迷宫的S到E,求一直往左优先走和一直往右走优先以及最短路所经过的格子数。
#include <stdio.h>
#include <stdlib.h>
typedef struct{
int y,x;
int depth;
}SE;
SE s, e; // 起止点;
int map[42][42];
int lsteps;
int rsteps;
void DFS_LF(int i,int j,int d){ //当前位置的行,列和方向;
++lsteps;
if(i==e.y&&j==e.x)
return ;
switch(d){ //左优先 1032
case 0:
{
if(map[i][j-1])
DFS_LF(i,j-1,1);
else if(map[i-1][j])
DFS_LF(i-1,j,0);
else if(map[i][j+1])
DFS_LF(i,j+1,3);
else if(map[i+1][j])
DFS_LF(i+1,j,2);
break;
}
case 1:
{ //左优先 2103
if(map[i+1][j])
DFS_LF(i+1,j,2);
else if(map[i][j-1])
DFS_LF(i,j-1,1);
else if(map[i-1][j])
DFS_LF(i-1,j,0);
else if(map[i][j+1])
DFS_LF(i,j+1,3);
break;
}
case 2:
{ //3210
if(map[i][j+1])
DFS_LF(i,j+1,3);
else if(map[i+1][j])
DFS_LF(i+1,j,2);
else if(map[i][j-1])
DFS_LF(i,j-1,1);
else if(map[i-1][j])
DFS_LF(i-1,j,0);
break;
}
case 3:
{ //左优先 0321
if(map[i-1][j])
DFS_LF(i-1,j,0);
else if(map[i][j+1])
DFS_LF(i,j+1,3);
else if(map[i+1][j])
DFS_LF(i+1,j,2);
else if(map[i][j-1])
DFS_LF(i,j-1,1);
break;
}
}
return ;
}
void DFS_RF(int i,int j,int d){
++rsteps;
if(i==e.y&&j==e.x)
return;
switch(d)
{
case 0:
{
if(map[i][j+1])
DFS_RF(i,j+1,3);
else if(map[i-1][j])
DFS_RF(i-1,j,0);
else if(map[i][j-1])
DFS_RF(i,j-1,1);
else if(map[i+1][j])
DFS_RF(i+1,j,2);
break;
}
case 1:
{
if(map[i-1][j])
DFS_RF(i-1,j,0);
else if(map[i][j-1])
DFS_RF(i,j-1,1);
else if(map[i+1][j])
DFS_RF(i+1,j,2);
else if(map[i][j+1])
DFS_RF(i,j+1,3);
break;
}
case 2:
{
if(map[i][j-1])
DFS_RF(i,j-1,1);
else if(map[i+1][j])
DFS_RF(i+1,j,2);
else if(map[i][j+1])
DFS_RF(i,j+1,3);
else if(map[i-1][j])
DFS_RF(i-1,j,0);
break;
}
case 3:
{
if(map[i+1][j])
DFS_RF(i+1,j,2);
else if(map[i][j+1])
DFS_RF(i,j+1,3);
else if(map[i-1][j])
DFS_RF(i-1,j,0);
else if(map[i][j-1])
DFS_RF(i,j-1,1);
break;
}
}
return;
}
void BFS_MIN(int i,int j){
int visit[42][42]={0};
SE queue[1700]; //队列
int head=0,tail=0;
queue[head].y=i;
queue[tail].x=j;
queue[tail++].depth=1;
visit[i][j]=1;
while(head<tail){
SE tmp=queue[head++];
if(tmp.y==e.y&&tmp.x==e.x){
printf("%d\n",tmp.depth);
return;
}
if(map[tmp.y-1][tmp.x]&&!visit[tmp.y-1][tmp.x]){
visit[tmp.y-1][tmp.x]=1;
queue[tail].y=tmp.y-1; //入队
queue[tail].x=tmp.x;
queue[tail++].depth=tmp.depth+1;
}
if(map[tmp.y][tmp.x-1]&&!visit[tmp.y][tmp.x-1]){
visit[tmp.y][tmp.x-1]=1;
queue[tail].y=tmp.y;
queue[tail].x=tmp.x-1;
queue[tail++].depth=tmp.depth+1;
}
if(map[tmp.y+1][tmp.x]&&!visit[tmp.y+1][tmp.x]){
visit[tmp.y+1][tmp.x]=1;
queue[tail].y=tmp.y+1;
queue[tail].x=tmp.x;
queue[tail++].depth=tmp.depth+1;
}
if(map[tmp.y][tmp.x+1]&&!visit[tmp.y][tmp.x+1]){
visit[tmp.y][tmp.x+1]=1;
queue[tail].y=tmp.y;
queue[tail].x=tmp.x+1;
queue[tail++].depth=tmp.depth+1;
}
}
return;
}
int main(){
//freopen("fang.in","r",stdin);
//freopen("fang.out","w",stdout);
int i, j, direction;
int w,h;
int test;
scanf("%d", &test);
while (test--){
scanf("%d%d", &w,&h);
// memset(map, 0, sizeof(map));
for(i=0;i<=h;i++)
for(j=0;j<=w;j++)
map[i][j]=0;
lsteps=1;
rsteps=1;
for (i = 1; i <= h; i++)
for (j = 1; j <= w; j++){
char tmp;
scanf(" %c", &tmp);
if (tmp == 'S'){
s.y = i;
s.x = j;
map[i][j] = 1;
if (h == i)
direction = 0;
else if (1 == i)
direction = 2;
else if (1 == j)
direction = 3;
else if (w == j)
direction = 1;
}
else if (tmp == 'E'){
e.y = i;
e.x = j;
map[i][j] = 1;
}
else if (tmp == '.'){
map[i][j] = 1;
}
}
switch (direction)
{
case 0:{
DFS_LF(s.y-1,s.x,0);
break;
}
case 1:{
DFS_LF(s.y,s.x-1,1);
break;
}
case 2:{
DFS_LF(s.y+1,s.x,2);
break;
}
case 3:{
DFS_LF(s.y,s.x+1,3);
break;
}
}
printf("%d ",lsteps);
switch(direction)
{
case 0:{
DFS_RF(s.y-1,s.x,0);
break;
}
case 1:{
DFS_RF(s.y,s.x-1,1);
break;
}
case 2:{
DFS_RF(s.y+1,s.x,2);
break;
}
case 3:{
DFS_RF(s.y,s.x+1,3);
break;
}
}
printf("%d ",rsteps);
BFS_MIN(s.y,s.x);
}
return 0;
}