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 ...
随机推荐
- 前端学习之jquery
前端学习之jquery 1. 什么是jQuery对象? jQuery对象就是通过jQuery包装DOM对象后产生的对象.jQuery对象是jQuery独有的.如果一个对象是jQuery对象,那么它 ...
- hive:默认允许动态分区个数为100,超出抛出异常:
在创建好一个分区表后,执行动态分区插入数据,抛出了错误: Caused by: org.apache.hadoop.hive.ql.metadata.HiveFatalException: [Erro ...
- POJ-1556 The Doors---线段相交+最短路
题目链接: https://vjudge.net/problem/POJ-1556 题目大意: 给一个10*10的正方形房间中间用墙隔开每个墙上有两个门,给出门的两个端点坐标求从左边中点走到右边中点所 ...
- urllib.request.Request
import urllib.request #可以将url先构造成一个Request对象,传进urlopen #Request存在的意义是便于在请求的时候传入一些信息,而urlopen则不 reque ...
- AtomicInteger类的理解及使用
AtomicInteger在多线程并发场景的使用 AtomicInteger提供原子操作来进行Integer的使用,因此十分适合高并发情况下的使用. AtomicInteger位于包package j ...
- 确定稳定的 Spring Cloud 相关环境版本
开发部署 Spring Cloud 微服务框架,需要先确定 Spring Cloud 的相关环境版本,主要包含:Spring Cloud.Spring Cloud Netflix.JDK.JRE.Ja ...
- [SDOI2010]古代猪文
题目背景 “在那山的那边海的那边有一群小肥猪.他们活泼又聪明,他们调皮又灵敏.他们自由自在生活在那绿色的大草坪,他们善良勇敢相互都关心……” ——选自猪王国民歌 很久很久以前,在山的那边海的那边的某片 ...
- [Noi2016]区间
题目描述 在数轴上有 n个闭区间 [l1,r1],[l2,r2],...,[ln,rn].现在要从中选出 m 个区间,使得这 m个区间共同包含至少一个位置.换句话说,就是使得存在一个 x,使得对于每一 ...
- 2015 多校联赛 ——HDU5372(树状数组)
Sample Input 3 0 0 0 3 0 1 5 0 1 0 0 1 1 0 1 0 0 Sample Output Case #1: 0 0 0 Case #2: 0 1 0 2 有0, ...
- HDU2108和HDU2036(叉乘)
hdu2108 判断是否为凸边形 判断连续三点的叉乘 若为凸,内角<180:若为凹,内角>180 所以通过正负来判断 #include <iostream> #include ...