HDU-1312题解(DFS)】的更多相关文章

  HDU 1312:Red and Black Time Limit:1000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u   Description There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A man is standing on a black tile. F…
HDU 1312 题目大意: 一个地图里面有三种元素,分别为"@",".","#",其中@为人的起始位置,"#"可以想象为墙,然后.为可以走的空地,求人可以走的最大点数. 解题思路:从起点开始,从4个方向进行递归遍历(已经访问的点进行标记). /*HDU 1312 ----- Red and Black 入门搜索 */ #include <cstdio> int n, m; //n行m列 int cnt, star…
题目代号: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 Problem Description There i…
HDU 1312 题目大意: 一个地图里面有三种元素,分别为"@",".","#",其中@为人的起始位置,"#"可以想象为墙,然后.为可以走的空地,求人可以走的最大点数. 解题思路:从起点开始,起点的四个方向满足条件的点分别入队(放置重复入队,需只要一入队就标记已访问而不是取出时再进行标记),直至队为空. /*HDU 1312 ----- Red and Black 入门搜索 BFS解法*/ #include <cstd…
HDU.5692 Snacks ( DFS序 线段树维护最大值 ) 题意分析 给出一颗树,节点标号为0-n,每个节点有一定权值,并且规定0号为根节点.有两种操作:操作一为询问,给出一个节点x,求从0号节点开始到x节点,所能经过的路径的权值最大为多少:操作二为修改,给出一个节点x和值val,将x的权值改为val. 可以看出是树上修改问题.考虑的解题方式有DFS序+线段树,树链剖分,CXTree.由于后两种目前还不会,选择用DFS序来解决. 首先对树求DFS序,在求解过程当中,顺便求解树上前缀和(p…
Problem Description There 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 mo…
题目链接: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): 17773    Accepted Submission(s): 10826 Problem Description There is a rectangula…
Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 19731    Accepted Submission(s): 11991 Problem Description There is a rectangular room, covered with square tiles. Each tile is col…
嗯... 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1312 一道很经典的dfs,设置上下左右四个方向,读入时记下起点,然后跑dfs即可...最后答案加上起点的1,无需回溯!! AC代码: #include<cstdio> #include<iostream> #include<cstring> using namespace std; ][] = {{-, }, {, }, {, -}, {, }}; int n,…
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1312 题目大意:有一间矩形房屋,地上铺了红.黑两种颜色的方形瓷砖.你站在其中一块黑色的瓷砖上,只能向相邻的黑色瓷砖移动,计算你总共能够到达多少块黑色的瓷砖. 要求:输入包含多组数据,每组数据的第一行输入的是两个整数 W 和 H, 分别表示 x 方向与 y 方向上的瓷砖数量,并且 W 和 H 都不超过20.在接下来的 H 行中,每行包含 W 个字符,每个字符表示一块瓷砖的颜色,规则如下: ' . '…
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 Problem Description There is a rectangular room, covered with square tiles. Each tile is color…
Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 8435    Accepted Submission(s): 5248 Problem Description There is a rectangular room, covered with square tiles. Each tile is colore…
Red and Black Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6762 Accepted Submission(s): 4284 Problem Description There is a rectangular room, covered with square tiles. Each tile is colored eit…
Problem Description There 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 mo…
搜索虐我千万遍@_@-----一道搜索的水题,WA了好多好多次@_@发现是n,m搞反了-_- 题意-- 给出m行 n列的矩形,其中从@出发,不能跳到#,只能跳到'.'问最多能够跳到多少块'.' 直接搜就好,不用剪枝 #include <stdio.h> #include <string.h> int n,m,cnt; char map[1000][1000]; int dir[4][2]={{1,0},{0,1},{-1,0},{0,-1}}; void dfs(int x,int…
Necklace/center> 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5727 Description SJX has 2*N magic gems. N of them have Yin energy inside while others have Yang energy. SJX wants to make a necklace with these magic gems for his beloved BHB. To avoid…
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1312 题目大意:问迷宫中有多少个点被访问. 解题思路: DFS肯定能水过去的.这里就拍了一下BFS. 然后发现自己BFS访问标记有问题,导致某些点被重复访问了. 赶紧改了一下. #include "cstdio" #include "queue" #include "string" #include "cstring" #inc…
Cannon Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4499 Description In Chinese Chess, there is one kind of powerful chessmen called Cannon. It can move horizontally or vertically along the chess grid. At eac…
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6228 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) Problem DescriptionConsider a un-rooted tree T which is not the biological significance of tree or plant, but a tre…
Friends 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5305 Description There are n people and m pairs of friends. For every pair of friends, they can choose to become online friends (communicating using online applications) or offline friends (most…
传送门: 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): 25397    Accepted Submission(s): 15306 Problem Description There is a rectangula…
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1175 解题思路:从出发点开始DFS.出发点与终点中间只能通过0相连,或者直接相连,判断能否找出这样的路径. #include<cmath> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; #define N 1…
题目链接 Problem Description Zty is a man that always full of enthusiasm. He wants to solve every kind of difficulty ACM problem in the world. And he has a habit that he does not like to solvea problem that is easy than problem he had solved. Now yifenfe…
题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=5547 题目: Sudoku Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 2372    Accepted Submission(s): 804 Problem Description   Yi Sima was one of the be…
题目链接: F - Auxiliary Set HDU - 5927 学习网址:https://blog.csdn.net/yiqzq/article/details/81952369题目大意一棵节点数为n的有根数,根节点为1,一开始所有的点都是重点,接下来有q次询问,每次询问把m个点变为轻点,问你树中还有多少个重点. 重点应该满足的条件为: 1.它本身是重点. 2.它为两个重点的最近公共祖先. 每次询问之后在下次询问前,所有的点都恢复为重点. 具体思路:对于每个点保存他的深度.因为每次输入的数…
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4714 本来想直接求树的直径,再得出答案,后来发现是错的. 思路:任选一个点进行DFS,对于一棵以点u为根节点的子树来说,如果它的分支数大于1,那么我们把这颗子树从整棵树上剪下来(优先减去),同时把这颗子树的分支留下两个,其它多余的也剪掉,然后把剪下来的这些部分连接到根节点那里,从而形成一条直链,总代价就是我们减的次数+把剪下来的部分连接到根节点+把最后的直链连成环.在这里剪的次数=把剪下来的部分连接…
http://acm.hdu.edu.cn/showproblem.php?pid=1983 首先,题目要求出口和入口不能封闭,那么,只要把出口或入口的周围全给封闭了那盗贼肯定无法成功偷盗,出口或入口周围最多 会有四个点,所以初始答案ans=4(没必要计算出出口或入口可以封闭的最小值,就初始为4就好),然后从一到四枚举看有没有 最小的符合条件的值, 所以先用BFS查找出至少偷到一个宝石的最小时间的路径,记录下来,然后枚举封闭路径上的点递归回去,再BFS判断是否能偷盗 成功---DFS中插入BFS…
题目在这里:http://acm.hdu.edu.cn/showproblem.php?pid=1175 大家都很熟悉的连连看,原理基本就是这个,典型的搜索.这里用的是广搜.深搜的在下面 与普通的搜索比不同的是要求转折的线不能转折超过两次,就是在结构体中多开一个step(储存转折的次数)和一个dir(记录此刻的方向) 方向初始为-1,当行走一步后的方向与走之前不同的时候,step就应该加一, 然后还要注意的是为了保证得到的是所有的路线中转折次数最小的,这里的visit数组要用来保存每个点的最小转…
题目链接—— http://acm.hdu.edu.cn/showproblem.php?pid=1241 首先给出一个n*m的字符矩阵,‘*’表示空地,‘@’表示油井.问在这个矩阵中有多少组油井区? 每个点周围的8个点都可以与之相连. 从左上角的点开始向后枚举然后dfs搜索就可以了.记得记忆化. 废话说完,上代码—— #include <cstdio> #include <cmath> #include <cstring> #include <algorithm…
Subtrees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Problem Description There is a complete binary tree with N nodes.The subtree of the node i has Ai nodes.How many distinct numbers are there of Ai?   Input…