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即可...最后答 ...
随机推荐
- VB调用存储过程 - CreateParameter 方法
这次又转为VB6了....... (┬_┬) ---------------------------------------------------------------------------- ...
- SOA架构介绍和理解
SOA架构介绍和理解 SOA的正确方法论及目标模型,其实SOA在实现架构落地上,需要考虑到对服务的组合,不断的重用现有的服务,让企业应用可以逐步集成,快速实现业务的迭代. 通过SOA架构分层将服务按照 ...
- asp.net连接数据库
Asp.net web连接数据库步骤. 一. 新建一个web工程. 1.文件->添加->新建网站->asp.net web网站Winform窗体. 2.新建好的网站最下面有 ...
- html window.open 使用详解
window.open ('page.html', 'newwindow', 'height=100, width=400, top=0, left=0, toolbar=no, menubar=no ...
- spring mvc 第二天【注解实现springmvc Handler处理ajax简单请求 的配置】
这里使用的是在前台发起ajax请求Handler,后台伪造数据响应给前台, 配置对应ajax请求的Handler信息如下 @Controller public class MyController { ...
- Tcc学习笔记(二) 安装和配置
1.下载和编译 去repo.or.cz/tinycc.git下载最新的snapshot压缩包, 或者用git命令下载: git clone git://repo.or.cz/tinycc.git . ...
- IE6/IE7/IE8兼容H5标签
可以使用html5shiv(html5shiv主要解决HTML5提出的新元素不被IE6-8识别,这些新元素不能作为父节点包裹子元素,并且不能应用CSS样式)来解决 <!--[if lt IE 9 ...
- JS实现动态提示文本框可输入剩余字数(类似发表微博数字提示)
一.实现效果: 为了更直观的体现用户在文本框输入文本时能看到自己输入了多少字,项目中需要通过判断提示文本框剩余可输入字数. html & JS: <div> <textare ...
- Android WIFI 分析(二)
本文介绍Wifi 分析线路二:在Setting中打开WiFi功能.扫描网络以及连接网络的流程. WifiSettings 无线网络设置界面 WifiEnabler 相当于无线网络设置开关 WifiDi ...
- 应用程序缓存--manifest
应用程序缓存(Application Cache)为应用带来三个优势: 离线浏览 - 用户可在应用离线时使用它们 速度 - 已缓存资源加载得更快 减少服务器负载 - 浏览器将只从服务器下载更新过或更改 ...