hdu 1312(DFS)
Red and Black
Tme Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 18126 Accepted Submission(s): 11045
is a rectangular room, covered with square tiles. Each tile is colored
either red or black. A man is standing on a black tile. From a tile, he
can move to one of four adjacent tiles. But he can't move on red tiles,
he can move only on black tiles.
Write a program to count the number of black tiles which he can reach by repeating the moves described above.
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)
each data set, your program should output a line which contains the
number of tiles he can reach from the initial tile (including itself).
#include<iostream>
#include<cstdio>
#include<cmath>
#include<map>
#include<cstdlib>
#include<vector>
#include<set>
#include<queue>
#include<cstring>
#include<string.h>
#include<algorithm>
#define INF 0x3f3f3f3f
typedef long long ll;
typedef unsigned long long LL;
using namespace std;
const int N = 1e6+10;
const ll mod = 1e9+7;
int t=0,flag=0;
char s[26][26];
int visited[26][26];
int m,n;
int _x[4]={0,1,-1,0};
int _y[4]={1,0,0,-1};
int ans;
void DFS(int x,int y){
for(int t=0;t<4;t++){
int i=x+_x[t];
int j=y+_y[t];
if(i>=0&&j>=0&&visited[i][j]==0&&s[i][j]=='.'&&i<=n-1&&j<=m-1)
{
ans++;
visited[i][j]=1;
DFS(i,j);
}
}
}
int main(){
while(scanf("%d%d",&m,&n)!=EOF){
if(m==0&&n==0)break;
memset(visited,0,sizeof(visited));
int ii,jj;
int i,j;
for(ii=0;ii<n;ii++){
for(jj=0;jj<m;jj++){
cin>>s[ii][jj];
if(s[ii][jj]=='@'){i=ii;j=jj;}
}
}
ans=1;
visited[i][j]=1;
DFS(i,j);
cout<<ans<<endl;
}
}
hdu 1312(DFS)的更多相关文章
- HDU 1312 Red and Black --- 入门搜索 DFS解法
HDU 1312 题目大意: 一个地图里面有三种元素,分别为"@",".","#",其中@为人的起始位置,"#"可以想象 ...
- HDU 1312:Red and Black(DFS搜索)
HDU 1312:Red and Black Time Limit:1000MS Memory Limit:30000KB 64bit IO Format:%I64d & ...
- HDU 1312 Red and Black(bfs,dfs均可,个人倾向bfs)
题目代号:HDU 1312 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1312 Red and Black Time Limit: 2000/100 ...
- HDU 1312 Red and Black --- 入门搜索 BFS解法
HDU 1312 题目大意: 一个地图里面有三种元素,分别为"@",".","#",其中@为人的起始位置,"#"可以想象 ...
- HDU 5143 DFS
分别给出1,2,3,4 a, b, c,d个 问能否组成数个长度不小于3的等差数列. 首先数量存在大于3的可以直接拿掉,那么可以先判是否都是0或大于3的 然后直接DFS就行了,但是还是要注意先判合 ...
- Snacks HDU 5692 dfs序列+线段树
Snacks HDU 5692 dfs序列+线段树 题意 百度科技园内有n个零食机,零食机之间通过n−1条路相互连通.每个零食机都有一个值v,表示为小度熊提供零食的价值. 由于零食被频繁的消耗和补充, ...
- 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 ...
- HDU 1312 Red and Black(DFS,板子题,详解,零基础教你代码实现DFS)
Red and Black Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- HDU 1312 Red and Black(经典DFS)
嗯... 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1312 一道很经典的dfs,设置上下左右四个方向,读入时记下起点,然后跑dfs即可...最后答 ...
随机推荐
- windows下运行的linux服务器批量管理工具(带UI界面)
产生背景: 由于做服务器运维方面的工作,需要一人对近千台LINUX服务器进行统一集中的管理,如同时批量对LINUX服务器执行相关的指令.同时批量对LINUX服务器upload程序包.同时批量对LINU ...
- windows server 无人值守安装
使用 answer file:AutoUnattend.xml 来无人值守安装,到了选系统这步 还是要手动选择一个系统,这是为什么?怎么设置可以默认进入GUI的系统进行安装啊?
- redis命令String
$ keys * $ rename oldkey newkey $ renamex oldkey newkey 新key存在抛出异常 $ dbsize $ expire key 时间(秒) $ ttl ...
- Python遍历目录下所有文件的最后一行进行判断若错误及时邮件报警-案例
遍历目录下所有文件的最后一行进行判断若错误及时邮件报警-案例: #-*- encoding: utf-8 -*- __author__ = 'liudong' import linecache,sys ...
- FastJSON 使用
FastJSON是一个Java语言编写的高性能,功能完善,完全支持http://json.org的标准的JSON库.多了不说了,百度一下一大把. 在此,简单的总结一下自己用过,测试过的方法. 如果使用 ...
- html window.open 使用详解
window.open ('page.html', 'newwindow', 'height=100, width=400, top=0, left=0, toolbar=no, menubar=no ...
- bootstrap datetimerange
天用的了bootstrap日期插件感觉搜索的资料不是很多在此写下一些使用的心得: 插件开源地址:daterangepicker日期控件, 插件使用只要按照开源中的文档信息来就好先包括以下引用: < ...
- python学习之路——基础篇(3)模块(续)
re正则表达式.shutil.ConfigParser.xml 一.re 正则元字符和语法: 语法 说明 表达式 完全匹配字符 字符 一般字符 匹配自身 abc abc . 匹配除换行符"\ ...
- hdu 1387(Team Queue) STL
Team Queue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- javascript:history.go()和History.back()的区别
http://www.mikebai.com/Article/2009-11/757.html