求助。。。dfs
查看原帖
求助。。。dfs
337753
xzy15326682521楼主2020/5/29 20:15
#include<iostream>
using namespace std;
int n,m,sx,sy,fx,fy,num=0,vis[10][10],mp[10][10],t;
int dir[4][2]={{0,1},{1,0},{-1,0},{0,-1}};
void dfs(int sx,int sy)
{
    int tx,ty;
    if(sx==fx&&sy==fy)
    {
        num++;
        return;
    }

    for(int i=0;i<4;i++)
    {
        tx=sx+dir[i][0];
        ty=sy+dir[i][1];
        if(tx<1||tx>n||ty<1||ty>m)  continue;
        if(vis[tx][ty]==0&&mp[tx][ty]==0)
        {
            vis[tx][ty]=1;
            dfs(tx,ty);
            vis[tx][ty]=0;
        }
    }
    return;
}
int main()
{
    int a,b;
    cin>>n>>m>>t;
    cin>>sx>>sy>>fx>>fy;
    for(int i=1;i<=t;i++)
    {
        cin>>a>>b;
        mp[a][b]=1;//1 障碍
    }
    dfs(sx,sy);
    cout<<num<<endl;
    return 0;
}

2020/5/29 20:15
加载中...