题目描述
你所在城市的街道好像一个棋盘,有a条南北方向的街道和b条东西方向的街道。南北方向的a条街道从西到东依次编号为l到a,而东西方向的b条街道从南到北依次编号为l到b,南北方向的街道i和东西方向的街道j的交点记为(i,j)。
你住在(1,1)处,而学校在(a,b)处,你骑自行车去上学,自行车只能沿着街道走,而且为了缩短时间只允许沿着向东和北的方向行驶。
现在有N个交叉路口在施工(X1,Yl)、(X2,Y2)……,(Xn,Yn),这些路口是不能通车的。
问你上学一共有多少走法?
输入格式
第一行包含两个整数a和b,并且满足1≤a,b≤16。
第二行包含一个整数N,表示有N个路口在维修(1≤N≤40)。
接下来N行,每行两个整数X_i,Y_i,描述路口的位置。
输出格式
输出一个整数表示从(1,1)到(a,b)的行车路线总数。
输入输出样例
输入 #1
5 4
3
2 2
2 3
4 2
输出 #1
5
说明/提示
【样例解释】
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#include<cstdio>
#include<math.h>
#include<climits>
#include <map>
using namespace std;
int n,m ;
int a[20][20] ,b[20][20];
int x,y;
int c,d;
int main(){
ios_base::sync_with_stdio(0); // 让cin变快
cin.tie(0); // 让cin变快
cin >> x >> y >> n;
for(int i = 1; i <= n; i++) {
cin >> c >> d;
b[c][d] = 1;
}
for(int i = 1; i <= x; i++) {
if(b[i][1] != 0) break;
a[i][1] = 1;
}
for(int i = 1; i <= y; i++) {
if(b[1][i] != 0) break;
a[1][i] = 1;
}
for(int i =2 ; i <= x ; i++) {
for(int j = 2;j <= y; j++) {
if(b[i][j] == 0 ) {
a[i][j] = a[i-1][j]+a[i][j-1];
}
}
}
cout << a[x][y] << endl;
return 0;
}
另一种
#include<bits/stdc++.h>
using namespace std;
int a,b,k,x,y,sum;//定义
bool s[20][20];//地图,初始化为全false,都不能走
void dfs(int x,int y){//子程序
if(x==a&&y==b){//到学校了
sum++;//加
return;//返回
}
if(s[x][y+1]) dfs(x,y+1);//如果上面能走,走上面
if(s[x+1][y]) dfs(x+1,y);//如果右面能走,走右面
}
int main(){//主程序
cin>>a>>b;//输入
for(int i=1;i<=a;i++){
for(int j=1;j<=b;j++){
s[i][j]=true;//将地图区域全变为能走的,留一个边界为false
}
}
cin>>k;
while(k--){//k后面没用了,k--就好
cin>>x>>y;//输入坐标
s[x][y]=false;//施工不能走(话说这啥路啊,这么多施工的)
}
dfs(1,1);//从1,1开始深搜
cout<<sum;//输出
return 0;//完美结束
}