40分求助!!!
  • 板块P1605 迷宫
  • 楼主D5ysfzhw
  • 当前回复3
  • 已保存回复4
  • 发布时间2024/3/24 13:42
  • 上次更新2024/3/24 15:29:25
查看原帖
40分求助!!!
1219162
D5ysfzhw楼主2024/3/24 13:42

40分求改

#include <bits/stdc++.h>
using namespace std;
bool vis[666][666]={};
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
int i,j,n,m,t,k,o,p;
int sx,sy,ex,ey,tx,ty,ans=0;
void  dfs(int x,int y)
{	if (x==ex &&y==ey) {ans++;return;}   
 for(int k=0;k<4;k++){//开始访问相邻点 
                 tx=x+dx[k],ty=y+dy[k];

	if (vis[tx][ty]==0 && tx<=n &&ty<=m && ty>=1 &&tx>=1)
	{
		vis[tx][ty]=1; 
		dfs(tx,ty);
		vis[tx][ty]=0;
	}
}}
int main()
{

		cin>>n>>m>>t;

	cin>>sx>>sy>>ex>>ey;
		vis[sx][sy]=1;
	for (int k=1;k<=t;k++)
	{
		cin>>o>>p;
		   vis[o][p]=1;
	}

	dfs(sx,sy);
	cout<<ans;
}

一下是极其相似的满分代码

#include<iostream>
using namespace std; 
bool visit[666][666]={};
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
int n,m,t,ans=0,fx,fy;
void dfs(int a,int b){
    if (a==fx&&b==fy)    
	{ans++;return;}
	for (int i=0;i<=3;i++){
	int A=a+dx[i];int B=b+dy[i]; 
	if  (visit[A][B]==0 and A<=n and B<=m and A>=1 and B>=1 )  //不能越界 ) 
	{visit[A][B]=1;//标记 
	dfs(A,B); 
	visit[A][B]=0;
	//回溯 
	}}}
int main()
{
	int sx,sy,u,v;
	cin>>n>>m>>t;
	cin>>sx>>sy>>fx>>fy;
	visit[sx][sy]=1;
	for (int q=1;q<=t;q++)
	{
		cin>>u>>v;
		visit[u][v]=1;
	}
	dfs(sx,sy);
	cout<<ans;
	return 0;
}

实在找不出有什么问题 请大佬回复 ~互关~

2024/3/24 13:42
加载中...