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 ...
随机推荐
- vi和vim编辑器
VI vi是一种计算机文本编辑器,由美国计算机科学家比尔·乔伊(Bill Joy)完成编写,并于1976年以BSD协议授权发布. VIM Vim是从vi发展出来的一个文本编辑器.其代码补完.编译及错误 ...
- 并发容器和框架之ConcurrentHashMap
了解HashMap的人都知道HashMap是线程不安全的(多线程下的put方法达到一定大小,引发rehash,导致闭链,最终占满CPU),同时线程安全的HashTable效率又令人望而却步(每个方法都 ...
- POJ-2993 Emag eht htiw Em Pleh---棋盘模拟
题目链接: https://vjudge.net/problem/POJ-2993 题目大意: 输入和输出和这里相反. 思路: 模拟题,没啥算法,直接模拟,不过为了代码精简,还是花了一点心思的 #in ...
- 使用multiprocessing模块创建多进程
# 使用multiprocessing模块创建多进程 # multiprcessing模块提供了一个Process类来描述一个进程对象. # 创建子进程时,只需要传入一个执行函数和函数的参数,即可完成 ...
- hash详解
首先介绍一下hash? 事实上是一种叫做蛤丝的病毒 hash的做法: 首先设一个进制数base,并设一个模数mod 而哈希其实就是把一个数转化为一个值,这个值是base进制的,储存在哈希表中,注意一下 ...
- 如何让服务端同时支持WebSocket和SSL加密的WebSocket(即同时支持ws和wss)?
自从HTML5出来以后,使用WebSocket通信就变得火热起来,基于WebSocket开发的手机APP和手机游戏也越来越多.我的一些开发APP的朋友,开始使用WebSocket通信,后来觉得通信不够 ...
- [LeetCode] Max Stack 最大栈
Design a max stack that supports push, pop, top, peekMax and popMax. push(x) -- Push element x onto ...
- Java IO(一)
在Java中,所有的io类都放在java.io包中. 在IO操作中,我们总是会从一个源数据读取到一个目标数据.那么这个源数据和目标数据可以是文件,流等等.那最常见的就是文件,就像我们在本地电脑上写入东 ...
- Java操作属性文件与国际化
在前面讲到的java类集中的Hashtable中,有一个子类Properties,此类定义如下: public class Properties extends Hashtable<Object ...
- TensorFlow学习笔记(MNIST报错修正 适用Tensorflow1.3)
在Tensorflow实战Google框架下的深度学习这本书的MNIST的图像识别例子中,每次都要报错 错误如下: Only call `sparse_softmax_cross_entropy_ ...