原题:http://www.acmerblog.com/taobao-1246.html
原题是广搜
下面是自己写的深搜:
#include<iostream>
using namespace std;
#define max 10
int dirx[] = { 0, 0, -1, 1 };
int diry[] = { 1, -1, 0, 0 };
int map[max][max];
int visited[max][max];
int totaltime=INT_MAX;
bool flag = false;
int w, h;
void dfs(int step, int x, int y, float oil)
{
if (map[x][y] == 2)
{
if (oil >= 0.5)
{
flag = true;
if (step < totaltime)
{
totaltime = step;
}
}
return;
}
if (oil == 0)
{
return;
}
if (map[x][y] == 3)
{
oil = 4;
}
for (int i = 0; i < 4; i++)
{
if (x + dirx[i] >= w || x + dirx[i] < 0 || y + diry[i] >= h || y + diry[i] < 0)
continue;
if (map[x + dirx[i]][y + diry[i]] != -1&&visited[x+dirx[i]][y+diry[i]]==0)
{
visited[x + dirx[i]][y + diry[i]] = 1;
dfs(step + 1, x + dirx[i], y + diry[i], oil - 0.5);
visited[x + dirx[i]][y + diry[i]] = 0;
}
}
}
int main()
{
int ex, ey;
while (cin >> w >> h)
{
if (w == 0 && h == 0)
break;
for (int i = 0; i < h; i++)
{
for (int j = 0; j < w; j++)
{
cin >> map[j][i];
if (map[j][i] == 1)
{
ex = j;
ey = i;
}
}
}
visited[ex][ey] = 1;
dfs(0, ex, ey, 4);
if (flag == false)
{
cout << "oz pity!" << endl;
}
else
cout << totaltime << endl;
}
system("pause");
return 0;
}