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 ...
随机推荐
- python Django注册页面显示头像
python Django注册页面显示头像(views) def register(request): ''' 注册 :param request: :return: ''' if request.m ...
- JavaScript 对图像进行(追加,插入,替换,删除)
JavaScript 对图像进行(追加,插入,替换,删除) 本次所学内容: document.querySelector('.container') 这个是可以查找单个[id标签和class标签] d ...
- type="file"实现兼容IE8本地选择图片预览
一.HTML代码 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Uploa ...
- 存储过程学习笔记(SQL数据库
一. 存储过程简介 Sql Server的存储过程是一个被命名的存储在服务器上的Transacation-Sql语句集合,是封装重复性工作的一种方法,它支持用户声明的变量.条件执行和其他强大的编程 ...
- linux下使用crontab定时执行脚本
使用crontab定时执行脚本 cron服务是一个定时执行的服务,可以通过crontab 命令添加或者编辑需要定时执行的任务: crontab –e : 修改 crontab 文件,如果文件不存在会自 ...
- [LeetCode] K Inverse Pairs Array K个翻转对数组
Given two integers n and k, find how many different arrays consist of numbers from 1 to n such that ...
- 【Swift】swift定义全局变量
swift定义全局变量非常简单哈,只要在类class上面直接定义,就是全局变量了 当需要在类里面定义一个类函数访问的变量的时候,直接在var或者let 前面加一个 static
- [Codeforces 100633J]Ceizenpok’s formula
Description 题库链接 求 \[C_n^m \mod p\] \(1\leq m\leq n\leq 10^{18},2\leq p\leq 1000000\) Solution 一般的 \ ...
- [IOI 2011]ricehub
Description 乡间有一条笔直而长的路称为“米道”.沿着这条米道上 R 块稻田,每块稻田的坐标均为一个 1 到 L 之间(含 1 和 L)的整数.这些稻田按照坐标以不减的顺序给出,即对于 0 ...
- 计蒜客模拟赛5 D2T2 蚂蚁搬家
很久很久以前,有很多蚂蚁部落共同生活在一片祥和的村庄里.但在某一天,村庄里突然出现了一只食蚁兽,蚂蚁们为了保全性命而决定搬家. 然而这个村庄四面环山,想要离开这个村庄必须要从地洞里离开,村子里一共有 ...