一道简单的搜索题,由于限制了只能走10步,故直接深搜就行(貌似BFS过不了,我没有试)。
虽然是道水题,不过我竟然wa了三次……这道题目要注意两个地方
1、冰壶不是一定要停在G处,只要到达G处都算success
2、冰壶只有在碰到石头(也就是1)的时候才会停下,然后才能进行下一步移动(遇到边界不会停,也不能进行下一步移动)
果然学好英语很重要……
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
int map[30][30], stx, sty, ans, w, h;
const int mx[] = { 0,1,0,-1 }, my[] = { 1,0,-1,0 };
void dfs(int x, int y, int step)
{
if (step >= ans) return;
if (step == 10) return;
for (int i = 0; i < 4; i++)
{
int nx = x + mx[i], ny = y + my[i];
if (nx <= 0 || nx > w || ny <= 0 || ny > h || map[ny][nx] == 1)
continue;
while (map[ny][nx] == 0 && nx > 0 && nx <= w && ny > 0 && ny <= h)
nx += mx[i], ny += my[i];
if (map[ny][nx] == 3)
{
ans = min(ans, step + 1);
return;
}
if (map[ny][nx]==1)
{
map[ny][nx] = 0;
dfs(nx - mx[i], ny - my[i], step + 1);
map[ny][nx] = 1;
}
}
}
int main()
{
int a;
cin >> w >> h;
while (w)
{
memset(map, 0, sizeof(map));
for (int i = 1; i <= h; i++)
for (int j = 1; j <= w; j++)
{
cin >> a;
if (a == 2)
{
stx = j;
sty = i;
a = 0;
}
map[i][j] = a;
}
ans = 11, dfs(stx, sty, 0);
if (ans < 11) cout << ans << endl;
else cout << -1 << endl;
cin >> w >> h;
}
return 0;
}