欢迎来到_qcr的博客

Luogu [P1958] 上学路线_NOI导刊2009普及(6)

上学路线_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;
}

 

 
posted @ 2018-03-04 18:44  青珹  阅读(198)  评论(0编辑  收藏  举报
Live2D