1 solutions

  • 1
    @ 2022-8-28 19:12:19

    本题其实就是一道搜索题,在洛谷有一道基本一样的题

    我用的是dfs,具体代码如下:

    #include <iostream>
    using namespace std;
    const int N=5;
    int m,n,t,sx,sy,fx,fy,zx,zy,ans;
    int ma[N][N];
    int opx[5]={1,-1,0,0};
    int opy[5]={0,0,1,-1};
    void dfs(int x,int y)
    {
    	if(x==fx&&y==fy) ans++;//能走到终点说明有一条路径
    	else{
    		int nex,ney;
    		for(int i=0;i<4;i++){
    			nex=x+opx[i];
    			ney=y+opy[i];
    			if(nex>=1&&nex<=n&&ney>=1&&ney<=m&&ma[nex][ney]==0)//保证下一个点不越界,且可通过
    			{
    				ma[nex][ney]=1;
    				dfs(nex,ney);
    				ma[nex][ney]=0;
    			}
    		}
    		
    	}
    }
    int main()
    {
    	cin>>n>>m>>t;
    	cin>>sx>>sy>>fx>>fy;
    	while(t--)
    	{
    		cin>>zx>>zy;
    		ma[zx][zy]=1;//将障碍点做标记
    	}
    	ma[sx][sy]=1;
    	dfs(sx,sy);
    	cout<<ans<<endl;
    	return 0;
    }
    

    Information

    ID
    6533
    Time
    1000ms
    Memory
    256MiB
    Difficulty
    5
    Tags
    # Submissions
    68
    Accepted
    26
    Uploaded By