问题标题: 酷町堂:5014

0
0
已解决
林熙彭
林熙彭
资深守护
资深守护

#include <iostream>
using namespace std;
int sx, sy, fx, fy;
int vis[10][10], G[10][10];
const int dir[8][2] = {
{-1, 0}, {-1, 1}, {0, 1}, {1, 1},
{1, 0}, {1, -1}, {0, -1}, {-1, -1}
};
int out[10005][3], total;
bool valid(int x, int y) {
if(x >= 1 && x <= 8 && y >= 1 && y <= 8) return true;
return false;
}
void print(int n) {
for(int i=1; i<=n; i++) {
if(i != n)
cout << "(" << out[i][1] << "," << out[i][2] << ")" << "->";
else
cout << "(" << out[i][1] << "," << out[i][2] << ")";
}
cout << endl;
}
void dfs(int x, int y, int num) {
// cout << x << " " << y << endl;
out[num][1] = x;
out[num][2] = y;
if(x == fx && y == fy) {
total ++;
if(total <= 5)
print(num);
}
for(int i=0; i<8; i++) {
int nx = x + dir[i][0], ny = y + dir[i][1];
if(valid(nx, ny) && vis[nx][ny] == 0 && G[nx][ny] == 0) {
vis[nx][ny] = 1;
dfs(nx, ny, num + 1);
vis[nx][ny] = 0;
}
}
}
int main() {
cin >> sx >> sy >> fx >> fy;
for(int i=1; i<=8; i++)
for(int j=1; j<=8; j++)
cin >> G[i][j];
vis[sx][sy] = 1;
dfs(sx, sy, 1);
cout << total;
return 0;
}//找错!!!!!!
90分,在线等!!!急!!!!!!!


0
已采纳
汪恺恒
汪恺恒
中级启示者
中级启示者

在 x==fx && y==fy 时,说明已经找到终点了,要return

0
我要回答