您的位置:首页 > 其它

ZOJ 2412 dfs

2016-05-12 22:31 274 查看
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int dir[4][2] = {{0, 1}, {1, 0}, {0, -1}, { -1, 0}};
const int wat[][4] = {0, 0, 1, 1, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1};
const int maxn = 50 + 10;
char mp[maxn][maxn];
int n, m;
bool vis[maxn][maxn];
void dfs(int x, int y, int id)
{
vis[x][y] = 1;
for (int i = 0; i < 4; i++)
if (wat[id][i])
{
int tx = x + dir[i][0];
int ty = y + dir[i][1];
int tk = mp[tx][ty] - 'A';
if (tx >= 0 && tx < n && ty >= 0 && ty < m && !vis[tx][ty] && wat[tk][(i + 2) % 4])
dfs(tx, ty, tk);
}
}
int main(int argc, char const *argv[])
{
while (scanf("%d%d", &n, &m) == 2 && n != -1)
{
memset(vis, 0, sizeof(vis));
for (int i = 0; i < n; i++)
scanf("%s", mp[i]);
int cnt = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
if (!vis[i][j])
dfs(i, j, mp[i][j] - 'A'), cnt++;
printf("%d\n", cnt);
}
return 0;
}


简单题。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: