Catalog
洛谷p1605迷宫

题目地址:https://www.luogu.org/problem/P1605

#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std;
int migong[10][10];
int n,m, fx, fy, dx, dy;
int total;
int barraier;
bool vis[10][10];
int dir[][2] = { {0,1},{1,0},{0,-1},{-1,0} };
void dfs(int x, int y,int step) {
if (dx == x && dy == y) {
total++;
return;
}
for (int i = 0; i < 4; i++) {
int nx = x + dir[i][0];
int ny = y + dir[i][1];
if (vis[nx][ny] == 0 && nx >= 1 && ny >= 1 && x <= n && y <= m) {
vis[nx][ny] = 1;
dfs(nx, ny, step + 1);
vis[nx][ny] = 0;
}
}
}
int main() {
cin >> n >> m >> barraier;
cin >> fx >> fy >> dx >> dy;
for (int i = 1; i <= barraier; i++) {
int tx, ty;
cin >> tx >> ty;
vis[tx][ty] = 1;

}
vis[fx][fy] = 1;不要忘了给初始值打标记
dfs(fx, fy,0);
cout << total;
}
Author: superzhaoyang
Link: http://yoursite.com/2019/11/21/洛谷p1605迷宫/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.
Donate
  • 微信
  • 支付宝
��