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 ...
随机推荐
- oracle11g导出表时会发现少表,空表导不出解决方案
oracle11g导出表时会发现少表,空表导不出解决方案. 一:背景引入 oracle11g用exp命令导出数据库表时,有时会发现只导出了一部分表时而且不会报错,原因是有空表没有进行导出,之前一直 ...
- ABP框架 - 我的第一个Web API
本文示例源代码地址https://github.com/lcyhjx/abp-training 上一篇我们已经对ABP是什么,能做什么.有了一个印象.那么接下来我们将动手使用ABP框架快速开发一个AP ...
- NoClassDefFoundError && ClassNotFoundException
两种错误都是涉及类加载问题,类层次结构如下: NoClassDefFoundError是系统错误,ClassNotFoundException是系统异常,可以捕获. NoClassDefFoundEr ...
- python九九乘法表
j = 1 while j <= 9: i = 1 while i <= j: print("%d*%d=%d\t" % (i, j, i*j), end=" ...
- [LeetCode] Number of Longest Increasing Subsequence 最长递增序列的个数
Given an unsorted array of integers, find the number of longest increasing subsequence. Example 1: I ...
- [LeetCode] Trim a Binary Search Tree 修剪一棵二叉搜索树
Given a binary search tree and the lowest and highest boundaries as L and R, trim the tree so that a ...
- [LeetCode] Add Bold Tag in String 字符串中增添加粗标签
Given a string s and a list of strings dict, you need to add a closed pair of bold tag <b> and ...
- python--元祖和字典
一.1:元祖:tuple 在python中,元祖也是一个list, 它和list的区别是list中的元素可以修改,而元祖中的元素不可以修改. 2:元祖的定义:用小括号() 二.1.字典 字典全程d ...
- [HAOI2008]糖果传递
题目描述 有n个小朋友坐成一圈,每人有ai个糖果.每人只能给左右两人传递糖果.每人每次传递一个糖果代价为1. 输入输出格式 输入格式: 小朋友个数n 下面n行 ai 输出格式: 求使所有人获得均等糖果 ...
- [BZOJ]4755: [Jsoi2016]扭动的回文串
Time Limit: 10 Sec Memory Limit: 512 MB Description JYY有两个长度均为N的字符串A和B. 一个"扭动字符串S(i,j,k)由A中的第i ...