题目链接

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. 八、Kafka总结

    一 Kafka概述 1.1 Kafka是什么 在流式计算中,Kafka一般用来缓存数据,Storm通过消费Kafka的数据进行计算. 1)Apache Kafka是一个开源消息系统,由Scala写成. ...

  2. Java并发编程原理与实战十一:锁重入&自旋锁&死锁

    一.锁重入 package com.roocon.thread.t6; public class Demo { /* 当第一个线程A拿到当前实例锁后,进入a方法,那么,线程A还能拿到被当前实例所加锁的 ...

  3. soj1036. Crypto Columns

    1036. Crypto Columns Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description The columnar en ...

  4. host映射方法

    host映射: host是根据TCP/IP for Windows 的标准来工作的,它的作用是包含IP地址和Host name(主机名)的映射关系,是一个映射IP地址和Host name(主机名)的规 ...

  5. spfa+差分约束系统(D - POJ - 1201 && E - POJ - 1364&&G - POJ - 1)+建边的注意事项+超级源点的建立

    题目链接:https://cn.vjudge.net/contest/276233#problem/D 具体大意: 给出n个闭合的整数区间[ai,bi]和n个整数c1,-,cn. 编写一个程序: 从标 ...

  6. vuejs心法和技法

    原文地址:http://www.cnblogs.com/kidsitcn/p/5409994.html 所有的vuejs组件都是被扩展的vue实例: var MyComponent = Vue.ext ...

  7. CSS line-height应用

    一.固定高度的容器,单行文本垂直居中 代码如下: <!DOCTYPE html> <html> <head> <meta charset="utf- ...

  8. 查看oracle数据库日志存放位置

    1,默认情况下,oracle的日志文件记录在$ORACLE/rdbms/log目录下 [oracle@oracle log]$ pwd /home/oracle/oracle/product/11.2 ...

  9. 从零单排Hadoop——1.搭建Hadoop开发环境

    Hadoop环境准备:ubuntu 12.05.Hadoop 2.4 一.安装ssh 由于hadoop可以配置为集群运行,因此系统需要安装ssh工具保证集群中各节点可以互相访问. 获取ssh软件: s ...

  10. Print Numbers by Recursion

    Print numbers from 1 to the largest number with N digits by recursion. Notice It's pretty easy to do ...