Poj1979 Red and Black (DFS)
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 47466 | Accepted: 25523 |
Description
Write a program to count the number of black tiles which he can reach by repeating the moves described above.
Input
There are H more lines in the data set, each of which includes W characters. Each character represents the color of a tile as follows.
'.' - a black tile
'#' - a red tile
'@' - a man on a black tile(appears exactly once in a data set)
The end of the input is indicated by a line consisting of two zeros.
Output
Sample Input
- 6 9
- ....#.
- .....#
- ......
- ......
- ......
- ......
- ......
- #@...#
- .#..#.
- 11 9
- .#.........
- .#.#######.
- .#.#.....#.
- .#.#.###.#.
- .#.#..@#.#.
- .#.#####.#.
- .#.......#.
- .#########.
- ...........
- 11 6
- ..#..#..#..
- ..#..#..#..
- ..#..#..###
- ..#..#..#@.
- ..#..#..#..
- ..#..#..#..
- 7 7
- ..#.#..
- ..#.#..
- ###.###
- ...@...
- ###.###
- ..#.#..
- ..#.#..
- 0 0
Sample Output
- 45
- 59
- 6
- 13
Source
- #include <iostream>
- #include <algorithm>
- #include <cmath>
- using namespace std;
- char a[][];
- int n,m;
- int res=;
- int dx[]={,-,,};
- int dy[]={,,,-};
- void dfs(int x,int y)
- {
- res++;
- a[x][y]='#';
- for(int i=;i<;i++){
- int nx=x+dx[i],ny=y+dy[i];
- if(nx>=&&nx<n&&ny>=&&ny<m&&a[nx][ny]=='.'){
- dfs(nx,ny);
- }
- }
- return ;
- }
- void solve()
- {
- for(int i=;i<n;i++){
- for(int j=;j<m;j++){
- if(a[i][j]=='@'){
- dfs(i,j);
- }
- }
- }
- }
- int main()
- {
- while(cin>>m>>n&&n!=&&m!=){
- res=;
- for(int i=;i<n;i++){
- for(int j=;j<m;j++){
- cin>>a[i][j];
- }
- }
- solve();
- cout<<res<<endl;
- }
- return ;
- }
脱离参考书自己再根据自己的理解过一遍:
- #include <iostream>
- #include <algorithm>
- #include <cmath>
- #include <string>
- #include <cstring>
- using namespace std;
- int n,m;
- char a[][];
- int sx,sy,nx,ny;
- int dx[]={,,,-};
- int dy[]={,-,,};
- int res;
- void dfs(int x,int y)
- {
- res++;
- a[x][y]='#';
- for(int i=;i<;i++){
- nx=x+dx[i],ny=y+dy[i];
- if(nx>=&&nx<m&&ny>=&&ny<n&&a[nx][ny]=='.'){
- dfs(nx,ny);
- }
- }
- }
- int main()
- {
- while(cin>>n>>m&&(n&&m)){
- for(int i=;i<m;i++){
- for(int j=;j<n;j++){
- cin>>a[i][j];
- if(a[i][j]=='@'){
- sx=i,sy=j;
- }
- }
- }
- res=;
- dfs(sx,sy);
- cout<<res<<endl;
- }
- return ;
- }
Poj1979 Red and Black (DFS)的更多相关文章
- POJ-1979 Red and Black(DFS)
题目链接:http://poj.org/problem?id=1979 深度优先搜索非递归写法 #include <cstdio> #include <stack> using ...
- HDU 1312 Red and Black (dfs)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1312 Red and Black Time Limit: 2000/1000 MS (Java/Oth ...
- HDU1312 Red and Black(DFS) 2016-07-24 13:49 64人阅读 评论(0) 收藏
Red and Black Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total ...
- poj-1979 red and black(搜索)
Time limit1000 ms Memory limit30000 kB There is a rectangular room, covered with square tiles. Each ...
- Red and Black---hdu1312(dfs)
2015-04-07http://acm.hdu.edu.cn/showproblem.php?pid=1312 Sample Input 6 9....#......#............... ...
- POJ 1979 Red and Black (DFS)
Description There is a rectangular room, covered with square tiles. Each tile is colored either red ...
- HDU1312 / POJ1979 / ZOJ2165 Red and Black(红与黑) 解题报告
题目链接:pid=1312" target="_blank">HDU1312 / POJ1979 / ZOJ2165 Red and Black(红与黑) Red ...
- POJ 1979 Red and Black (红与黑)
POJ 1979 Red and Black (红与黑) Time Limit: 1000MS Memory Limit: 30000K Description 题目描述 There is a ...
- LeetCode Subsets II (DFS)
题意: 给一个集合,有n个可能相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: 看这个就差不多了.LEETCODE SUBSETS (DFS) class Solution { publ ...
随机推荐
- JS 使用const声明常量的本质(很多人都有误解)
在我们使用const声明常量时,总认为值一旦声明就不可改变,其实是有误解的: 刚在看ES6标准文档时,仔细阅读了const的解析,恍然大悟的感觉,分享给大家. 本质 const实际上保证的,并不是变量 ...
- CSS设置浏览器滚动条样式
/*定义滚动条高宽及背景 高宽分别对应横竖滚动条的尺寸*/ ::-webkit-scrollbar { width: 5px; height: 110px; background-color: #F5 ...
- vs2013在使用ef6时,创建模型向导过程中,四种模型方式缺少2种
下载eftool,并安装 https://download.microsoft.com/download/2/C/F/2CF7AFAB-4068-4DAB-88C6-CEFD770FAECD/EFTo ...
- 守护线程daemon
如下代码: from threading import Thread import time def func1(n): time.sleep(10) print(n) t = Thread(targ ...
- 小程序里let和var以及const区别
在JavaScript中有三种声明变量的方式:var.let.const. var:声明全局变量,换句话理解就是,声明在for循环中的变量,跳出for循环同样可以使用. [JavaScript] 纯文 ...
- js的简单介绍
1.js的介绍 js全称叫javascript,但不是java,他是一门前台语言,而java是后台语言. js的作者是布兰登艾奇. 前台语言:运行在客户端的 后台语言:跟数据库有关的. 2.能干什么? ...
- 2018今日头条杯 E-Jump a Jump
Problem E. Jump A JumpInput file: standard inputOutput file: standard outputTime limit: 1 secondsMemor ...
- H5的缓存 manifest
H5里面的App Cache是由开发Web页面的开发者控制的,而不是由Native去控制的,但是Native里面的WebView也需要我们做一下设置才能支持H5的这个特性. 1.工作原理 写Web页面 ...
- Python字符串拼接的6种方法(转)
add by zhj: 对于多行字符串连接,第6种连接方法很方便,连接时不会添加额外的空格. 原文:http://www.cnblogs.com/bigtreei/p/7892113.html 1. ...
- [特征工程]-------使用sklearn做单机特征工程[转载]
https://www.cnblogs.com/jasonfreak/p/5448385.html 使用sklearn做单机特征工程 目录 1 特征工程是什么?2 数据预处理 2.1 无量纲化 2.1 ...