上学路线_NOI导刊2009普及(6)

题目详见:上学路线_NOI导刊2009普及(6)

这是一道基础的DFS(深搜)题,堪称模板,是新手练习搜索与回溯的好题选。

大致思路:从(1,1)开始搜索,每次只能往上走或往右走一个格(遇到题目给出的障碍物则直接不走),一直到(a,b),再回来找另一条路。每到一遍(a,b)就让计数器tot+1,最后输出tot,即为最终方案总数。

AC代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
using namespace std;
int k,a,b,f[101][101],tot;
void search(int x,int y)
{
    if(x==a&&y==b)
    {
        tot++;
        return;
    }
    if((x+1<=a)&&f[x+1][y]==0)//往上找 
    search(x+1,y);  
    if((y+1<=b)&&f[x][y+1]==0)//往右找 
    search(x,y+1);
}
int main()
{
    cin>>a>>b>>k;
    for(int i=1;i<=k;i++)
    {
        int x,y;
        cin>>x>>y;
        f[x][y]=1;
    }
    search(1,1);
    cout<<tot;
    return 0;
}

 

 

相关文章:

  • 2021-06-04
  • 2021-07-16
  • 2022-02-01
  • 2021-11-13
  • 2021-11-01
  • 2022-01-08
  • 2021-05-21
  • 2022-01-09
猜你喜欢
  • 2022-01-06
  • 2022-12-23
  • 2022-01-10
  • 2021-06-10
  • 2022-02-24
  • 2022-12-23
  • 2022-02-10
相关资源
相似解决方案