- 博客(177)
- 收藏
- 关注
转载 46, 47:Permutations 1, 2
46. Permutationsclass Solution {public: void DFS(int k, int& n, int* vis, vector<int>& nums, vector<int>& chs, vector<vector<int>>& result){ ...
2019-03-10 18:58:00
153
转载 39, 40:Combination Sum 1, 2
39. Combination Sum这道题就是简单的回溯,需要注意的是为了避免重复结果以及降低时间复杂度,可以先对cans数组进行排序,然后每次回溯的时候从chs数组的最后一个元素在cans数组中的位置开始进行,这样就保证了解序列是升序,从而避免了重复回溯,因此也减少了时间。class Solution {public: void DFS(int now, int tar...
2019-03-08 10:44:00
162
转载 12325:Zombie's Treasure Chest
Zombie's Treasure Chest#include<bits/stdc++.h>using namespace std;const int maxn = 50;typedef long long ll;ll n, s1, v1, s2, v2;void swap(ll& a, ll& b){ ll t = a; a =...
2019-01-13 23:17:00
161
转载 12113:Overlapping Squares
Overlapping Squares我的思路:可以根据所给图形计算出图形中包含的方块的个数以及它们各自的位置,方块可以根据某一个角点的位置来确定,具体方法见 count() 函数。方块确定之后,图形的各种变化就取决于方块们的不同放置顺序了,枚举所有排列,进行模拟,看看模拟结果里面有没有所给图形即可。思路没什么问题,代码了检查了好多遍,但是一直W...
2018-12-29 15:49:00
125
转载 211:The Domino Effect
The Domino Effect回溯加剪枝。只需DFS右、下两个方向即可,可以一行一行来。弄完一行之后看下该行所有数字是否都配对了,否则剪枝。还是得细心,两个 cnt 认错了找了大半天 bug 。。。#include<bits/stdc++.h>using namespace std;const int n = 7;int c...
2018-12-16 23:42:00
105
转载 225:Golygons
Golygons回溯加剪枝。某个方向可走的条件是此方向上没有障碍物挡道,还有一点容易被忽视的就是不能落到之前到过的点上!!!最重要的剪枝就是每走一步就判断一下剩下的步数是否有可能回到原点,还有就是如果提前到达原点也要剪掉。#include<bits/stdc++.h>using namespace std;const int ma...
2018-12-13 23:34:00
195
转载 208:Firetruck
Firetruck回溯即可,不过要注意先判断是否可以到达着火点,如果到不了那就没必要回溯了,可能给的数据比较坑,到不了的数据比较多,不判断的话会超时。还有就是输出的格式和样例给的竟然不一样,真是醉了。#include<bits/stdc++.h>using namespace std;const int maxn = 25;in...
2018-12-11 17:09:00
142
转载 1601:The Morning after Halloween(经典)
The Morning after Halloween直接BFS会超时,题目中提示过墙壁很多,那么可以将所有的空格提取出来做张图,然后记录每个空格周围的邻居,这样就不用每次都判断能不能走了。优化的话可以使用双向BFS,从每次正向(从开始位置搜索)搜索一次,反向(从目标位置搜索)搜索一次,如果有相同状态则找到最短路径。BFS(800ms):#i...
2018-12-05 22:27:00
103
转载 10603:Fill
Fill#include<bits/stdc++.h>using namespace std;const int maxn = 200 + 5;int T, a, b, c, d;struct node{ int v[3], dist; bool operator < (const node& rhs...
2018-12-03 23:33:00
116
转载 1073D. Berland Fair
1073D. Berland Fair每次先走一圈,计算出可以购买的糖果的总价格 c 和总个数 t,然后 cnt += t*(T/c) T %= c,循环至无糖果可买。时间复杂度计算如下:#include<bits/stdc++.h>using namespace std;const int maxn = 200000 ...
2018-10-31 23:54:00
125
转载 1073C. Vasya and Robot
1073C. Vasya and Robot注意如果 d 和 n 奇偶性不一致则是不可能到达的,因为机器人每移动一步,其坐标之和的奇偶性就会发生变化。#include<bits/stdc++.h>using namespace std;typedef pair<int,int>P;const int maxn =...
2018-10-31 22:40:00
86
转载 1073B. Vasya and Books
1073B. Vasya and Books#include<bits/stdc++.h>using namespace std;const int maxn = 2*100000 + 5;int n,s[maxn],t[maxn],vis[maxn],res[maxn];int main(){ // freopen("...
2018-10-29 22:34:00
100
转载 1073A. Diverse Substring
1073A. Diverse Substring只要不是一串完全相同的字母就是 YES,因为至少有一个长度为2的子串是 diverse 的。傻傻地测试了所有的子串。。 转载于:https://www.cnblogs.com/JingwangLi/p/10202682.html...
2018-10-29 22:31:00
122
转载 1354:Mobile Computing
Mobile Computing枚举二叉树然后计算其宽度即可,每次枚举两个节点构造一个父节点,计算宽度时需要注意的是每棵树的左节点的右边缘可能超过其右子树的左边缘,反之亦然。#include<bits/stdc++.h>using namespace std;const int maxn = 12;int n,s,idx,w[m...
2018-10-26 17:37:00
175
转载 16. 3Sum Closest
16.3Sum Closest和之前3Sum那道题思路一样,刚开始想的是不用每次计算误差,只需要在小于目标和大于目标的转折点出计算两次然后临界的时候计算一次即可,可是实际上和每次都计算误差速度一样的。。。class Solution {public: int threeSumClosest(vector<int>&...
2018-09-24 17:21:00
80
转载 8. String to Integer (atoi)
8.String to Integer (atoi)class Solution {public: int myAtoi(string str) { int Max = (1<<31)-1, Min = -1<<31; int i = 0; int minus = 1...
2018-09-20 21:07:00
92
转载 15. 3Sum
15.3Sumclass Solution {public: vector<vector<int>> threeSum(vector<int>& nums) { sort(nums.begin(),nums.end()); vector<vector<...
2018-09-08 23:57:00
87
转载 11853:Paintball
Paintball这个题可以看作是图上有很多圆形禁区,如果直接考虑是否可以从左边走到右边似乎有些无从下手,其实可以这样考虑,把整个方形区域看作水面,把禁区看作小岛,如果可以从上边沿着这些小岛走到下边的话,就说明方形区域被禁区整个隔断了,那么就不能从左边走到右边,这样一来DFS即可。(思路来自紫书)至于最北的位置,只需要在DFS的时候计算当前禁区与...
2018-08-29 00:32:00
128
转载 11. Container With Most Water
11.Container With Most Water设置两个指针i j ,分别指向首尾两块板,然后向中间移动,那么宽度变小的情况下为了增大面积只有让高度变大,所以每次移动所指板较低的指针,然后计算面积,重复以上过程。int max(int a,int b){ return a > b ? a : b;}int maxAre...
2018-08-27 12:31:00
108
转载 14. Longest Common Prefix
14.Longest Common Prefixchar s[100000];int min(int a,int b){ return a < b ? a : b;}char* longestCommonPrefix(char** strs, int strsSize) { int len = 10000,cnt = 0...
2018-08-26 23:47:00
86
转载 12. Integer to Roman
12.Integer to Romanchar s[100];char* intToRoman(int num) { char rs[] = "MDCLXVI"; int ts[] = { 1000,500,100,50,10,5,1 }; int cnt = 0; while(num){ int ...
2018-08-26 23:33:00
60
转载 13. Roman to Integer
13.Roman to Integerint romanToInt(char* s) { int dic[26]; dic['I'-'A'] = 1; dic['V'-'A'] = 5; dic['X'-'A'] = 10; dic['L'-'A'] = 50; dic['C'-'A'] = 100...
2018-08-26 22:32:00
79
转载 673:Parentheses Balance
Parentheses Balance之前脑子可能坏掉了。。。简单的栈的应用,要注意的一个地方是一定要用 fgets,因为如果是空串的话 scanf会直接读下一行。#include<bits/stdc++.h>using namespace std;const int maxn = 128 + 5;int n;char s...
2018-08-21 16:22:00
104
转载 10410:Tree Reconstruction
Tree Reconstruction#include<bits/stdc++.h>using namespace std;const int maxn = 1000 + 5;int n,x,root;int pos[maxn];vector<int>A[maxn];int main(){ // freop...
2018-08-20 23:13:00
78
转载 140:Bandwidth
Bandwidth注意节点不一定是按字母表顺序从前到后用的,也有可能是A C 却没用B,因为这个TLE了好几次。。#include<bits/stdc++.h>using namespace std;const int maxn = 26;int L,w;char s[100];int seq[maxn],vis[maxn]...
2018-08-18 22:48:00
80
转载 129:Krypton Factor
Krypton Factor注意 && 别写成 & 了。。。#include<bits/stdc++.h>using namespace std;const int maxn = 80 + 5;int n,L,cnt;int A[maxn];int dfs(int cur){ if(cnt++...
2018-08-17 00:39:00
116
转载 524:Prime Ring Problem
Prime Ring Problem#include<bits/stdc++.h>using namespace std;const int maxn = 17;int n;int A[maxn],vis[maxn],p[2*maxn];int isPrime(int n){ for(int i = 2;i <= ...
2018-08-16 00:13:00
71
转载 10976:Fractions Again?!
Fractions Again?!x 要用 long long。#include<bits/stdc++.h>using namespace std;const int maxk = 10000;int k;long long x[2*maxk];int y[2*maxk];int judge(int y){ l...
2018-08-14 12:35:00
76
转载 11059:Maximum Product
Maximum Product#include<bits/stdc++.h>using namespace std;const int maxn = 20;int n;int seq[maxn];int main(){ // freopen("data.in","r",stdin); // freopen("d...
2018-08-14 11:50:00
62
转载 725:Division
Division#include<bits/stdc++.h>using namespace std;const int maxn = 64 + 5;int n,a[30240][6];int num,kase = 0,cnt = 0;int Pow(int a,int b){ int n = 1; while(...
2018-08-14 00:07:00
85
转载 806:Spatial Structures
Spatial Structures#include<bits/stdc++.h>using namespace std;const int maxn = 64 + 5;int n,len;int seq[maxn*maxn];char img[maxn][maxn];struct node{ char c = 0;...
2018-08-11 23:26:00
54
转载 1600:Patrol Robot
Patrol Robot这个题还是DFS,不过每个状态除了记录位置之外,还要记录剩余穿越次数,这样一来就变成了三维DFS。对于值为1的点如果剩余穿越次数大于0且该状态未遍历过的话也可以访问。像这种四个方向的话用常量数组比较方便,没必要搞个二重循环,容易出bug。#include<bits/stdc++.h>using namespa...
2018-08-10 00:57:00
70
转载 439:Knight Moves
Knight MovesBFS即可,字符串数组 size 定义成了 2 导致输入一直错误,应该是无法存入'\0' 引起的,待会儿再深究。#include<bits/stdc++.h>using namespace std;const int maxn = 8;typedef pair<int,int> P;int ...
2018-08-09 23:10:00
74
转载 712:S-Trees
S-Trees#include<bits/stdc++.h>using namespace std;const int maxn = 7;int n,m,t,cnt = 0;char s[2];int a[maxn];char b[maxn];char leaf[int(pow(2,maxn))];char simu()...
2018-08-09 22:11:00
97
转载 536:Tree Recovery
Tree Recovery#include<bits/stdc++.h>using namespace std;const int maxn = 30;struct node{ char c; struct node* l = NULL; struct node* r = NULL;};char p[ma...
2018-08-08 23:53:00
113
转载 12171:Sculpture
Sculpture思路:将三维空间网格化,每个长方体占据的所有单元标记为1。求面积的话,DFS所有的单元,依次检查是上下左右前后六个方向上相邻单元是否为1,若否则是表面,面积加+1。求体积的话,从外面某个单元开始DFS,求出外面值为0的单元的个数,那么总单元个数 - 外部值为0的单元个数 = 雕塑体积。但是由于外部单元个数巨大会导致堆栈溢出,所以需要...
2018-08-07 23:55:00
81
转载 10562:Undraw the Trees
Undraw the Trees这题没啥说的,利用DFS进行先序遍历即可,注意 node 可以不是字母,然后 puts() 默认换行的,太久没用都忘了。。#include<bits/stdc++.h>using namespace std;const int maxn = 200 + 5;int T;char G[maxn][...
2018-08-06 23:37:00
119
转载 10305:Ordering Tasks
Ordering Tasks这题比较简单,就是拓扑排序,而且肯定是有向无环图,直接DFS即可。注意数据读取,只要 n 和 m 有一个不为0即可。。。不考虑是否存在环:#include<bits/stdc++.h>using namespace std;const int maxn = 100 + 10;int m,n,t...
2018-08-05 00:29:00
87
转载 816:Abbott's Revenge
Abbott's Revenge一些细节要特别注意#include<bits/stdc++.h>using namespace std;const int maxn = 10;const char* dirs = "NESW";const char* turns = "FLR";struct Node{ int r,...
2018-07-31 23:39:00
119
转载 1103:Ancient Messages
Ancient Messages数一数就能发现,题目表中的6个符号从左到右依次有1,3,5,4,0,2个洞,各不相同。这样,只需要数一数输入的符号有几个“白洞”,就能准确地知道它是哪个符号了。至于具体实现,我的思路是:先确定一个符号,找到该符号的空洞个数然后将该符号“擦除”,继续确定下一个符号。具体而言,每次找到一个黑点,然后DFS,如果其周围点为黑点则...
2018-06-02 23:53:00
98
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人