洛谷p1065迷宫题解和心得(深度搜索)

#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cmath>
using namespace std;
int map[6][6];
bool temp[6][6];
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
int total,fx,fy,sx,sy,T,n,m,l,r;
void walk(int x,int y)
{
    if(x==fx&&y==fy)
    {
        total++;
        return;
    }
    else
    {
        for(int i=0;i<4;i++)
        {
            if(temp[x+dx[i]][y+dy[i]]==0&&map[x+dx[i]][y+dy[i]]==1)
            {
                temp[x][y]=1;
                walk(x+dx[i],y+dy[i]);
                temp[x][y]=0;
            }   
        }
    }
}
int main()
{
    cin>>n>>m>>T;
    for(int xi=1;xi<=n;xi++)
        for(int yi=1;yi<=m;yi++)
            map[xi][yi]=1;
    cin>>sx>>sy;
    cin>>fx>>fy;
    for(int u=1;u<=T;u++)
    {
        cin>>l>>r;
        map[l][r]=0;
    }
    walk(sx,sy);
    cout<<total;
    return 0;
}

  我当时一开始写这题就像这题名字的第一个字一样

  虽然现在还是迷!哈哈

  这题用到了深度搜索

void dfs(/*找到起始坐标*/)//先定义一个深度搜索的函数dfs ()
{
 if(/*找到终点*/)
 {
  //后续操作,在本题中就是total+1
  return; //返回值
 }
 for(//循环遍历所以方向)
 {
   if(//新坐标不符合条件)
   continue;
  //操作
  dfs(//继续向下搜索新的坐标);
  //回溯
 }
 }
 int main ()
 {
  //读入数据
  dfs(//起点坐标);
  return 0;
  }

这种体型之所以难  ,一是对深度搜索还不熟练

         二是对题目的想表达的意思没有清楚

好好努力多多训练以后这种题就简单了

猜你喜欢

转载自www.cnblogs.com/fanwentao/p/11621003.html