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. CentOS6.4_x86_开关机查看

    1. 有时候,开机进系统的时候,就会卡在哪里,进不去系统.查看开机时 是哪个东西卡在哪里: 开机,显示进度条的时候,按 F4,就可以看到加载的具体情况了. 2. 关机的时候,有时候关闭了机: 用 CT ...

  2. 10 database tables

    本章提要-----------------------------------各种数据库表的讨论, 并介绍什么情况使用哪种表情调表的物理存储特征---------------------------- ...

  3. MonkeyRunner学习(2)常用命令

    目录: 1.截图 2.暂停 (时延秒) 3.屏幕操作 4.打印 5.字符串发送到键盘输入(登录输入) 6.唤醒设备屏幕 7.重起手机 8.按键(系统键) 9.回车键 10.for 循环 11.循环截图 ...

  4. CentOS7静默安装oracle11g

    操作系统: [root@docker ~]# uname -m x86_64 [root@docker ~]# cat /etc/redhat-release CentOS Linux release ...

  5. ssh免密码登陆及其原理

    ssh 无密码登录要使用公钥与私钥.linux下可以用用ssh-keygen生成公钥/私钥对,下面我以CentOS为例. 有机器A(192.168.1.155),B(192.168.1.181).现想 ...

  6. 定义函数def

  7. POJ 2385 Apple Catching

    比起之前一直在刷的背包题,这道题可以算是最纯粹的dp了,写下简单题解. 题意是说cows在1树和2树下来回移动取苹果,有移动次数限制,问最后能拿到的最多苹果数,含有最优子结构性质,大致的状态转移也不难 ...

  8. [C:\Users\Administrator\.IntelliJIdea2016.1\system\tomcat\Unnamed_demo_2\work\Catalina\localhost\demo\org\apache\jsp\index_jsp.java]

    http://www.oschina.net/question/1444338_2146454?sort=time

  9. ASP.NET Core文档中Work with Data章节的翻译目录

    作为初学者看了相关的教程,遇到的问题有: 1. 教程不是针对初学者,往往在某一方面教的较深,但并不系统,不适合初学者: 2. 虽然翻译的很顺畅,但是谈了自己较多的开发体会,初学者看着困难,尤其是TOM ...

  10. js将long日期格式转换为标准日期格式

    <script language="javascript"> //扩展Date的format方法 Date.prototype.format = function (f ...