嗯...

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1312

一道很经典的dfs,设置上下左右四个方向,读入时记下起点,然后跑dfs即可...最后答案加上起点的1,无需回溯!!

AC代码:

 #include<cstdio>
#include<iostream>
#include<cstring> using namespace std; int dir[][] = {{-, }, {, }, {, -}, {, }}; int n, m, ans;
bool flag[][];
char g[][]; inline void dfs(int x, int y){
for(int i = ; i < ; i++){
int nx = x + dir[i][];
int ny = y + dir[i][];
if(nx <= m && ny <= n && nx > && ny > && !flag[nx][ny] && g[nx][ny] == '.'){
ans++;
flag[nx][ny] = ;
dfs(nx, ny);
}
}
} int main(){
while(~scanf("%d%d", &n, &m) && n != && m != ){
int x, y;
memset(flag, , sizeof(flag));
for(int i = ; i <= m; i++){
for(int j = ; j <= n; j++){
cin >> g[i][j];
if(g[i][j] == '@'){ x = i; y = j;}
}
}
ans = ;
flag[x][y] = ;
dfs(x, y);
printf("%d\n", ans + );
}
return ;
}

AC代码

HDU 1312 Red and Black(经典DFS)的更多相关文章

  1. HDU 1312:Red and Black(DFS搜索)

      HDU 1312:Red and Black Time Limit:1000MS     Memory Limit:30000KB     64bit IO Format:%I64d & ...

  2. 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 ...

  3. hdu 1312:Red and Black(DFS搜索,入门题)

    Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  4. HDU 1312 Red and Black【DFS】

    搜索虐我千万遍@_@-----一道搜索的水题,WA了好多好多次@_@发现是n,m搞反了-_- 题意-- 给出m行 n列的矩形,其中从@出发,不能跳到#,只能跳到'.'问最多能够跳到多少块'.' 直接搜 ...

  5. HDU 1312 Red and Black --- 入门搜索 DFS解法

    HDU 1312 题目大意: 一个地图里面有三种元素,分别为"@",".","#",其中@为人的起始位置,"#"可以想象 ...

  6. HDU 1312 Red and Black --- 入门搜索 BFS解法

    HDU 1312 题目大意: 一个地图里面有三种元素,分别为"@",".","#",其中@为人的起始位置,"#"可以想象 ...

  7. HDU 1312 Red and Black(最简单也是最经典的搜索)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1312 Red and Black Time Limit: 2000/1000 MS (Java/Oth ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. codis安装以及启动

    https://blog.csdn.net/ygd266/article/details/78469654

  2. dp--B - Hard problem

    B - Hard problem Vasiliy is fond of solving different tasks. Today he found one he wasn't able to so ...

  3. mysql查询速度慢的分析和解决

    一.定位执行慢的sql,如2秒内没执行完的抽取出来 show engines;查看慢查询时间show variables like 'slow%';查看设置多久是慢查询show variables l ...

  4. python3爬取电影数据

    爬取电影票房数据,用于统计建模分析.目标网站为电影票房数据库(http://58921.com/alltime). 基本的爬取静态网站的技术,模拟登陆使用的是最简单的cookies.(这种模拟登陆的方 ...

  5. sql server和eclipse连接问题

    最近学习java,需要用的数据库sql sever,这就有一个连接问题需要设置 首先需要下载sql server,可查看我的博客sql sever下载教程: 连接教程:eclipse连接sql ser ...

  6. ASP.NET + MVC5 入门完整教程三 (上) ---第一个MVC项目

    https://blog.csdn.net/qq_21419015/article/details/80420815 第一个MVC应用程序 1创建MVC项目 打开VS ,File--新建--项目,选择 ...

  7. Educational Codeforces Round 76 (Rated for Div. 2) C. Dominated Subarray

    Let's call an array tt dominated by value vv in the next situation. At first, array tt should have a ...

  8. ubuntu16.04spyder闪退

    解决办法我试了好用 sudo pip install --upgrade html5lib==.0b8 完事.

  9. Qt那点事儿(一)

    原文http://www.cnblogs.com/andreitang/archive/2011/08/03/2125815.html 第一回 Signal和Slot是同步的还是异步的? 我们知道Qt ...

  10. 将html代码部署到阿里云服务器,并进行域名解析,以及在部署过程中遇到的问题和解决方法

    本博客主要是说一下,,如何将html代码部署到阿里云服务器,并进行域名解析,以及在部署过程中遇到的问题和解决方法. 1.先在阿里云上购买一台阿里云服务器(ECS云服务器): 2.远程连接上该服务器,在 ...