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/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 20820 Accepted Submission(s): 12673
Write a program to count the number of black tiles which he can reach by repeating the moves described above.
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)
....#.
.....#
......
......
......
......
......
#@...#
.#..#.
11 9
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#..@#.#.
.#.#####.#.
.#.......#.
.#########.
...........
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7 7
..#.#..
..#.#..
###.###
...@...
###.###
..#.#..
..#.#..
0 0
59
6
13
题目大意:一个男子站在‘.’上,他不能走到‘#’上问他能走到的‘.’的数量是多少。
解题思路:初始点bfs四个方向都遍历一次即可。
差点被自己气哭,第一次提交的时候忘记初始化数组,因为没有判断边界,直接导致WA。
AC代码:
- # include <stdio.h>
- # include <string.h>
- # include <stdlib.h>
- # include <iostream>
- # include <fstream>
- # include <vector>
- # include <queue>
- # include <stack>
- # include <map>
- # include <math.h>
- # include <algorithm>
- using namespace std;
- # define pi acos(-1.0)
- # define mem(a,b) memset(a,b,sizeof(a))
- # define FOR(i,a,n) for(int i=a; i<=n; ++i)
- # define For(i,n,a) for(int i=n; i>=a; --i)
- # define FO(i,a,n) for(int i=a; i<n; ++i)
- # define Fo(i,n,a) for(int i=n; i>a ;--i)
- typedef long long LL;
- typedef unsigned long long ULL;
- const int MAXM=;
- char a[MAXM][MAXM];
- int n,m,ans;
- int cx[]={-,,,};
- int cy[]={,,-,};
- struct node
- {
- int x,y;
- };
- queue<node>Q;
- void bfs()
- {
- while(!Q.empty())
- {
- int x=Q.front().x;
- int y=Q.front().y;
- Q.pop();
- for(int i=;i<;i++)
- {
- int tx=x+cx[i];
- int ty=y+cy[i];
- if(a[tx][ty]=='.')
- {
- ans++;
- a[tx][ty]='#';
- Q.push(node{tx,ty});
- }
- }
- }
- }
- int main()
- {
- //freopen("in.txt", "r", stdin);
- while(cin>>m>>n,n&&m)
- {
- mem(a,);
- for(int i=;i<=n;i++)
- {
- cin>>a[i]+;
- for(int j=;j<=m;j++)
- {
- if(a[i][j]=='@')
- {
- Q.push(node{i,j});
- a[i][j]='#';
- }
- }
- }
- ans=;
- bfs();
- cout<<ans<<endl;
- }
- return ;
- }
HDU 1312 Red and Black(bfs,dfs均可,个人倾向bfs)的更多相关文章
- 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 (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搜索,入门题)
Red and Black Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- HDU 1312 Red and Black【DFS】
搜索虐我千万遍@_@-----一道搜索的水题,WA了好多好多次@_@发现是n,m搞反了-_- 题意-- 给出m行 n列的矩形,其中从@出发,不能跳到#,只能跳到'.'问最多能够跳到多少块'.' 直接搜 ...
- HDU 1312 Red and Black --- 入门搜索 BFS解法
HDU 1312 题目大意: 一个地图里面有三种元素,分别为"@",".","#",其中@为人的起始位置,"#"可以想象 ...
- HDU 1312 Red and Black --- 入门搜索 DFS解法
HDU 1312 题目大意: 一个地图里面有三种元素,分别为"@",".","#",其中@为人的起始位置,"#"可以想象 ...
- HDU 1312 Red and Black DFS(深度优先搜索) 和 BFS(广度优先搜索)
Red and Black Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...
- HDU 1312 Red and Black(经典DFS)
嗯... 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1312 一道很经典的dfs,设置上下左右四个方向,读入时记下起点,然后跑dfs即可...最后答 ...
- HDU 1312 Red and Black(最简单也是最经典的搜索)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1312 Red and Black Time Limit: 2000/1000 MS (Java/Oth ...
随机推荐
- linux 内核数据结构之 avl树.
转载: http://blog.csdn.net/programmingring/article/details/37969745 https://zh.wikipedia.org/wiki/AVL% ...
- Executor框架(转)
摘要: Executor作为灵活且强大的异步执行框架,其支持多种不同类型的任务执行策略,提供了一种标准的方法将任务的提交过程和执行过程解耦开发,基于生产者-消费者模式,其提交任务的线程相 ...
- MySql 5.7关键字和保留字-附表
现在使用navicat图形界面或者Hibernate做映射生成表的时候,渐渐的会忽视掉关键字这个问题,而后续也会不断的产生错误提示,一遍遍的查询代码无果,甚至开始怀疑人生,但是其实很多情况下只是使用了 ...
- javascript 与node的 event-loop
参考:https://juejin.im/post/5c337ae06fb9a049bc4cd218 https://github.com/forthealllight/blog/issues/5 h ...
- 2019-11-29-C#-很少人知道的科技
title author date CreateTime categories C# 很少人知道的科技 lindexi 2019-11-29 10:12:43 +0800 2018-03-16 08: ...
- 学习-Pytest(五)yield操作
1.fixture的teardown操作并不是独立的函数,用yield关键字呼唤teardown操作 2.scope="module" 1.fixture参数scope=”modu ...
- windows pip使用国内源
在这里我使用清华源来做示范 win+R 打开用户目录%HOMEPATH%,在此目录下创建 pip 文件夹,在 pip 目录下创建 pip.ini 文件, 内容如下, 在pip文件夹里新建的pip.in ...
- Hive的安装搭建(三)
03 Hive的安装搭建 Hive可以从源码中编译安装,也可以直接使用官网下载的安装包,在此处我们选择安装包解压安装的方式. Hive中最最重要的角色就是metastore 因此按照metastore ...
- open函数的打开标志所在文件
/usr/include/x86_64-linux-gnu/bits/fcntl-linux.h
- nginx代理证书使用方法
一.证书购买 一般情况,申请证书时需要添加DNS解析,具体的步骤一般运营商都会给予详细说明.当然,也需要填写证书保护的处理的域名,一般非免费的证书可以支持多个域名处理,免费的一般只能支持一个域名的设置 ...