#include<queue>
#include<climits>
#include<fstream>
using namespace std;
ifstream cin( "ni.in" );ofstream cout( "ni.out" );
const int MAP_MAX = 1000 + 10;
struct siteType
{
int x;
int y;
};
int h = 0;
int w = 0;
int ans = INT_MAX;
siteType ni = {0};
siteType start = {0};
siteType f[4] = {0};
int map[MAP_MAX][MAP_MAX] = {0};
queue< siteType > olive;
void init();
void work();
void bfs(siteType & site);
void outit();
int main()
{
init();
work();
outit();
return 0;
}
void init()
{
siteType temp = {0};
f[0].x = 1;
f[1].y = 1;
f[2].x = -1;
f[3].y = -1;
cin >> w >> h;
for (temp.x = 0; temp.x != h; ++temp.x)
for (temp.y = 0; temp.y != w; ++temp.y)
{
cin >> map[temp.x][temp.y];
if (map[temp.x][temp.y] == 2)
start = temp;
if (map[temp.x][temp.y] == 3)
ni = temp;
if (map[temp.x][temp.y] == 4)
olive.push(temp); }
}
void work()
{
while (!olive.empty())
{
siteType site = olive.front();
//这个位置BUG,进入函数bfs出错
bfs(site);
olive.pop();
}
}
void bfs(siteType & site)
{
int step[MAP_MAX][MAP_MAX] = {0};
queue< siteType > team;
team.push(site);
while (!team.empty())
{
siteType now = team.front();
team.pop();
for (int i = 0; i != 4; ++i)
{
siteType temp = now;
cout << endl;
temp.x += f[i].x;
temp.y += f[i].y;
if ((temp.x < 0) || (temp.y < 0) || (temp.x >= h) || (temp.y >= w))
continue;
if (map[temp.x][temp.y] == 1)
continue;
if (step[temp.x][temp.y])
continue;
step[temp.x][temp.y] = step[now.x][now.y] + 1;
team.push(temp);
}
}
if ((step[start.x][start.y] + step[ni.x][ni.y]) < ans)
ans = step[start.x][start.y] + step[ni.x][ni.y];
cout << ans << endl;
}
void outit()
{
cout << ans << endl;
}
/*编辑器是DEV-C++5.4.1,进入bfs函数的位置出错,求大犇帮忙看看原因。。。*/
#include<climits>
#include<fstream>
using namespace std;
ifstream cin( "ni.in" );ofstream cout( "ni.out" );
const int MAP_MAX = 1000 + 10;
struct siteType
{
int x;
int y;
};
int h = 0;
int w = 0;
int ans = INT_MAX;
siteType ni = {0};
siteType start = {0};
siteType f[4] = {0};
int map[MAP_MAX][MAP_MAX] = {0};
queue< siteType > olive;
void init();
void work();
void bfs(siteType & site);
void outit();
int main()
{
init();
work();
outit();
return 0;
}
void init()
{
siteType temp = {0};
f[0].x = 1;
f[1].y = 1;
f[2].x = -1;
f[3].y = -1;
cin >> w >> h;
for (temp.x = 0; temp.x != h; ++temp.x)
for (temp.y = 0; temp.y != w; ++temp.y)
{
cin >> map[temp.x][temp.y];
if (map[temp.x][temp.y] == 2)
start = temp;
if (map[temp.x][temp.y] == 3)
ni = temp;
if (map[temp.x][temp.y] == 4)
olive.push(temp); }
}
void work()
{
while (!olive.empty())
{
siteType site = olive.front();
//这个位置BUG,进入函数bfs出错
bfs(site);
olive.pop();
}
}
void bfs(siteType & site)
{
int step[MAP_MAX][MAP_MAX] = {0};
queue< siteType > team;
team.push(site);
while (!team.empty())
{
siteType now = team.front();
team.pop();
for (int i = 0; i != 4; ++i)
{
siteType temp = now;
cout << endl;
temp.x += f[i].x;
temp.y += f[i].y;
if ((temp.x < 0) || (temp.y < 0) || (temp.x >= h) || (temp.y >= w))
continue;
if (map[temp.x][temp.y] == 1)
continue;
if (step[temp.x][temp.y])
continue;
step[temp.x][temp.y] = step[now.x][now.y] + 1;
team.push(temp);
}
}
if ((step[start.x][start.y] + step[ni.x][ni.y]) < ans)
ans = step[start.x][start.y] + step[ni.x][ni.y];
cout << ans << endl;
}
void outit()
{
cout << ans << endl;
}
/*编辑器是DEV-C++5.4.1,进入bfs函数的位置出错,求大犇帮忙看看原因。。。*/