http://poj.org/problem?id=1979

#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = 21;
bool vis[maxn][maxn];
char maz[maxn][maxn];
int n,m;
const int dx[4] = {1,-1,0,0};
const int dy[4] = {0,0,1,-1};
int ans; bool in(int x,int y)
{
return x >= 0 && x < n && y >= 0 && y < m;
} void dfs(int x,int y)
{
for(int i = 0;i < 4;i++)
{
int tx = x + dx[i],ty = y + dy[i];
if(in(tx,ty) && !vis[tx][ty] && maz[tx][ty] != '#')
{
ans++;
vis[tx][ty] = true;
dfs(tx,ty);
}
}
} int main(){
while(scanf("%d%d",&m,&n) == 2 && (m || n))
{
ans = 0;
memset(vis,0,sizeof vis);
for(int i = 0;i < n; i++)
{
scanf("%s",maz[i]);
}
for(int x = 0;x < n; x++)
{
for(int y = 0;y < m;y++)
{
if(!vis[x][y] && maz[x][y] == '@')
{
ans++;
vis[x][y] = true;
dfs(x,y);
}
}
}
printf("%d\n",ans);
}
return 0;
}

POJ 1979 Red and Black dfs 难度:0的更多相关文章

  1. poj 1979 Red and Black(dfs)

    题目链接:http://poj.org/problem?id=1979 思路分析:使用DFS解决,与迷宫问题相似:迷宫由于搜索方向只往左或右一个方向,往上或下一个方向,不会出现重复搜索: 在该问题中往 ...

  2. POJ 1979 Red and Black (红与黑)

    POJ 1979 Red and Black (红与黑) Time Limit: 1000MS    Memory Limit: 30000K Description 题目描述 There is a ...

  3. POJ 1979 Red and Black (zoj 2165) DFS

    传送门: poj:http://poj.org/problem?id=1979 zoj:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problem ...

  4. POJ 1321 棋盘问题 dfs 难度:0

    http://poj.org/problem?id=1321 注意是在'#'的地方放棋子 矩阵大小不过8*8,即使是8!的时间复杂度也足以承受,可以直接dfs求解 dfs时标注当前点的行和列已被访问, ...

  5. POJ 1979 Red and Black【DFS】

    标准DFS,统计遍历过程中遇到的黑点个数 #include<cstdio> #include<vector> #include<queue> #include< ...

  6. POJ 3009 Curling 2.0 回溯,dfs 难度:0

    http://poj.org/problem?id=3009 如果目前起点紧挨着终点,可以直接向终点滚(终点不算障碍) #include <cstdio> #include <cst ...

  7. poj 1979 Red and Black(dfs水题)

    Description There is a rectangular room, covered with square tiles. Each tile is colored either red ...

  8. POJ 1979 Red and Black (DFS)

    Description There is a rectangular room, covered with square tiles. Each tile is colored either red ...

  9. POJ 1979 Red and Black (简单dfs)

    题目: 简单dfs,没什么好说的 代码: #include <iostream> using namespace std; typedef long long ll; #define IN ...

随机推荐

  1. [css]【转载】CSS样式分离之再分离

    原文链接:http://www.zhangxinxu.com/wordpress/2010/07/css%E6%A0%B7%E5%BC%8F%E5%88%86%E7%A6%BB%E4%B9%8B%E5 ...

  2. Tomcat:配置虚拟目录或主目录

    为了在访问web服务的时候,不需要输入项目名,直接"/"就可以访问的话,需要设置虚拟目录或在ROOT下直接放项目 1.采用虚拟目录的方式 在Host中添加  <Context ...

  3. D3.js 完整的柱形图

    一个完整的柱形图包含三部分:矩形.文字.坐标轴.制作一个实用的柱形图,内容包括:选择集.数据绑定.比例尺.坐标轴等内容. 1. 添加 SVG 画布 //画布大小 var width = 400; va ...

  4. FATAL ha.BootstrapStandby: Unable to fetch namespace information from active NN at ***

    This problem (Unable to fetch namespace information from active NN) occurs, because the active namen ...

  5. Java Performance - 优化和分析Garbage Collection/垃圾收集

    随着硬件的不断提升,Java Heap 越来越大,合理的垃圾收集调优变得愈发重要.下面介绍一些最佳实践: 注意: 下面不涉及 IBM AIX Java. 同时不介绍原理,仅仅是建议以及初始配置/最佳实 ...

  6. [转载]java中try 与catch的使用

    留着以后看 原文地址:与catch的使用">java中try 与catch的使用作者:碌碌如玉 try{ //代码区 }catch(Exception e){ //异常处理 } 代码区 ...

  7. sql中out与output

    --SQLQuery Create By Faywool         create proc Proc_OutPutTest--创建 @numA int,--numA为存储过程的参数 @numB  ...

  8. Appium 切换上下文环境

    Appium 切换上下文环境,代码如下: private void switchToContext(String sContext) { LogManager.getLogger(this.getCl ...

  9. 【bzoj1037】生日聚会

    bzoj1037 题意 \(n\)个男孩,\(m\)个女孩,共\(n+m\)个排成一排. 要求对于任意连续的一段,男孩与女孩的数目之差不超过\(k\). 求排列的方案数. \(1\leq n,m\le ...

  10. python 练习 4

    #!/usr/bin/python # -*- coding: utf-8 -*- from math import sqrt import random def daoxu(n): d=n s=0 ...