
題解:dfs搜尋
#include <iostream>
#include <algorithm>
#include <cstring>
#include <stdio.h>
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 sx,sy,fx,fy,total,l,r,n,m;
void walk(int x,int y)//x和y分别表示走到的點
{
if(x==fx&&y==fy)//如果剛好達到終點,則方案加1
{
total++;
return ;
}
else
{
for(int i=0;i<=3;i++)//i《=3意思就是四個方向進行周遊
{
int a=x+dx[i];//下一個位置
int b=y+dy[i];
if(temp[a][b]==0&&map[a][b]==1)//判斷沒有走過,并且沒有障礙
{
temp[x][y]=1;//标記
walk(a,b);//繼續搜下一個點
temp[x][y]=0;//恢複
}
}
}
}
int main ()
{
int n,m,t;
cin>>n>>m>>t;
for(int ix=1;ix<=n;ix++)//把所給的區域變成1
{
for(int iy=1;iy<=m;iy++)
{
map[ix][iy]=1;
}
}
cin>>sx>>sy>>fx>>fy;
int l,r;
for(int u=0;u<t;u++)
{
cin>>l>>r;
map[l][r]=0;//障礙标記成0
}
walk(sx,sy);//搜尋一般都是起點坐标
cout<<total;
return 0;
}