ACM Red and Black
编写一个程序来计算他可以通过重复上述移动来达到的黑色瓦片的数量。
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
#include<bits/stdc++.h> //c++库
using namespace std;
const char visited = '!'; //0x21
const char red = '#'; //0X23
const char black = '.'; //0X2E
const char init = '@'; //0X40
int w,h,bx,by,sum = ;
char tiles[][];
void find() /*找起始位置*/
{
bool geted = false;
for(int i = ; i < h; i++)
{
if(geted)
break;
for(int j =; j < w; j++)
{
if(tiles[i][j]=='@')
{
bx = j; /*记录起始位置的坐标*/
by = i;
geted = true;
}
}
}
}
void dfs(int row,int col) /**/
{
if(tiles[row][col] < black|| row < ||col < ||row >= h||col >= w)
return ;
sum++;
tiles[row][col] = visited;
dfs(row,col-);
dfs(row-,col);
dfs(row,col+);
dfs(row+,col);
} int main()
{
while(cin>>w>>h,w||h)
{
for(int i = ; i < h; i++)
scanf("%s",tiles[i]); //puts(tiles[i]);测试
find();
sum = ;
dfs(by,bx);
cout<<sum<<endl;
} return ;
}
dfs函数中另一种实现方式,改变搜索方式,用dy dx数组先定义好方向。
#include<iostream>
#include<cstdio>
using namespace std;
const char visited = '!'; //0x21
const char red = '#'; //0X23
const char black = '.'; //0X2E
const char init = '@'; //0X40
int w,h,sx,sy,sum = ;
char tiles[][];
int dy[] = {,-,,};
int dx[] = {,,,-};
void dfs(int row,int col)
{
if(tiles[row][col] < black) return;
sum++;
tiles[row][col] = visited;
for(int i = ; i < ; i++)
{
int nr = row + dy[i];
int nc = col + dx[i];
if(nr < ||nr >= h || nc < || nc >= w) continue;
dfs(nr,nc);
}
}
void find()
{
for(int i = ; i < h; i++)
for(int j = ; j < w; j++)
{
if(tiles[i][j]=='@')
{
sx = j;
sy = i;
}
}
}
int main()
{
while(cin>>w>>h,w||h)
{
for(int i = ; i < h; i++)
scanf("%s",tiles[i]);
find();
sum = ;
dfs(sy,sx);
cout<<sum<<endl;
} return ;
}
ACM Red and Black的更多相关文章
- 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 ...
- 转载 ACM训练计划
leetcode代码 利用堆栈:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/http://oj.leetcode. ...
- sdut 2162:The Android University ACM Team Selection Contest(第二届山东省省赛原题,模拟题)
The Android University ACM Team Selection Contest Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里 ...
- HDU 5029 Relief grain(离线+线段树+启发式合并)(2014 ACM/ICPC Asia Regional Guangzhou Online)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5029 Problem Description The soil is cracking up beca ...
- 杭电ACM分类
杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze ...
- 北大ACM题库习题分类与简介(转载)
在百度文库上找到的,不知是哪位大牛整理的,真的很不错! zz题 目分类 Posted by fishhead at 2007-01-13 12:44:58.0 -------------------- ...
- ACM学习
转:ACM大量习题题库 ACM大量习题题库 现在网上有许多题库,大多是可以在线评测,所以叫做Online Judge.除了USACO是为IOI准备外,其余几乎全部是大学的ACM竞赛题库. US ...
- HDOJ 1312题Red and Black
Red and Black Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...
- 一位学长的ACM总结(感触颇深)
发信人: fennec (fennec), 信区: Algorithm 标 题: acm 总结 by fennec 发信站: 吉林大学牡丹园站 (Wed Dec 8 16:27:55 2004) AC ...
随机推荐
- 脱upx壳--初试--单步追踪
脱upx壳--初试--单步追踪 这里的练习题目是reversing.kr 的Easy Crack 我自己用upx加壳工具给它加了个壳,由于原文件逻辑简单,所以用它来练练手 之后用到的工具是IDA和Ol ...
- Python基础-用户验证
一.项目需求 1.根据用户名和密码,验证用户是否可登陆 2.允许一次执行可验证三次 3.当用户名输错三次后,该用户名锁定,永久不可登陆 二.代码如下 #!/usr/bin/env python #-* ...
- Windows10+Docker搭建分布式Redis集群(SSH服务镜像)(二)
前言:上篇文章我们搭建好了Docker,下面我们开始使用Docker创建镜像,Docker命令就不介绍了.这里宿主是Windows10,cmd的管理和后期文件的复制不是很方便,将创建支持SSH的Cen ...
- spark2.1:rdd.combineByKeyWithClassTag的用法示例
测试spark版本: Spark context Web UI available at http://192.168.1.1:32735 Spark context available as 'sc ...
- html的基本结构
html的基本结构 1.<html>内容</html> html文档的文档标记,也称为html开始标记 这对标记分别位于网页的最前端和最后端,表示开始和结束 2.<hea ...
- EFCore CodeFirst 连接MySql
一.工具及环境 Visual Studio 2017 15.4.3 MySql Navicat for MySQL 二.Entity Framwork Core 2.0 MySql Code Firs ...
- Java集合框架之四大接口、常用实现类
Java集合框架 <Java集合框架的四大接口> Collection:存储无序的.不唯一的数据:其下有List和Set两大接口. List:存储有序的.不唯一的数据: Set:存储无序的 ...
- 云如何让App开发更简单?
欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 互联网"下半场",移动App开发对于质量.速度的要求更加苛刻.4月初,腾讯云正式上线移动开发平台MobileLine,借 ...
- [LeetCode] Count Binary Substrings 统计二进制子字符串
Give a string s, count the number of non-empty (contiguous) substrings that have the same number of ...
- ios开发-MapKit(地图框架)使用简介
我们使用app的时候,很多软件都自带了地图功能.我们可以看到自己的位置,看到周围商场等信息.我们也可以导航,划线等. 其实苹果的MapKit使用起来还是很简单的.这里简单的介绍一下. 0.使用前准备 ...