题目比较简单 直接dfs,当然也可以用bfs来做,dfs更加简单,明了。我们还是用dfs吧~
这里有个要注意的点,题目没有说迷宫的坐标是从0开始还是从1开始,这个时候你要去看一下样例,很明显样例会告诉你数组下标从1开始.下面是AC代码~
#include <iostream>
#include <cstring>
#include <utility>
using namespace std;
int N,M,T;
#define Max 666
int a[Max][Max];
int pos[5][5]={{0,0,0},{0,-1,0},{0,0,1},{0,1,0},{0,0,-1}};
bool flag[Max][Max];
pair<int,int > beg,en;
pair<int ,int > now;
bool check(pair<int ,int > now,int x,int y);
void dfs();
int res=0;
int main()
{
memset(a,0, sizeof(a));
memset(flag,false, sizeof(flag));
cin>>N>>M>>T;
cin>>beg.first>>beg.second>>en.first>>en.second;
now.first=beg.first;
now.second=beg.second;
flag[beg.first][beg.second]=true; //起点标记为已走
for(int i=1;i<=T;i++)
{
int nx,ny;
cin>>nx>>ny;
a[nx][ny]=-1;//障碍物
}
dfs();
cout<<res<<endl;
return 0;
}
void dfs()
{
if(now.first==en.first&&now.second==en.second)
{
res++;
return ;
}
else
{
for(int i=1;i<=4;i++)
{
if(check(now,pos[i][1],pos[i][2]))
{
flag[now.first+pos[i][1]][now.second+pos[i][2]]=true;
int nx=now.first,ny=now.second;
now.first=now.first+pos[i][1];
now.second=now.second+pos[i][2];
dfs();
flag[now.first][now.second]=false;
now.first=nx;
now.second=ny;
}
}
}
}
bool check(pair<int ,int > now,int x,int y)
{
int nx=now.first+x;
int ny=now.second+y;
if(nx>=1&&nx<=M&&ny>=1&&ny<=N&&!flag[nx][ny]&&a[nx][ny]!=-1)
{
return true;
}
return false;
}