fjnu 1354 小鼠迷宫问题

Description

小鼠a与小鼠b身处一个m×n的迷宫中,如图所示。每一个方格表示迷宫中的一个房间。这m×n个房间中有一些房间是封闭的,不允许任何人进入。在迷宫中任何位置均可沿上,下,左,右4个方向进入未封闭的房间。小鼠a位于迷宫的(p,q)方格中,它必须找出一条通向小鼠b所在的(r,s)方格的路。请帮助小鼠a找出所有通向小鼠b的最短道路。


对于给定的小鼠的迷宫,编程计算小鼠a通向小鼠b的所有最短道路。

Input

第一行有3个正整数n,m,k,分别表示迷宫的行数,列数和封闭的房间数。接下来的k行中,每行2个正整数,表示被封闭的房间所在的行号和列号。最后的2行,每行也有2个正整数,分别表示小鼠a所处的方格(p,q)和小鼠b所处的方格(r,s)。

Output

输出小鼠a通向小鼠b的最短路长度和有多少条不同的最短路。
第一行是最短路长度,第2行是不同的最短路数。
如果小鼠a无法通向小鼠b则输出“No Solution!”。

Sample Input

8 8 3
3 3
4 5
6 6
2 1
7 7

Sample Output

11
96

 

KEY:算最短路径我用了广度搜索,算条数的时候,我用了回溯的方法……,这题是我以低年级比赛的题目,好怀念啊,当时感觉好难的……;

 

Source:

#include
< iostream >
using   namespace  std;

struct  node 
{
    
int x;
    
int y;
}
;

class  Queue
{
    
int front;
    
int rear;
    node elem[
10001];
public:
    Queue()
    
{
        front
=rear=1;
    }

    
void EnQueue(node x);
    node DeQueue();
    
int Empty();
}
;

void  Queue::EnQueue(node x)
{
    elem[rear
++]=x;
}


node Queue::DeQueue()
{
    
return elem[front++];
}


int  Queue::Empty()
{
    
if(front==rear) return 1;
    
else return 0;
}


int  a[ 100 ][ 100 ];
int  dx[ 5 ] = {2,-1,0,1,0} ;
int  dy[ 5 ] = {2,0,1,0,-1} ;
int  n,m;
int  p,q,r,s;
int  N;
node S,E;
int  T;

void  init()
{
    
int i,j;
    
for(i=0;i<=n+1;i++)
        
{
            a[
0][i]=-1;
            a[i][
0]=-1;
            a[n
+1][i]=-1;
            a[i][m
+1]=-1;
        }

    
for(i=1;i<=n;i++)
        
for(j=1;j<=m;j++)
            a[i][j]
=0;
    
int x,y;
    
for(i=1;i<=N;i++)
    
{
        cin
>>x>>y;
        a[x][y]
=-1;
    }

    cin
>>p>>q;
    cin
>>r>>s;
    S.x
=p;
    S.y
=q;
    E.x
=r;
    E.y
=s;
}


void  shortest()
{
    Queue Q;
    Q.EnQueue(S);
    a[S.x][S.y]
=1;
    
int i;
    node t;
    
while(!Q.Empty())
    
{
        t
=Q.DeQueue();
        node tmp;
        
for(i=1;i<=4;i++)
        
{
            tmp
=t;
            tmp.x
+=dx[i];
            tmp.y
+=dy[i];
            
if(a[tmp.x][tmp.y]==0)
            
{
                a[tmp.x][tmp.y]
=a[t.x][t.y]+1;
                Q.EnQueue(tmp);
            }

        }

        
/*for(i=0;i<=n+1;i++)
        {
            for(int j=0;j<=m+1;j++)
                cout<<"  "<<a[i][j];
            cout<<endl;
        }
        cout<<endl;
*/

    }

}


void  search( int  x, int  y,node v)
{
    
if(v.x==x&&v.y==y) T++;
    
else
    
{
        
for(int i=1;i<=4;i++)
        
{
            
if(a[x+dx[i]][y+dy[i]]==a[x][y]-1)
                search(x
+dx[i],y+dy[i],v);
        }

    }

}


int  main()
{
//    freopen("fjnu_1354.in","r",stdin);
    cin>>n>>m>>N;
    init();
    shortest();
    
if(a[r][s]==0) cout<<"No Solution!"<<endl;
    
else
    
{
        cout
<<a[r][s]-1<<endl;
        search(E.x,E.y,S);
        cout
<<T<<endl;
    }

    
return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值