poj 1164 深度优先搜索模板题

#include<iostream>                  //用栈进行的解决;
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<stack>
using namespace std; const int maxn = ; int room_map[maxn][maxn];
bool color[maxn][maxn]; int RoomArea;
int maxRoomArea;
int RoomNum; struct room
{
int r, c;
room(int rr, int cc):r(rr),c(cc) {};
}; void bfs(int r,int c)
{
stack<room> stk;
stk.push(room(r, c));
while (!stk.empty())
{
room rm = stk.top();
int x = rm.r;
int y = rm.c;
if (color[x][y])
stk.pop();
else
{
RoomArea++;
color[x][y] = true;
if ((room_map[x][y] & ) == ) stk.push(room(x, y - )); //向西;
if ((room_map[x][y] & ) == ) stk.push(room(x - , y)); // 向北;
if ((room_map[x][y] & ) == ) stk.push(room(x, y + )); //向东;
if ((room_map[x][y] & ) == ) stk.push(room(x + , y)); //向南;
}
}
}
int main()
{
int r, c;
while (~scanf("%d%d", &r, &c))
{
for (int i = ; i <= r; i++)
for (int j = ; j <= c; j++)
scanf("%d", &room_map[i][j]);
memset(color, false, sizeof(color));
maxRoomArea = ;
RoomNum = ;
for (int i = ; i <= r; i++)
for (int j = ; j <= c; j++)
{
if (!color[i][j])
{
RoomNum++;
RoomArea = ;
bfs(i, j);
maxRoomArea = max(maxRoomArea, RoomArea);
}
}
printf("%d\n", RoomNum);
printf("%d\n", maxRoomArea);
}
return ;
}
#include<algorithm>
#include<cstring>
#include<cstdio> //用栈;
using namespace std; const int maxn = ;
int roommap[maxn][maxn]; //城堡地图;
int color[maxn][maxn]; //房间染色; int room_num, room_area ;
int maxroom_aera; void bfs(int x, int y)
{
if (color[x][y]) return ;
room_area++;
color[x][y] = room_num; //给访问过的房间染色; if ((roommap[x][y] & ) == ) bfs(x, y - ); //向西;
if ((roommap[x][y] & ) == ) bfs(x - , y); //向北;
if ((roommap[x][y] & ) == ) bfs(x, y + ); //向东;
if ((roommap[x][y] & ) == ) bfs(x + , y); //向南;
} int main()
{
void bfs(int x, int y);
int row, cross;
while (~scanf("%d%d", &row,&cross))
{
memset(color, , sizeof(color));
maxroom_aera = ;
room_area = ;
room_num = ;
for (int i = ; i <= row; i++)
{
for (int j = ; j <= cross; j++)
{
scanf("%d", &roommap[i][j]);
}
} for (int i = ; i <= row; i++)
{
for (int j = ; j <= cross; j++)
{
if (!color[i][j])
{
room_num++;
room_area = ;
bfs(i, j);
maxroom_aera = max(maxroom_aera, room_area);
}
}
}
printf("%d\n", room_num);
printf("%d\n", maxroom_aera);
}
return ;
}
上一篇:VMware虚拟系统 bridged、NAT、host-only三种网络连接模式


下一篇:VMWare中三种网络连接模式的区别