POJ1979 Red and Black【DFS】
程序员文章站
2022-05-21 17:49:59
...
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
#define MAX_M 22
#define MAX_N 22
char m[MAX_M][MAX_N];
int M, N, ans;
//定义四个移动方向
int dx[4] = { 1,-1,0,0};
int dy[4] = { 0,0,1,-1};
void dfs(int x, int y)
{ m[x][y] = '#'; //重置,代表已经访问过!
ans++;
for (int i = 0; i < 4; i++)
{
int nx = x + dx[i];
int ny = y + dy[i];
if (nx >= 0 && nx < M && ny >= 0 && ny < N && m[nx][ny] == '.')
dfs(nx, ny);
}
return;
}
int main()
{
int i, j;
int sx, sy;
while (cin >> N >> M, M || N)
{
ans = 0;
for (i = 0; i < M; i++)
for (j = 0; j < N; j++)
{
cin >> m[i][j];
if (m[i][j] == '@')
{
sx = i;
sy = j;
m[i][j] = '.'; //其实不换也行,反正遍历的时候都重置成其他的了。
}
}
dfs(sx, sy);
cout << ans << endl;
}
system("pause");
}
上一篇: LaTex 插入图片问题
下一篇: poj 1979 dfs
推荐阅读
-
POJ 1979 Red And Black 红与黑DFS深搜 AC代码C++
-
ZOJ4048 Red Black Tree(The 2018 ACM-ICPC Asia Qingdao Regional Contest, Online,二分+LCA)
-
Red and Black HDU 1312
-
Red and Black(DSF)
-
基础数据结构和算法十一:Red-black binary search tree
-
Red and Black——个人c++解
-
Red and Black-----BFS(水题)
-
dfs/bfs--Red and Black
-
POJ1979 Red and Black【DFS】
-
Red and Black