解析
一道dfs题,入门级的深度搜索,很简单。注意原地打码即可。
代码
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
using namespace std;
typedef pair<int,int> P;
int n,m,fx,fy,times=0;
P mov[] = { {1,0},{-1,0},{0,1},{0,-1}};
char maze[6][6];
void dfs(int i, int j){
if((i==fx) && (fy== j )){
times++;
return;
}
maze[i][j] = '#';
for(int k =0;k<4;k++){
i+= mov[k].first;
j+= mov[k].second;
if( i>=1 && i<=n && j>=1 && j<=m && (maze[i][j]=='.') ){
maze[i][j] = '#';
dfs(i,j);
maze[i][j] = '.';
}
i-= mov[k].first;
j-= mov[k].second;
}
maze[i][j] = '.';
}
int main(){
int t,sx,sy,x,y;
cin>>n>>m>>t;
cin>>sx>>sy>>fx>>fy;
for(auto &r:maze) for(auto &i:r) i='.';
while( t-- ){
cin>>x>>y;
maze[x][y] = '#';
}
dfs(sx,sy);
cout<<times<<endl;
return 0;
}