题解:最短路用bfs暴搜,每走一步就将图上对应位置填上走的步数,找到后再从终点找到起点计有多少方式(用dfs,条件是和上一步的步数差1)。
#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;
const int N = 105;
struct Point {
int x, y;
}p1, p;
int g[N][N], row, col, n, x1, x2, y1, y2;
int num;
int flagx[4] = {0, 0, -1, 1};
int flagy[4] = {-1, 1, 0, 0};
void count(int x, int y) {
if (x == x1 && y == y1) {
num++;
return;
}
for (int i = 0; i < 4; i++) {
int x0 = x + flagx[i];
int y0 = y + flagy[i];
if (x0 <= 0 || x0 > row || y0 <= 0 || y0 > col)
continue;
if (g[x0][y0] == g[x][y] - 1)
count(x0, y0);
}
}
void bfs() {
queue<Point> q;
g[x1][y1] = 1;
p.x = x1;
p.y = y1;
q.push(p);
while (!q.empty()) {
p = q.front();
q.pop();
for (int i = 0; i < 4; i++) {
int x0 = p.x + flagx[i];
int y0 = p.y + flagy[i];
if (x0 <= 0 || x0 > row || y0 <= 0 || y0 > col)
continue;
if (g[x0][y0] == 0) {
p1.x = x0;
p1.y = y0;
g[x0][y0] = g[p.x][p.y] + 1;
q.push(p1);
if (x0 == x2 && y0 == y2)
return;
}
}
}
}
int main() {
scanf("%d%d%d", &row, &col, &n);
num = 0;
int a, b;
for (int i = 0; i < n; i++) {
scanf("%d%d", &a, &b);
g[a][b] = -1;
}
scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
bfs();
if (g[x2][y2]) {
count(x2, y2);
printf("%d\n%d\n", g[x2][y2] - 1, num);
}
else
printf("No Solution!\n");
return 0;
}