├── Level2 ├── Excel Column Number.cpp ├── Grid Unique Paths.cpp ├── Kth Row of Pascal's Triangle.cpp ├── Largest Number.cpp └── Rearrange Array.cpp ├── Level3 ├── Allocate Books.cpp ├── Atoi.cpp └── NumRange.cpp ├── Level4 └── NextGreater.cpp ├── Level5 ├── 2 Sum.cpp ├── All Unique Permutations.cpp ├── Anagrams.cpp ├── Generate all Parentheses II.cpp ├── Gray code.cpp ├── Letter Phone.cpp ├── Longest Consecutive Sequence.cpp ├── Subset II.cpp ├── Subset.cpp └── Window String.cpp ├── Level6 ├── Distinct Numbers in Window.cpp ├── Inversions.cpp └── Shortest Unique Prefix.cpp ├── Level7 ├── Assign Mice to Holes.cpp ├── Best Time to Buy and Sell Stocks II.cpp ├── Bulbs.cpp ├── Coins in a Line.cpp ├── Highest Product.cpp ├── Interleaving Strings.cpp ├── Longest Increasing Subsequence.cpp ├── Longest valid Parentheses.cpp ├── Majority Element.cpp ├── Max Rectangle in Binary Matrix.cpp ├── Max Sum Without Adjacent Elements.cpp ├── Min Sum Path in Matrix.cpp ├── Min Sum Path in Triangle.cpp ├── Palindrome Partitioning II.cpp ├── Repeating Sub-Sequence.cpp ├── Seats.cpp ├── Stairs.cpp ├── Unique Binary Search Trees II.cpp ├── Unique Paths in a Grid.cpp ├── Ways to Decode.cpp └── Word Break II.cpp ├── Level8 ├── Black Shapes.cpp ├── Capture Regions on Board.cpp ├── Knight On Chess Board.cpp ├── Word Ladder I.cpp └── Word Search Board.cpp └── README.md /Level2/Excel Column Number.cpp: -------------------------------------------------------------------------------- 1 | int Solution::titleToNumber(string A) { 2 | int r=0; 3 | for(int i=A.size()-1,j=0;i>=0;j++,i--){ 4 | r+=(pow(26,j)*(A[i]-'A'+1)); 5 | } 6 | return r; 7 | } 8 | -------------------------------------------------------------------------------- /Level2/Grid Unique Paths.cpp: -------------------------------------------------------------------------------- 1 | int memo[500][500]; 2 | int dp(int i,int j,int A,int B){ 3 | if(i==A&&j==B)return 1; 4 | if(i>A||j>B)return 0; 5 | if(memo[i][j])return memo[i][j]; 6 | return memo[i][j]=dp(i+1,j,A,B)+dp(i,j+1,A,B); 7 | } 8 | int Solution::uniquePaths(int A, int B) { 9 | memset(memo,0,sizeof(memo)); 10 | A--; 11 | B--; 12 | return dp(0,0,A,B); 13 | } 14 | -------------------------------------------------------------------------------- /Level2/Kth Row of Pascal's Triangle.cpp: -------------------------------------------------------------------------------- 1 | long long m[102][102]; 2 | bool ok=false; 3 | void pascal(){ 4 | if(ok)return; 5 | m[1][1]=1; 6 | m[2][1]=1; 7 | m[2][2]=1; 8 | for(int i=3;i<=100;i++){ 9 | m[i][1]=1; 10 | m[i][i]=1; 11 | for(int j=2;j Solution::getRow(int n) { 18 | pascal(); 19 | n++; 20 | vectorv; 21 | for(int i=1;i<=n;i++){ 22 | v.push_back(m[n][i]); 23 | } 24 | return v; 25 | } 26 | -------------------------------------------------------------------------------- /Level2/Largest Number.cpp: -------------------------------------------------------------------------------- 1 | string wrd(int x){ 2 | string r=""; 3 | while(x>0){ 4 | r+=((x%10)+'0'); 5 | x/=10; 6 | } 7 | reverse(r.begin(),r.end()); 8 | return r; 9 | } 10 | bool cmp(string a,string b){ 11 | return a+b>b+a; 12 | } 13 | string Solution::largestNumber(const vector &v) { 14 | string r[v.size()]; 15 | for(int i=0;i &A) { 2 | int n=A.size(); 3 | for(int i=0;iv,int n,int x){ 2 | int _max_ele = *max_element(v.begin(), v.end()); 3 | if(_max_ele > k) 4 | return false; 5 | int s=0,c=0; 6 | for(int i=0;ik){ 9 | c++; 10 | s=0; 11 | } 12 | else i++; 13 | } 14 | if(c &v, int x) { 18 | int n=v.size(),fin=0,in=0,r=(1<<30); 19 | if(x>n)return -1; 20 | for(int i=0;ipila; 5 | for(int i=0;i=48&&A[i]<=58)pila.push(A[i]); 7 | if(A[i]==' '&&!pila.empty()||A[i]==' '&&ok)break; 8 | if(pila.empty()&&A[i]=='-'){ 9 | uso=-1; 10 | ok=true; 11 | continue; 12 | } 13 | if(pila.empty()&&A[i]=='+'){ 14 | ok=true; 15 | continue; 16 | } 17 | if(!(A[i]>=48&&A[i]<=58)&&A[i]!=' ')break; 18 | } 19 | for(long long i=1;!pila.empty();i*=10){ 20 | r+=(long long)((pila.top()-'0')*(i)); 21 | if(r>=INT_MAX&&uso==1)return INT_MAX; 22 | if(r>=INT_MAX&&uso==-1)return INT_MIN; 23 | pila.pop(); 24 | } 25 | r*=uso; 26 | return r; 27 | } 28 | -------------------------------------------------------------------------------- /Level3/NumRange.cpp: -------------------------------------------------------------------------------- 1 | long long s[900002],a,b,x; 2 | bool check(int z){ 3 | if(z>=a&&z<=b)return true; 4 | return false; 5 | } 6 | int bs(int in,int fin){ 7 | if(in==fin||fin==0)return in; 8 | int mid=(in+fin)/2; 9 | if(s[mid]-x>b)return bs(in,mid); 10 | else return bs(mid+1,fin); 11 | } 12 | int Solution::numRange(vector &A, int B, int C) { 13 | if(A.size()==0)return 0; 14 | long long n=A.size(),r=0; 15 | a=B; 16 | b=C; 17 | s[0]=A[0]; 18 | for(int i=1;ib)continue; 25 | int pos=bs(i,n-1); 26 | while(pos>=0&&s[pos]-x>b)pos--; 27 | if(pos=0&&check(s[pos]-x)){ 29 | r++; 30 | pos--; 31 | } 32 | } 33 | return r; 34 | } 35 | -------------------------------------------------------------------------------- /Level4/NextGreater.cpp: -------------------------------------------------------------------------------- 1 | vector Solution::nextGreater(vector &A) { 2 | stackpila; 3 | pila.push(0); 4 | vectorr; 5 | r.push_back(-1); 6 | for(int i=1;iA[pila.top()]){ 10 | r[pila.top()]=j; 11 | pila.pop(); 12 | } 13 | pila.push(i); 14 | } 15 | return r; 16 | } 17 | -------------------------------------------------------------------------------- /Level5/2 Sum.cpp: -------------------------------------------------------------------------------- 1 | vector Solution::twoSum(const vector &v, int B) { 2 | mapmemo; 3 | vectorr; 4 | int a=0,b=(1<<30); 5 | for(int i=0;i >r; 2 | vectorv; 3 | bool memo[15]; 4 | map,bool>mapita; 5 | void solve(int i,vectoruso){ 6 | if(i==v.size()&&!mapita[uso]){ 7 | mapita[uso]=true; 8 | r.push_back(uso); 9 | return; 10 | } 11 | for(int j=0;j > Solution::permute(vector &A) { 22 | v=A; 23 | r.clear(); 24 | mapita.clear(); 25 | vectoruso; 26 | solve(0,uso); 27 | return r; 28 | } 29 | -------------------------------------------------------------------------------- /Level5/Anagrams.cpp: -------------------------------------------------------------------------------- 1 | vector > Solution::anagrams(const vector &v) { 2 | vector >r; 3 | map >memo; 4 | for(int i=0;i >::iterator it=memo.begin();it!=memo.end();it++){ 10 | r.push_back(it->second); 11 | } 12 | sort(r.begin(),r.end()); 13 | return r; 14 | } 15 | -------------------------------------------------------------------------------- /Level5/Generate all Parentheses II.cpp: -------------------------------------------------------------------------------- 1 | vectorr; 2 | bool check(string v){ 3 | stackpila; 4 | for(int i=0;i Solution::generateParenthesis(int A) { 23 | r.clear(); 24 | if(A==0)return r; 25 | solve(A*2,""); 26 | sort(r.begin(),r.end()); 27 | return r; 28 | } 29 | -------------------------------------------------------------------------------- /Level5/Gray code.cpp: -------------------------------------------------------------------------------- 1 | vector Solution::grayCode(int A) { 2 | vector >v,v2; 3 | vectora,b,r; 4 | a.push_back(0); 5 | b.push_back(1); 6 | v.push_back(a); 7 | v.push_back(b); 8 | for(int i=1;i=0;j--,k++){ 24 | if(v[i][j]==1)s+=(pow(2,k)); 25 | } 26 | r.push_back(s); 27 | } 28 | return r; 29 | } 30 | -------------------------------------------------------------------------------- /Level5/Letter Phone.cpp: -------------------------------------------------------------------------------- 1 | vectorr; 2 | vectorm; 3 | string v; 4 | void ini(){ 5 | string uso="0"; m.push_back(uso);uso="1";m.push_back(uso); 6 | uso="abc";m.push_back(uso);uso="def";m.push_back(uso); 7 | uso="ghi";m.push_back(uso);uso="jkl";m.push_back(uso);uso="mno"; 8 | m.push_back(uso);uso="pqrs";m.push_back(uso);uso="tuv";m.push_back(uso); 9 | uso="wxyz";m.push_back(uso); 10 | } 11 | void solve(int i,string uso){ 12 | if(i==v.size()){ 13 | r.push_back(uso); 14 | return; 15 | } 16 | for(int j=0;j Solution::letterCombinations(string A) { 21 | r.clear(); 22 | m.clear(); 23 | ini(); 24 | v=A; 25 | solve(0,""); 26 | sort(r.begin(),r.end()); 27 | return r; 28 | } 29 | -------------------------------------------------------------------------------- /Level5/Longest Consecutive Sequence.cpp: -------------------------------------------------------------------------------- 1 | int Solution::longestConsecutive(const vector &A) { 2 | int res=1,r=1,a=0; 3 | vectorv=A; 4 | sort(v.begin(),v.end()); 5 | a=v[0]; 6 | for(int i=1;iv; 2 | map,bool>memo; 3 | vector > r; 4 | void solve(int i,vectoruso){ 5 | if(i>=v.size()){ 6 | if(!memo[uso])r.push_back(uso); 7 | memo[uso]=true; 8 | return; 9 | } 10 | uso.push_back(v[i]); 11 | solve(i+1,uso); 12 | uso.pop_back(); 13 | solve(i+1,uso); 14 | } 15 | vector > Solution::subsetsWithDup(vector &A) { 16 | v=A; 17 | memo.clear(); 18 | r.clear(); 19 | sort(v.begin(),v.end()); 20 | vectoruso; 21 | solve(0,uso); 22 | sort(r.begin(),r.end()); 23 | return r; 24 | } 25 | -------------------------------------------------------------------------------- /Level5/Subset.cpp: -------------------------------------------------------------------------------- 1 | vectorv; 2 | vector > r; 3 | void solve(int i,vectoruso){ 4 | if(i>=v.size()){ 5 | r.push_back(uso); 6 | return; 7 | } 8 | uso.push_back(v[i]); 9 | solve(i+1,uso); 10 | uso.pop_back(); 11 | solve(i+1,uso); 12 | } 13 | vector > Solution::subsets(vector &A) { 14 | v=A; 15 | r.clear(); 16 | sort(v.begin(),v.end()); 17 | vectoruso; 18 | solve(0,uso); 19 | sort(r.begin(),r.end()); 20 | return r; 21 | } 22 | -------------------------------------------------------------------------------- /Level5/Window String.cpp: -------------------------------------------------------------------------------- 1 | string Solution::minWindow(string S, string T) { 2 | int in=0,fin=0,c=0,a=0,r=0; 3 | unordered_mapmemo; 4 | for(int i=0;i=0)c++; 14 | if(c==T.size()){ 15 | while(memo.find(S[in])==memo.end()||memo[S[in]]<0){ 16 | if(memo.find(S[in])!=memo.end())memo[S[in]]++; 17 | in++; 18 | } 19 | if(r==0||fin-in+1 Solution::dNums(vector &A, int k) { 2 | int in=0,fin=k-1,r=0; 3 | vectorres; 4 | mapmemo; 5 | for(int i=0;i0){ 13 | r+=bit[i]; 14 | i-=(i&-i); 15 | } 16 | return r; 17 | } 18 | int Solution::countInversions(vector &A) { 19 | memset(bit,0,sizeof(bit)); 20 | ll r=0; 21 | pairv[A.size()+1]; 22 | for(int i=0;i Solution::prefix(vector &A) { 29 | memset(trie,0,sizeof(trie)); 30 | for(int i=0;ires; 34 | for(int i=0;i &A, vector &B) { 2 | sort(A.begin(),A.end()); 3 | sort(B.begin(),B.end()); 4 | int r=0; 5 | for(int i=0;iv; 3 | int dp(int i,int s){ 4 | if(i==v.size())return 0; 5 | if(memo[i][s])return memo[i][s]; 6 | if(s==0){ 7 | return memo[i][s]=max(dp(i+1,0),dp(i+1,1)-v[i]); 8 | } 9 | else return memo[i][s]=max(dp(i+1,0)+v[i],dp(i+1,1)); 10 | } 11 | int Solution::maxProfit(const vector &A) { 12 | if(A.size()==0)return 0; 13 | v=A; 14 | memset(memo,0,sizeof(memo)); 15 | return dp(0,0); 16 | } 17 | -------------------------------------------------------------------------------- /Level7/Bulbs.cpp: -------------------------------------------------------------------------------- 1 | int Solution::bulbs(vector &A) { 2 | int par=0,r=0; 3 | for(int i=0;iv; 3 | int dp(int i,int j,int s){ 4 | if(i>j)return 0; 5 | if(memo[i][j][s])return memo[i][j][s]; 6 | if(!s){ 7 | return memo[i][j][s]=max(v[j]+dp(i,j-1,1),v[i]+dp(i+1,j,1)); 8 | } 9 | else { 10 | return memo[i][j][s]=min(dp(i,j-1,0),dp(i+1,j,0)); 11 | } 12 | } 13 | int Solution::maxcoin(vector &A) { 14 | memset(memo,0,sizeof(memo)); 15 | v=A; 16 | return dp(0,A.size()-1,0); 17 | } 18 | -------------------------------------------------------------------------------- /Level7/Highest Product.cpp: -------------------------------------------------------------------------------- 1 | int Solution::maxp3(vector &A) { 2 | sort(A.begin(),A.end()); 3 | int a=A[0],b=A[1]; 4 | reverse(A.begin(),A.end()); 5 | int r=1,many=0,res=(a*b)*A[0]; 6 | for(int i=0;i<3;i++){ 7 | r*=A[i]; 8 | } 9 | res=max(res,r); 10 | return res; 11 | } 12 | -------------------------------------------------------------------------------- /Level7/Interleaving Strings.cpp: -------------------------------------------------------------------------------- 1 | string a,b,v; 2 | bool dp(int in,int i,int j){ 3 | if(in==v.size())return true; 4 | if(i==a.size()&&j==b.size())return false; 5 | bool uso=false; 6 | if(i &A) { 3 | memset(dp,0,sizeof(dp)); 4 | int n=A.size(); 5 | for(int i=n-1;i>=0;i--){ 6 | dp[i]=1; 7 | for(int j=i+1;jA[i])dp[i]=max(dp[i],dp[j]+1); 9 | } 10 | } 11 | int r=0; 12 | for(int i=0;ipila; 5 | int r=0; 6 | for(int i=0;i &A) { 2 | mapmemo; 3 | for(int i=0;i::iterator it=memo.begin();it!=memo.end();it++){ 8 | if(it->second>(A.size()/2))r=max(it->first,r); 9 | } 10 | return r; 11 | } 12 | -------------------------------------------------------------------------------- /Level7/Max Rectangle in Binary Matrix.cpp: -------------------------------------------------------------------------------- 1 | #define MAX 1002 2 | int bit[MAX][MAX]; 3 | void update(int x,int y,int val){ 4 | int y1; 5 | while(x0){ 17 | int y1=y; 18 | while(y1>0){ 19 | r+=bit[x][y1]; 20 | y1-=(y1&-y1); 21 | } 22 | x-=(x&-x); 23 | } 24 | return r; 25 | } 26 | int Solution::maximalRectangle(vector > &A) { 27 | memset(bit,0,sizeof(bit)); 28 | for(int i=0;i >m; 2 | int memo[2][100002]; 3 | int n; 4 | int dp(int i,int j){ 5 | if(j>=n)return 0; 6 | if(memo[i][j])return memo[i][j]; 7 | if(i==0){ 8 | return memo[i][j]=max(m[i][j]+dp(i,j+2),max(m[i][j]+dp(i+1,j+2),dp(i,j+1))); 9 | } 10 | else { 11 | return memo[i][j]=max(m[i][j]+dp(i,j+2),max(m[i][j]+dp(i-1,j+2),dp(i,j+1))); 12 | } 13 | } 14 | int Solution::adjacent(vector > &A) { 15 | m.clear(); 16 | n=A[0].size(); 17 | memset(memo,0,sizeof(memo)); 18 | m.push_back(A[0]); 19 | m.push_back(A[1]); 20 | return max(dp(0,0),dp(1,0)); 21 | } 22 | 23 | -------------------------------------------------------------------------------- /Level7/Min Sum Path in Matrix.cpp: -------------------------------------------------------------------------------- 1 | vector > m; 2 | int memo[1002][1002]; 3 | int dp(int i,int j){ 4 | if(i==m.size()-1&&j==m[i].size()-1)return m[i][j]; 5 | if(i>=m.size()||i<0||j<0||j>=m[i].size())return (1<<30); 6 | if(memo[i][j])return memo[i][j]; 7 | return memo[i][j]=m[i][j]+min(dp(i+1,j),dp(i,j+1)); 8 | } 9 | int Solution::minPathSum(vector > &A) { 10 | m=A; 11 | memset(memo,0,sizeof(memo)); 12 | return dp(0,0); 13 | } 14 | -------------------------------------------------------------------------------- /Level7/Min Sum Path in Triangle.cpp: -------------------------------------------------------------------------------- 1 | vector >m; 2 | int memo[1002][1002]; 3 | int dp(int i,int j){ 4 | if(i<0||i>=m.size()||j<0||j>=m[i].size())return (1<<30); 5 | if(i==m.size()-1)return m[i][j]; 6 | if(memo[i][j])return memo[i][j]; 7 | return memo[i][j]=m[i][j]+min(dp(i+1,j),dp(i+1,j+1)); 8 | } 9 | int Solution::minimumTotal(vector > &A) { 10 | m=A; 11 | memset(memo,0,sizeof(memo)); 12 | return dp(0,0); 13 | } 14 | -------------------------------------------------------------------------------- /Level7/Palindrome Partitioning II.cpp: -------------------------------------------------------------------------------- 1 | #define MAX 1002 2 | int memo[MAX][MAX]; 3 | string v; 4 | bool ispa(int i,int j){ 5 | while(i<=j){ 6 | if(v[i]!=v[j])return false; 7 | i++; 8 | j--; 9 | } 10 | return true; 11 | } 12 | int dp(int in,int i){ 13 | if(i==v.size()){ 14 | if(!ispa(in,i-1))return (1<<30); 15 | else return 0; 16 | } 17 | if(memo[in][i])return memo[in][i]; 18 | if(ispa(in,i)){ 19 | return memo[in][i]=min(1+dp(i+1,i+1),dp(in,i+1)); 20 | } 21 | else { 22 | return memo[in][i]=dp(in,i+1); 23 | } 24 | } 25 | int Solution::minCut(string A) { 26 | v=A; 27 | int r=(A.size()-1); 28 | memset(memo,0,sizeof(memo)); 29 | return min(r,dp(0,0)); 30 | } 31 | -------------------------------------------------------------------------------- /Level7/Repeating Sub-Sequence.cpp: -------------------------------------------------------------------------------- 1 | string v; 2 | bool ok; 3 | int memo[1002][1002]; 4 | int dp(int i,int j,int s){ 5 | if(ok)return 1; 6 | if(s==2){ 7 | ok=true; 8 | return 1; 9 | } 10 | if(i==v.size()||j==v.size())return -(1<<30); 11 | if(memo[i][j])return memo[i][j]; 12 | for(int k=j;ki)return memo[i][j]=max(dp(i+1,k+1,s+1),dp(i+1,j,s)); 14 | } 15 | return memo[i][j]=dp(i+1,j,s); 16 | } 17 | int Solution::anytwo(string A) { 18 | ok=false; 19 | memset(memo,0,sizeof(memo)); 20 | v=A; 21 | int uso=dp(0,0,0); 22 | return ok; 23 | } 24 | -------------------------------------------------------------------------------- /Level7/Seats.cpp: -------------------------------------------------------------------------------- 1 | #define MOD 10000003 2 | int Solution::seats(string v) { 3 | int n=v.size(); 4 | int mid=0,x=0,pos,pos2,r=0; 5 | for(int i=0;i=0&&x>0;i--){ //De la mid pa abajo 19 | if(v[i]=='x'){ 20 | r+=(pos2-i)-1; 21 | r%=MOD; 22 | pos2--; 23 | x--; 24 | } 25 | } 26 | x=mid-1; 27 | pos2=pos; 28 | for(int i=pos+1;i0;i++){ //de la mid pa arriba 29 | if(v[i]=='x'){ 30 | r+=(i-pos2)-1; 31 | r%=MOD; 32 | pos2++; 33 | x--; 34 | } 35 | } 36 | return r%MOD; 37 | } 38 | -------------------------------------------------------------------------------- /Level7/Stairs.cpp: -------------------------------------------------------------------------------- 1 | int memo[1002]; 2 | int dp(int n){ 3 | if(n==0)return 1; 4 | if(n<0)return 0; 5 | if(memo[n])return memo[n]; 6 | return memo[n]=dp(n-1)+dp(n-2); 7 | } 8 | int Solution::climbStairs(int A) { 9 | memset(memo,0,sizeof(memo)); 10 | return dp(A); 11 | } 12 | -------------------------------------------------------------------------------- /Level7/Unique Binary Search Trees II.cpp: -------------------------------------------------------------------------------- 1 | int memo[1001]; 2 | int dp(int x){ 3 | if(x==1)return 1; 4 | if(memo[x])return memo[x]; 5 | int s=0; 6 | for(int i=1;i > v; 2 | int memo[102][102]; 3 | int n,c; 4 | bool ok; 5 | int dp(int i,int j){ 6 | if(i==0&&j==0&&v[i][j]==1)return 0; 7 | if(i==(n-1)&&j==(c-1)){ 8 | ok=true; 9 | return 1; 10 | } 11 | if(i>=n||j>=c)return 0; 12 | if(memo[i][j])return memo[i][j]; 13 | memo[i][j]=0; 14 | if(i+1 > &A) { 23 | memset(memo,0,sizeof(memo)); 24 | v.clear(); 25 | for(int i=0;iv.size()||v[i]=='0')return 0; 6 | if(memo[i])return memo[i]; 7 | if(v[i]>='1')memo[i]+=dp(i+1); 8 | if(i+1r; 9 | void add(string v){ 10 | int pos=1; 11 | for(int i=0;i Solution::wordBreak(string A, vector &B) { 39 | memset(trie,0,sizeof(trie)); 40 | r.clear(); 41 | m=1; 42 | for(int i=0;iv; 2 | void dfs(int i,int j){ 3 | if(i>=v.size()||i<0||j>=v[i].size()||j<0)return; 4 | if(v[i][j]!='X')return; 5 | v[i][j]='0'; 6 | dfs(i+1,j); 7 | dfs(i,j+1); 8 | dfs(i-1,j); 9 | dfs(i,j-1); 10 | } 11 | int Solution::black(vector &A) { 12 | v=A; 13 | int r=0; 14 | for(int i=0;i >v; 3 | int n,c; 4 | void dfs(int i,int j){ 5 | if(memo[i][j])return; 6 | if(i>=n||i<0||j<0||j>=c)return; 7 | if(v[i][j]=='X')return; 8 | memo[i][j]=true; 9 | dfs(i+1,j); 10 | dfs(i,j+1); 11 | dfs(i-1,j); 12 | dfs(i,j-1); 13 | } 14 | void Solution::solve(vector > &A) { 15 | memset(memo,0,sizeof(memo)); 16 | n=A.size(); 17 | c=A[0].size(); 18 | v=A; 19 | for(int i=0;icola; 12 | solve in(x1,y1,0); 13 | cola.push(in); 14 | while(!cola.empty()){ 15 | solve now=cola.front(); 16 | cola.pop(); 17 | if(now.i==x2&&now.j==y2){ 18 | return now.d; 19 | } 20 | if(memo[now.i][now.j])continue; 21 | memo[now.i][now.j]=true; 22 | for(int i=0;i<8;i++){ 23 | int x=now.i+dx[i]; 24 | int y=now.j+dy[i]; 25 | if(x>0&&x<=N&&y>0&&y<=M){ 26 | solve r(x,y,now.d+1); 27 | cola.push(r); 28 | } 29 | } 30 | } 31 | return -1; 32 | } 33 | -------------------------------------------------------------------------------- /Level8/Word Ladder I.cpp: -------------------------------------------------------------------------------- 1 | mapmapa; 2 | mapmemo; 3 | mapDP; 4 | bool ok; 5 | string fin; 6 | int dp(string v){ 7 | if(v==fin){ 8 | ok=true; 9 | return 0; 10 | } 11 | if(DP[v])return DP[v]; 12 | int mini=(1<<30); 13 | for(int i=0;i &dictV) { 30 | fin=end; 31 | ok=false; 32 | DP.clear(); 33 | mapa.clear(); 34 | memo.clear(); 35 | for(int i=0;im; 2 | string v; 3 | bool ok; 4 | int n,c; 5 | int dx[]={1,-1,0,0}; 6 | int dy[]={0,0,1,-1}; 7 | void solve(int i,int x,int y){ 8 | if(ok)return; 9 | if(i==v.size()){ 10 | ok=true; 11 | return; 12 | } 13 | for(int j=0;j<4;j++){ 14 | if(x+dx[j]>=n||x+dx[j]<0||y+dy[j]>=c||y+dy[j]<0)continue; 15 | if(m[x+dx[j]][y+dy[j]]==v[i])solve(i+1,x+dx[j],y+dy[j]); 16 | } 17 | } 18 | int Solution::exist(vector &A, string B) { 19 | ok=false; 20 | n=A.size(); 21 | c=A[0].size(); 22 | m=A; 23 | v=B; 24 | for(int i=0;i