简单题
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
#define maxn 25
struct Point
{
int x, y;
} s, e;
bool map[maxn][maxn];
int n, m, ans;
bool found;
int dir[4][2] =
{
{ 0, 1 },
{ 1, 0 },
{ 0, -1 },
{ -1, 0 } };
void input()
{
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
{
int a;
scanf("%d", &a);
if (a == 1)
map[i][j] = false;
else
map[i][j] = true;
if (a == 2)
{
s.x = i;
s.y = j;
}
if (a == 3)
{
e.x = i;
e.y = j;
}
}
}
int fail(Point &a)
{
if (a.x < 0 || a.y < 0 || a.x >= n || a.y >= m)
return -1;
if (!map[a.x][a.y])
return 1;
return 0;
}
void dfs(Point &a, int step)
{
if (step >= 10)
return;
Point b;
for (int i = 0; i < 4; i++)
{
b.x = a.x + dir[i][0];
b.y = a.y + dir[i][1];
if (fail(b))
continue;
int x;
while (!(x = fail(b)))
{
if (b.x == e.x && b.y == e.y)
{
found = true;
ans = min(ans, step + 1);
return;
}
b.x += dir[i][0];
b.y += dir[i][1];
}
if (x == -1)
continue;
map[b.x][b.y] = true;
b.x -= dir[i][0];
b.y -= dir[i][1];
dfs(b, step + 1);
b.x += dir[i][0];
b.y += dir[i][1];
map[b.x][b.y] = false;
}
}
int main()
{
//freopen("t.txt", "r", stdin);
while (scanf("%d%d", &m, &n), m | n)
{
input();
found = false;
ans = 20;
dfs(s, 0);
if (found)
printf("%d\n", ans);
else
printf("%d\n", -1);
}
return 0;
}