POJ 1979 Red and Black 四方向棋盘搜索
Red and Black
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 50913 | Accepted: 27001 |
Description
Write a program to count the number of black tiles which he can reach by repeating the moves described above.
Input
input consists of multiple data sets. A data set starts with a line
containing two positive integers W and H; W and H are the numbers of
tiles in the x- and y- directions, respectively. W and H are not more
than 20.
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
each data set, your program should output a line which contains the
number of tiles he can reach from the initial tile (including itself).
Sample Input
6 9
....#.
.....#
......
......
......
......
......
#@...#
.#..#.
11 9
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#..@#.#.
.#.#####.#.
.#.......#.
.#########.
...........
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7 7
..#.#..
..#.#..
###.###
...@...
###.###
..#.#..
..#.#..
0 0
Sample Output
45
59
6
13 题意:@是搜索起点,#不能走,.可以走,.走过一次后会变为#,问从@开始在棋盘上一共可以走几步(@起点算一步)
#include<iostream>
#include<string.h>
#include<string>
#include<algorithm>
using namespace std;
int dir[][]={{,-},{,},{,},{-,}};
string a[];
int n,m,cnt;
int check(int x,int y)
{
if(x>=&&x<n&&y>=&&y<m&&a[x][y]!='#')
return ;
else
return ;
}
void dfs(int x,int y)
{
if(check(x,y)==)
return ;
else
{
a[x][y]='#';
cnt++;
for(int i=;i<;i++)
{
int dx,dy;
dx=x+dir[i][];
dy=y+dir[i][];
dfs(dx,dy);
}
}
}
int main()
{
while(cin>>m>>n&&n&&m)
{
for(int i=;i<n;i++)
cin>>a[i];
cnt=;
for(int i=;i<n;i++)
{
for(int j=;j<m;j++)
{
if(a[i][j]=='@')
{
dfs(i,j);
break;
}
}
}
cout<<cnt<<endl;
}
return ;
}
POJ 1979 Red and Black 四方向棋盘搜索的更多相关文章
- POJ 1979 Red and Black (红与黑)
POJ 1979 Red and Black (红与黑) Time Limit: 1000MS Memory Limit: 30000K Description 题目描述 There is a ...
- OpenJudge/Poj 1979 Red and Black / OpenJudge 2816 红与黑
1.链接地址: http://bailian.openjudge.cn/practice/1979 http://poj.org/problem?id=1979 2.题目: 总时间限制: 1000ms ...
- poj 1979 Red and Black 题解《挑战程序设计竞赛》
地址 http://poj.org/problem?id=1979 Description There is a rectangular room, covered with square tiles ...
- POJ 1979 Red and Black dfs 难度:0
http://poj.org/problem?id=1979 #include <cstdio> #include <cstring> using namespace std; ...
- poj 1979 Red and Black(dfs)
题目链接:http://poj.org/problem?id=1979 思路分析:使用DFS解决,与迷宫问题相似:迷宫由于搜索方向只往左或右一个方向,往上或下一个方向,不会出现重复搜索: 在该问题中往 ...
- POJ 1979 Red and Black (zoj 2165) DFS
传送门: poj:http://poj.org/problem?id=1979 zoj:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problem ...
- HDOJ 1312 (POJ 1979) Red and Black
Problem Description There is a rectangular room, covered with square tiles. Each tile is colored eit ...
- poj 1979 Red and Black(dfs水题)
Description There is a rectangular room, covered with square tiles. Each tile is colored either red ...
- POJ 1979 Red and Black (DFS)
Description There is a rectangular room, covered with square tiles. Each tile is colored either red ...
随机推荐
- PHPStorm 使用 Xdebug
一.下载xdebug xdebug官网:https://xdebug.org/download.php 在选择下载哪个版本的xdebug的时候需要注意了,下面有两种方法,让你准确的下载自己环境对应的x ...
- 5G新时代开启,你会选择哪家运营商?
牌照正式发放后,5G新时代正式来临.而5G时代显然开了个好头,B站UP主"老师好我叫何同学"发布的<有多快?5G在日常使用中的真实体验>视频,引爆全网.除了仅在B站就获 ...
- Docker容器的操作命令
Docker容器的操作命令 写该文章时候的初衷是在使用Docker for windows 的时候遇到的一些问题.起初我在运行好docker客户端以后,创建了一个.NET Core应用发布到docke ...
- 三年以上php开发经验常见面试题
01 一般有三年以上php开发经验去百度.腾讯面试,常会接触的面试题小总结一下: 02 0.简单做一下自我介绍,? 然后谈一下近三年来你的得意之作? 03 1.面试官看过你的简历,会问一些你做的项目 ...
- python学习 —— post请求方法的应用
声明:本篇仅基于兴趣以及技术研究而对B站曾经发生过的抢楼事件背后相关技术原理进行研究而写.请不要将其作为私利而对B站以及B站用户体验造成影响!谢谢合作!若本文对B站及其用户带来困扰,请联系本人删除本文 ...
- netty集成springboot
一 前言 springboot 如何集成netty实现mapper调用不为null的问题让好多读者都头疼过,知识追寻者发了一点时间做了个基本入门集成应用给读者们指明条正确的集成方式,我相信,只要你有n ...
- Java基础知识笔记第三章:运算符表达式语句
算术运算符与表达式 操作符 描述 例子 + 加法 - 相加运算符两侧的值 A + B 等于 30 - 减法 - 左操作数减去右操作数 A – B 等于 -10 * 乘法 - 相乘操作符两侧的值 A * ...
- Python数据类型-8 集合set
集合set set集合是一个无序不重复元素的集,基本功能包括关系测试和消除重复元素.集合使用大括号({})框定元素,并以逗号进行分隔.但是注意:如果要创建一个空集合,必须用 set() 而不是 {} ...
- Ubuntu12.04LTS中安装和使用Spin
https://blog.csdn.net/jackandsnow/article/details/94434481 把第三步 安装tk(在wish中):apt-get install wish 改为 ...
- redis小功能大用处-bitmaps