
ACM-搜索DFS/BFS
文章平均质量分 78
LarryNLPIR
专注NLP/IR/Machine Learning/Data Mining
展开
-
POJ 1321 棋盘问题类似八皇后 dfs搜索
<br />#include <iostream>using namespace std;char qipan[8][8];bool col[8];int m,n,count;//r为行数,k代表已经填充的棋子数,count代表方案数void dfs(int r,int k){ if (k == m) { count++; return; } if (r > n) { return; } for (int i = 0;i < n;i原创 2010-12-10 15:59:00 · 1705 阅读 · 0 评论 -
LeetCode Restore IP Addresses
Given a string containing only digits, restore it by returning all possible valid IP address combinations.For example:Given "25525511135",return ["255.255.11.135", "255.255.111.35"]. (Order does not m原创 2015-02-23 10:01:11 · 1927 阅读 · 0 评论 -
LeetCode Permutations
Given a collection of numbers, return all possible permutations.For example,[1,2,3] have the following permutations:[1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1].思路分析:这题是Permutations II问题的简原创 2014-11-23 13:01:17 · 2026 阅读 · 0 评论 -
LeetCode Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.Each number in C may only be used once in the combination.N原创 2015-02-15 14:47:23 · 1733 阅读 · 0 评论 -
LeetCode Word Search
Given a 2D board and a word, find if the word exists in the grid.The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically nei原创 2015-02-15 09:15:11 · 1931 阅读 · 0 评论 -
LeetCode Combination Sum
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.The same repeated number may be chosen from C unlimited number of原创 2015-02-15 14:43:33 · 1566 阅读 · 0 评论 -
A*搜索算法的JAVA实现 解棋盘爵士游历问题 BFS
A knight moves on a chessboard two squares up, down, left, or right followed by one square in one of the two directions perpendicular to the first part of the move (i.e., the move is L-shaped). Suppos原创 2015-03-15 11:24:27 · 1911 阅读 · 0 评论 -
LeetCode Minimum Depth of Binary Tree
Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.思路分析:这题和Maximum Depth of Binary Tree类似,但是现原创 2015-03-19 13:25:30 · 1473 阅读 · 0 评论 -
Hackerrank Connected Cell in a Grid
Problem StatementYou are given a matrix with m rows and n columns of cells, each of which contains either 1or 0. Two cells are said to be connected if they are adjacent to each other horizontally, ver原创 2015-03-16 02:59:11 · 3992 阅读 · 0 评论 -
LeetCode Same Tree
Given two binary trees, write a function to check if they are equal or not.Two binary trees are considered equal if they are structurally identical and the nodes have the same value.思路分析:判断两个树是否相同,基本也原创 2015-03-19 04:04:40 · 1592 阅读 · 0 评论 -
LeetCode Maximum Depth of Binary Tree
Given a binary tree, find its maximum depth.The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.思路分析:这题比较简单,关于树的题目通常都可以用递归解决,这题也不例外,递归解法的思原创 2015-03-19 03:50:24 · 1668 阅读 · 0 评论 -
LeetCode Surrounded Regions
Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'.A region is captured by flipping all 'O's into 'X's in that surrounded region.For example,X X X XX O O XX X O XX O X X原创 2015-03-29 12:26:28 · 1292 阅读 · 0 评论 -
LeetCode Clone Graph
Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors.OJ's undirected graph serialization:Nodes are labeled uniquely.We use # as a separator for each node, and原创 2015-03-29 12:36:21 · 1784 阅读 · 0 评论 -
LeetCode Binary Tree Right Side View
Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom.For example:Given the following binary tree, 1原创 2015-05-25 14:37:25 · 2255 阅读 · 0 评论 -
LeetCode Invert Binary Tree
这是最近比较火的一个题目,因为一条推特的转播“Google HR:我们90%的工程师都用你写的软件,但是你竟然不会在白板上面反转一颗二叉树,所以滚吧”,各方看法不一,但看这个题目,的确是一个很简单的题目。考察最基本的递归和树操作。下面给出了递归实现和借助栈的迭代实现(非递归实现)。后一个版本的可扩展性更好,可以处理更大的树。实在是容易题,就是DFS遍历一遍树节点,把每个树节点的左右孩子互换就可以了。或许是那个ios开发牛人不屑于准备就去Google面试,结果被爆,与其说是能力问题,不如说是态度问题。原创 2015-06-15 10:46:20 · 3750 阅读 · 1 评论 -
LeetCode Permutations II
题目 Given a collection of numbers that might contain duplicates, return all possible unique permutations.For example,[1,1,2] have the following unique permutations:[1,1,2], [1,2,1], and [2,1,1].思路分析:Pe原创 2014-11-23 12:56:52 · 2376 阅读 · 0 评论 -
LeetCode Word Break II
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word.Return all such possible sentences.For example, given s = “catsanddo原创 2015-02-07 12:08:42 · 3795 阅读 · 0 评论 -
POJ 3083 Children of the Candy Corn DFS及BFS搜索
<br />///这题不是很理解有待于重做,BFS+模拟,模拟有点麻烦 #include<stdio.h> #include<string.h> #include<queue> using namespace std; queue<int> q; int w,h; char map[45][45]; int dl[4][2]={{0,-1},{-1,0},{0,1},{1,0}}; int vis[45][45]; int dire,k;///指向转载 2010-12-11 21:44:00 · 1670 阅读 · 0 评论 -
POJ 1651 Multiplication Puzzle 动态规划及搜索
这是一道比较简单的DP,通过分析可以设最后拿走的牌为i,则所求的最优解就是i左边和右边子列的最小连乘积再加上x[a]*x[i]*x[b],应为i将原来的序列划分为两个子列,这两个子列符合“最有子结构”和“重叠子问题”的dp特点,他们的最优解互相之间没有影响,只会影响全局问题的最优解,在POJ discuss中的解析比较经典,摘录如下,以后做题可以常常看看discuss,就当学习,但是还是要独立思考为主对于整个牌的序列,最左端和最右端的牌是不能被取走的,除这两张以外的所有牌> ,必然有一张最后取走。取走这原创 2010-12-14 23:35:00 · 2503 阅读 · 0 评论 -
POJ 2488 爵士游行问题--DFS遍历
路线可能有多条,线路要求输出的是按字典序搜索出现的第一个路线;也就是要从(1,1)开始;思路:从(1,1)点开始,直接深搜八个方向,注意方向的优先顺序;直接搜索就可以;编程要细心,如将==写成=导致调试一个多小时,此类错误以后务必杜绝!#include #include #include using namespace std;int map[30][30],c,r,num;//最大的行数、列数和已遍历点数string ans;//教训由于一个==写成=号而调试近一个小时!吸取教训,原创 2010-11-16 21:36:00 · 1829 阅读 · 0 评论 -
POJ 3278 用队列实现BFS搜索
理解算法的好方法可以单步调试查看关键变量的变化过程,如head和next,同时画出搜索树分析#include #include #define SIZE 100005using namespace std;//搜索是一种暴力的穷举//分1 - 1 * 2三个方向广搜queue x;bool vistied[SIZE];//记录点是否搜过int step[SIZE];//记录当前搜索的步数int bfs(int n,int k){ int head,next; x.原创 2010-11-17 23:40:00 · 2009 阅读 · 0 评论 -
POJ 1129 四色问题 暴力搜索
#include #include #include #define N 30#define f(i,a,b) for(i=a;i原创 2010-11-18 19:43:00 · 2075 阅读 · 0 评论 -
DFS深度搜索算法实现深度探究解析-以POJ 1040为例
POJ 1040TransportationTime Limit: 1000MS Memory Limit: 10000KTotal Submissions: 2872 Accepted: 1176DescriptionRuratania is just entering c原创 2011-12-20 02:58:42 · 2628 阅读 · 2 评论 -
POJ 2049 走迷宫选取经过门最少的路线 BFS搜索
很经典的BFS搜索 走迷宫选取经过门最少的路线,这题POJ测试数据设计不全面,changeDir数组赋值错误也可以过。。。主要图的数据结构存储方式和算法实现参考了http://blog.youkuaiyun.com/bobten2008/article/details/5093307(1)首先是建图, 由于输入给的都是线段, 但是我们平常处理这类问题都是转换为网格来做的, 因此需要将线段转换为网格.转原创 2011-12-25 01:51:46 · 4384 阅读 · 1 评论 -
POJ 2411 大矩形用1X2小矩形填充 状态dp DFS
这题和编程之美上面的“地板覆盖”问题有点像,不同的是,编程之美上面只需要判定能否覆盖,这题需要求出总方案数目结题报告转自 http://duanple.blog.163.com/blog/static/709717672008930104124684/题意:给你一个h*w的矩形,用一个1*2的小矩形去填充,问有多少种填充方法,不考虑对称性。 关键点提示: 1.DFS部分转载 2011-12-24 16:57:26 · 4037 阅读 · 0 评论 -
POJ 2499 求二叉树结点到根结点的路径长度 递归 二叉树
这题主要求二叉树结点到根结点的路径长度,基本的思路是 比较a与b,如果a大则当前结点是左孩子,a-b作为父结点的左数,父结点的右数与当前右数相等;如果b大则当前结点为右孩子,同理可以求父结点,直到父结点为(1,1)遍历结束。当用原始的递归算法会超时,需要考虑a=1或b=1的特殊情况,同时利用a与b的倍数关系加快遍历速度Source CodeProblem: 2499原创 2012-02-11 15:31:18 · 3242 阅读 · 0 评论 -
NYU AI作业习题-活动安排问题 BFS+DFS Iterative deepening depth-first search
题目链接 http://cs.nyu.edu/courses/spring12/CSCI-GA.2560-001/prog1.html题目大意:给定n个任务的时间、价值及先后序关系,求一个可行的任务子集,使得时间之和不大于deadline,价值之和不小于targetVaule,且不可出现逆序。算法思路:题目已经给出算法,转化为状态空间搜索问题(tree-structured state原创 2012-02-16 01:28:11 · 4390 阅读 · 0 评论 -
CMPSCI 683 AI 第三讲 启发式搜索-IDA*算法
IDA*算法是A*算法和迭代加深ID算法的结合,先搜录相关资料如下[1] 搜索算法:IDA*算法,这是对IDA*算法的一个解释和学习笔记http://blog.youkuaiyun.com/urecvbnkuhBH_54245df/article/details/5856756[2] A*以及迭代加深的A*算法(IDA*) 两种算法的对比 http://www.cnblogs.com/semo/archive原创 2014-10-29 13:10:33 · 3287 阅读 · 0 评论 -
CMPSCI 683 AI 第三讲 启发式搜索-A*算法
作为AI课的预习笔记,搜索了下启发式搜索 A*算法的一些经典资料,整理到下面,这个算法很有意思,有时间自己实现一下。[1] 一个老外写的A*算法实例解说,写的非常翔实易懂,这是一个中文翻译版 http://blog.youkuaiyun.com/crayondeng/article/details/12342989[2] 这个基于[1]的阅读,添加了自己的理解性的注释 http://www.cnblogs.c转载 2014-10-29 13:11:46 · 2369 阅读 · 1 评论 -
LeetCode Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.For example, given n = 3, a solution set is:"((()))", "(()())", "(())()", "()(())", "()()()"思路分析:原创 2014-11-16 10:28:18 · 2141 阅读 · 0 评论 -
LeetCode Dungeon Game
The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. The dungeon consists of M x N rooms laid out in a 2D grid. Our valiant knight (K) was initially pos原创 2015-01-12 15:52:30 · 3635 阅读 · 0 评论 -
LeetCode Kth Smallest Element in a BST
Given a binary search tree, write a function kthSmallest to find the kth smallest element in it.Note: You may assume k is always valid, 1 ≤ k ≤ BST's total elements.Follow up:What if the BST is modifi原创 2015-07-19 15:11:29 · 3514 阅读 · 0 评论