题目链接

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

思路

floodfill问题,使用dfs解决

代码

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std; const int N = 20;
char maze[N][N];
int visit[N][N];
int dir[4][2] = {{-1, 0}, {0 ,1}, {1, 0}, {0, -1}};
int nums;
int w, h; void dfs(int r, int c){
visit[r][c] = 1;
for(int i=0; i<4; i++){
int nextr = r + dir[i][0];
int nextc = c + dir[i][1]; if(nextr>=0 && nextr<h && nextc>=0 && nextc<w && maze[nextr][nextc]!='#' && !visit[nextr][nextc]){
visit[nextr][nextc] = 1;
nums++;
dfs(nextr, nextc);
}
}
} int main(){
//freopen("poj1979.txt", "r", stdin);
int sr, sc;
while(cin>>w>>h && w && h){
nums = 1;
memset(visit, 0, sizeof(visit));
for(int i=0; i<h; i++){
for(int j=0; j<w; j++){
cin>>maze[i][j];
if(maze[i][j] == '@'){
sr = i;
sc = j;
}
}
}
dfs(sr, sc);
cout << nums <<endl;
}
return 0;
}

poj1979 Red And Black(DFS)的更多相关文章

  1. POJ1979 Red and Black (简单DFS)

    POJ1979 Description There is a rectangular room, covered with square tiles. Each tile is colored eit ...

  2. Poj1979 Red and Black (DFS)

    Red and Black Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 47466   Accepted: 25523 D ...

  3. POJ-1979 Red and Black(DFS)

    题目链接:http://poj.org/problem?id=1979 深度优先搜索非递归写法 #include <cstdio> #include <stack> using ...

  4. POJ1979 Red and Black

    速刷一道DFS Description There is a rectangular room, covered with square tiles. Each tile is colored eit ...

  5. HDU 1312 Red and Black DFS(深度优先搜索) 和 BFS(广度优先搜索)

    Red and Black Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...

  6. HDU1312——Red and Black(DFS)

    Red and Black Problem DescriptionThere is a rectangular room, covered with square tiles. Each tile i ...

  7. 数据结构——HDU1312:Red and Black(DFS)

    题目描述 There is a rectangular room, covered with square tiles. Each tile is colored either red or blac ...

  8. HDU 1312 Red and Black (DFS)

    Problem Description There is a rectangular room, covered with square tiles. Each tile is colored eit ...

  9. HDU 1312 Red and Black(DFS,板子题,详解,零基础教你代码实现DFS)

    Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

随机推荐

  1. Java 多线程实现

    第一种方式 package demo3; public class Threddemo { public static void main(String[] args) { MyThred mt = ...

  2. [整理]SQL Server Reporting Services Charts

    http://msdn.microsoft.com/zh-cn/library/aa964128.aspx#mainSectionhttp://msdn.microsoft.com/en-us/lib ...

  3. 程序员 & 设计师都能用上的 75 份速查手册

    分享75份开发人员和设计师会用到的速查手册,由 vikas 收集整理,包括:jQuery.HTML.HTML5.CSS.CSS3.JavaScript.Photoshop .git.Linux.Jav ...

  4. linux 自定义yum仓库、repo文件 yum命令

    目录 自定义yum仓库:createrepo 自定义repo文件 使用yum命令安装httpd软件包 卸载httpd软件包:yum –y remove 软件名 清除yum缓存:yum clean al ...

  5. JS 数组 foreach 和 map

    本文地址:http://www.cnblogs.com/veinyin/p/8794677.html  foreach 和 map 都是数组的迭代方法,对数组的每一项执行给定函数,不会改变原数组. 两 ...

  6. HDU 1017 A Mathematical Curiosity 数学题

    解题报告:输入两个数,n和m,求两个数a和b满足0<a<b<n,并且(a^2+b^2+m) % (a*b) =0,这样的a和b一共有多少对.注意这里的b<n,并不可以等于n. ...

  7. HDU 1256 画8 模拟题

    解题报告:这题我觉得题目有一个没有交代清楚的地方就是关于横线的字符的宽度的问题,题目并没有说,事实上题目要求的是在保证下面的圈高度不小于上面的圈的高度的情况下,横线的宽度就是等于下面的圈的高度. #i ...

  8. Anaconda+django写出第一个web app(二)

    今天开始建立App中的第一个Model,命名为Tutorial. Model的定义在main文件夹下的models.py中通过类进行,我们希望Tutorial这个model包含三个属性:标题.内容和发 ...

  9. GRUB (简体中文)

    原文链接:https://wiki.archlinux.org/index.php/GRUB_(%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87) 前言 引导程序是计算机启动时 ...

  10. Max Sum Plus Plus (动态规划) HDU1024

    题目来源:http://acm.hdu.edu.cn/showproblem.php?pid=1024 (http://www.fjutacm.com/Problem.jsp?pid=1375) 题意 ...