poj-1979 red and black(搜索)
Time limit1000 ms
Memory limit30000 kB
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
- 题意:红色和黑色的地砖,只能走黑色的地砖,问最多可以走几块地砖
题解:dfs
- #include <iostream>
- #include <cstdio>
- #include <cstring>
- #include <cmath>
- #include <algorithm>
- #include <queue>
- #include <map>
- #include <set>
- #include <stack>
- #include <vector>
- #include <list>
- using namespace std;
- #define PI 3.14159265358979323846264338327950
- #define INF 0x3f3f3f3f3f3f3f3f;
- char a[][];
- int vis[][];
- int m,n,st,en,sum;
- void dfs(int x,int y)
- {
- a[x][y]='#';
- sum++;
- if(x->= && a[x-][y]=='.')
- dfs(x-,y);
- if(x+<n && a[x+][y]=='.')
- dfs(x+,y);
- if(y->= && a[x][y-]=='.')
- dfs(x,y-);
- if(y+<m && a[x][y+]=='.')
- dfs(x,y+);
- }
- int main()
- {
- while(scanf("%d %d",&m,&n) && (m||n))
- {
- sum=;
- int i,j;
- memset(vis,,sizeof(vis));
- for( i=;i<n;i++)
- for(j=;j<m;j++)
- {
- cin>>a[i][j];
- if(a[i][j]=='@')
- {
- st=i;
- en=j;
- }
- }
- int x=st,y=en;
- a[x][y]='#';
- if(x->= && a[x-][y]=='.')
- dfs(x-,y);
- if(x+<n && a[x+][y]=='.')
- dfs(x+,y);
- if(y->= && a[x][y-]=='.')
- dfs(x,y-);
- if(y+<m && a[x][y+]=='.')
- dfs(x,y+);
- printf("%d\n",sum);
- }
- }
poj-1979 red and black(搜索)的更多相关文章
- POJ 1979 Red and Black (红与黑)
POJ 1979 Red and Black (红与黑) Time Limit: 1000MS Memory Limit: 30000K Description 题目描述 There is a ...
- POJ 1979 Red and Black 四方向棋盘搜索
Red and Black Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 50913 Accepted: 27001 D ...
- OpenJudge/Poj 1979 Red and Black / OpenJudge 2816 红与黑
1.链接地址: http://bailian.openjudge.cn/practice/1979 http://poj.org/problem?id=1979 2.题目: 总时间限制: 1000ms ...
- poj 1979 Red and Black(dfs)
题目链接:http://poj.org/problem?id=1979 思路分析:使用DFS解决,与迷宫问题相似:迷宫由于搜索方向只往左或右一个方向,往上或下一个方向,不会出现重复搜索: 在该问题中往 ...
- poj 1979 Red and Black 题解《挑战程序设计竞赛》
地址 http://poj.org/problem?id=1979 Description There is a rectangular room, covered with square tiles ...
- POJ 1979 Red and Black dfs 难度:0
http://poj.org/problem?id=1979 #include <cstdio> #include <cstring> using namespace std; ...
- 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 ...
- HDOJ 1312 (POJ 1979) Red and Black
Problem Description There is a rectangular room, covered with square tiles. Each tile is colored eit ...
- poj 1979 Red and Black(dfs水题)
Description There is a rectangular room, covered with square tiles. Each tile is colored either red ...
- POJ 1979 Red and Black (DFS)
Description There is a rectangular room, covered with square tiles. Each tile is colored either red ...
随机推荐
- NET Core中使用Redis和Memcached
.NET Core中使用Redis和Memcached的序列化问题 前言 在使用分布式缓存的时候,都不可避免的要做这样一步操作,将数据序列化后再存储到缓存中去. 序列化这一操作,或许是显式的,或许 ...
- 使用Zeppelin时出现sh interpreter not found错误的解决办法(图文详解)
不多说,直接上干货! 问题详解 http://192.168.80.145:8099/#/notebook/2CSV2VT5S 相关博客是 Zeppelin的入门使用系列之使用Zeppelin运行sh ...
- 树莓派连接启动SSH
树莓派的官方更新消息发布:http://downloads.raspberrypi.org/raspbian/release_notes.txt SSH禁用的启用方法: 2016年11月25日: * ...
- asp.net mvc整合Nhibernate的配置方法
http://blog.csdn.net/xz2001/article/details/8452794 http://www.cnblogs.com/GoodHelper/archive/2011/0 ...
- centos 离线安装 mysql 5.7
1 . 安装新版mysql前,需将系统自带的mariadb-lib卸载. rpm -qa|grep mariadb mariadb-libs--.el7.centos.x86_64 rpm -e -- ...
- 一键部署Moodle开源课程管理系统
产品详情 产品介绍Moodle https://moodle.org/ 是一个开源及自由的电子学习软件平台,亦称为课程管理系统.学习管理系统或虚拟学习环境.Moodle 特色异于其他商业线上教学平台, ...
- 卓越管理的秘密(Behind Closed Doors)
或许提到本书甚至本书的作者Johanna Rothman我们会感到些许陌生,那么提起她的另一本获得素有软件界奥斯卡之称的Jolt生产效率大奖的名著<项目管理修炼之道>,会不会惊讶的发现,原 ...
- Java 集合框架_中
Set接口 特点: [1]Set接口表示一个唯一.无序的容器(和添加顺序无关) Set接口常用实现类有 HashSet [1]HashSet是Set接口的实现类,底层数据结构是哈希表. [2]Hash ...
- Render渲染函数和JSX
1.Render函数:render是用来替换temlate的,需要更灵活的模板的写法的时候,用render. 官网API地址:https://cn.vuejs.org/v2/guide/render- ...
- Maven:项目结构
目录结构图: project |- src |- main //工程源代码目录 |- java //工程java源代 ...