简单深搜
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cmath>
#include<string.h>
#include<cstring>
#include<string>
#include<map>
#include<set>
#include<vector>
#include<ctime>
#include<queue>
#define hash hashh
using namespace std;
typedef long long ll;
#define sp system("pause")
#define PB push_back
int G[5][5];
int vis[5][5];
int dx[4] = { 0, 0, 1, -1 };
int dy[4] = { 1, -1, 0, 0 };
class node
{
public:
int now;
int x, y;
int px[26];
int py[26];
};
int main()
{
memset(vis, 0, sizeof vis);
memset(G, 0, sizeof G);
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
scanf("%d", &G[i][j]);
}
node start;
start.now = 0;
start.x = 0;
start.y = 0;
start.px[start.now] = 0;
start.py[start.now] = 0;
queue<node>q;
q.push(start);
vis[0][0] = 1;
while (!q.empty())
{
node t = q.front();
if (t.x == 4 && t.y == 4)
{
for (int i = 0; i <= t.now; i++)
{
printf("(%d, %d)\n", t.px[i], t.py[i]);
}
sp;
return 0;
}
q.pop();
for (int i = 0; i < 4; i++)
{
int a = t.x + dx[i];
int b = t.y + dy[i];
if (a >= 0 && a< 5 && b>= 0 &&b < 5&&!vis[a][b]&&!G[a][b])
{
vis[a][b] = 1;
node in;
in.now = t.now + 1;
in.x = t.x + dx[i];
in.y = dy[i] + t.y;
memcpy(in.px, t.px, sizeof t.px);
memcpy(in.py, t.py, sizeof t.py);
in.px[in.now] = in.x;
in.py[in.now] = in.y;
q.push(in);
}
}
}
return 0;
}