题目链接

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. WAV MP3 Converter-强大的音频转换软件-特别版

    From:http://www.cnblogs.com/killerlegend/p/3873909.html Author:KillerLegend Date:2014.7.28 WAV MP3 C ...

  2. 接口测试Case之面向页面对象编写及规范

    一.什么是页面对象化 主要提倡的思想是:万物皆对象,即把一个Page看成一个对象,来进行接口自动化Case的编写,不要闲扯,直接讲怎么个操作法呢? 二.有什么优势? 2.1 Case层次清晰,便于管理 ...

  3. jmeter乱码问题

    JMeter 的版本由 2.13 升级到了 3.0 发现之前接口脚本 POST 请求主体中的中文无法正确显示,现象如下图所示:

  4. pywinauto: 导入时遇到 "TypeError: LoadLibrary() argument 1 must be string, not unicode"

    pywinauto: 导入时遇到 "TypeError: LoadLibrary() argument 1 must be string, not unicode" 经查询, 看到 ...

  5. 实现asp.net的文件压缩、解压、下载

    很早前就想做文件的解压.压缩.下载 了,不过一直没时间,现在项目做完了,今天弄了下.不过解压,压缩的方法还是看的网上的,嘻嘻~~不过我把它们综合了一下哦.呵呵~~ 1.先要从网上下载一个icsharp ...

  6. xpack文件打包解包代码库

    Github ###概述 xpack是一个文件资源打包工具及类库,可以对多文件进行打包解包. 其使用文件名的hash作为索引,建立hash索引表以加速文件查找. ###特性 支持hashid自动解冲突 ...

  7. [php]mysqli操作流程

    <?php class SqlTools{ private $con; private $trans; public function __construct($host, $user, $ps ...

  8. 2016-2017-20155329 《Java程序设计》第7周学习总结

    学号 2016-2017-20155329 <Java程序设计>第7周学习总结 教材学习内容总结 时间的度量 格林威治标准时间(GMT时间) 世界时(UT) 国际原子时(TAI) 世界协调 ...

  9. 基本控件文档-UILabel属性

    CHENYILONG Blog 基本控件文档-UILabel属性 Fullscreen UILabel属性技术博客http://www.cnblogs.com/ChenYilong/ 新浪微博http ...

  10. 【leetcode 简单】 第八十题 3的幂

    给定一个整数,写一个函数来判断它是否是 3 的幂次方. 示例 1: 输入: 27 输出: true 示例 2: 输入: 0 输出: false 示例 3: 输入: 9 输出: true 示例 4: 输 ...