├── Airplane.cpp ├── BasestationsConstruction.cpp ├── BurstBalloons.cpp ├── CPU_Selling.cpp ├── Fishery.cpp ├── KimAndRefrigirators.cpp ├── Marathon.cpp ├── README.md ├── RareElements.cpp ├── RobotRefuling.cpp ├── SamsungMobile(salesBooth).cpp ├── TollGate.cpp ├── TunnelConstruction.cpp └── WormHoles.cpp /Airplane.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int a[13][5],b[5][5]; 4 | 5 | void detonate(int row){ 6 | for(int i=row;i>=row-5;i--){ 7 | for(int j=0;j<5;j++){ 8 | b[row-i][j]=0; 9 | if(i>=0 && a[i][j]==2){ 10 | a[i][j]=0; 11 | b[row-i][j]=2; 12 | } 13 | } 14 | } 15 | } 16 | 17 | void undetonate(int row){ 18 | for(int i=row;i>=row-5;i--){ 19 | for(int j=0;j<5;j++){ 20 | if(i>=0 && b[row-i][j]==2){ 21 | a[i][j]=2; 22 | } 23 | } 24 | } 25 | } 26 | 27 | void getMax(int pos,int coins,int n,int& maxCoins){ 28 | if(pos<0 || pos>4 || coins<0) return; 29 | if(a[n-1][pos]==1) coins++; 30 | if(a[n-1][pos]==2) coins--; 31 | if(n==1){ 32 | if(coins>maxCoins) maxCoins=coins; 33 | return; 34 | } 35 | else{ 36 | getMax(pos-1,coins,n-1,maxCoins); 37 | getMax(pos+1,coins,n-1,maxCoins); 38 | getMax(pos,coins,n-1,maxCoins); 39 | } 40 | } 41 | 42 | int main(){ 43 | int t; 44 | cin >> t; 45 | while(t--){ 46 | int n; 47 | int ans=-1; 48 | cin >> n; 49 | for(int i=0;i> a[i][j]; 52 | } 53 | } 54 | for(int i=0;i<5;i++) a[n][i]=0; 55 | a[n][2]=3; 56 | for(int i=n-1;i>=0;i--){ 57 | int coins=-1; 58 | detonate(i); 59 | getMax(2,0,n,coins); 60 | if(coins>ans) ans=coins; 61 | undetonate(i); 62 | // getMax(2,0,n,coins); 63 | // if(coins>ans) ans=coins; 64 | } 65 | if(ans<0) cout << -1 << endl; 66 | else cout << ans << endl; 67 | } 68 | } -------------------------------------------------------------------------------- /BasestationsConstruction.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int ans=-1; 5 | int n,m; 6 | int base[13][13]; 7 | bool vis[13][13]; 8 | int odd[6][2]={{-1,0},{1,0},{0,-1},{0,1},{1,-1},{1,1}}; 9 | int even[6][2]={{-1,0},{1,0},{0,-1},{0,1},{-1,-1},{-1,1}}; 10 | 11 | bool valid(int i,int j){ 12 | if(i>=0 && i=0 && jans) ans=temp; 62 | return; 63 | } 64 | if(j%2==0){ 65 | for(int k=0;k<6;k++){ 66 | int x=i+even[k][0]; 67 | int y=j+even[k][1]; 68 | if(x>=0 && x=0 && y=0 && x=0 && y> n >> m; 86 | ans=0; 87 | for(int i=0;i> base[i][j]; 90 | vis[i][j]=false; 91 | } 92 | } 93 | for(int i=0;ians) ans=iy; 101 | int y=lowerY(i,j); 102 | //cout << y << endl; 103 | if(y>ans) ans=y; 104 | } 105 | } 106 | } 107 | cout << ans << endl; 108 | } -------------------------------------------------------------------------------- /BurstBalloons.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int dp[502][502]; 4 | vector balloons; 5 | int helper(int l,int r,int n){ 6 | if(dp[l][r]!=-1) return dp[l][r]; 7 | int ans=0; 8 | for(int i=l+1;i& nums) { 22 | balloons=nums; 23 | memset(dp,-1,sizeof(dp)); 24 | balloons.insert(balloons.begin(),1); 25 | balloons.push_back(1); 26 | int n=nums.size(); 27 | int ans=helper(0,n+1,n); 28 | return ans; 29 | } 30 | }; -------------------------------------------------------------------------------- /CPU_Selling.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define max(a,b) (a>b)?a:b 3 | using namespace std; 4 | 5 | int prod[8][4]; 6 | int dp[100][100][100]={-1}; 7 | 8 | int helper(int ncpu,int pcpu,int nmem,int pmem,int nboard,int n){ 9 | if(dp[ncpu][nmem][nboard]!=-1) return dp[ncpu][nmem][nboard]; 10 | else{ 11 | int temp1=0; 12 | int temp2=ncpu*pcpu+nmem*pmem; 13 | for(int i=0;i=0 && nmem-prod[i][1]>=0 && nboard-prod[i][2]>=0){ 15 | temp1=max(temp1,helper(ncpu-prod[i][0],pcpu,nmem-prod[i][1],pmem,nboard-prod[i][2],n)+prod[i][3]); 16 | } 17 | } 18 | dp[ncpu][nmem][nboard]=max(temp1,temp2); 19 | return dp[ncpu][nmem][nboard]; 20 | } 21 | } 22 | 23 | int main(){ 24 | int t; 25 | cin >> t; 26 | while(t--){ 27 | int ncpu,nmem,nboard,pcpu,pmem,n; 28 | cin >> ncpu >> nmem >> nboard >> pcpu >> pmem >> n; 29 | for(int i=0;i> prod[i][0] >> prod[i][1] >> prod[i][2] >> prod[i][3]; 31 | } 32 | for(int i=0;i<100;i++) 33 | for(int j=0;j<100;j++) 34 | for(int k=0;k<100;k++) 35 | dp[i][j][k]=-1; 36 | cout << helper(ncpu,pcpu,nmem,pmem,nboard,n) << endl; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /Fishery.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int ans=100000; 4 | 5 | void calculate(int j,int* gate,int* person,bool* vis,int* prem,int dis,int n){ 6 | if(j==3){ 7 | if(dis=0 && vis[left]){ 17 | left--; 18 | if(left==-1) break; 19 | } 20 | while(right=right-gate[i]){ 48 | dis+=right-gate[i]+1; 49 | vis[right]=true; 50 | right++; 51 | } 52 | } 53 | calculate(j+1,gate,person,vis,prem,dis,n); 54 | } 55 | 56 | int main(){ 57 | int t; 58 | cin >> t; 59 | while(t--){ 60 | int n; 61 | cin >> n; 62 | int a[n]; 63 | int g[3],p[3]; 64 | for(int i=0;i<3;i++){ 65 | cin >> g[i] >> p[i]; 66 | g[i]--; 67 | } 68 | bool vis[n]; 69 | int premu[3]; 70 | premu[0]=0,premu[1]=1,premu[2]=2; 71 | for(int i=0;i 2 | using namespace std; 3 | int x[20],y[20],n,ans; 4 | 5 | int abs(int i){//Absolute function 6 | if(i>0){ 7 | return i; 8 | } 9 | return -i; 10 | } 11 | 12 | int dist(int i, int j){//Calc dist between 2 points 13 | int x1 = x[i], x2 = x[j]; 14 | int y1 = y[i], y2 = y[j]; 15 | 16 | return (abs(x1-x2) + abs(y1-y2)); 17 | } 18 | 19 | void optimalPath(int x,bool visited[],int nodes,int value){ 20 | if(n == nodes){//If number of nodes equal n then set value of answer 21 | ans = min(ans,value + dist(x,n+1)); 22 | } 23 | for(int i=1;i<=n;i++){ 24 | if(!visited[i]){ 25 | visited[i] = true; 26 | optimalPath(i,visited,nodes+1,value + dist(x,i));//Dfs call 27 | visited[i] = false; 28 | } 29 | } 30 | } 31 | 32 | int main(){ 33 | int tCases; 34 | cin >> tCases;//For testcases 35 | for(int i=0;i> n; 38 | cin >> x[n+1] >> y[n+1] >> x[0] >> y[0];//Input destination and source x,y coordinates 39 | for(int i=1;i<=n;i++){//Input drop off location coordinates 40 | cin >> x[i] >> y[i]; 41 | } 42 | bool visited[n+2]={false}; 43 | optimalPath(0,visited,0,0); 44 | cout << "#" << i+1 << " " << ans << endl; 45 | } 46 | return 0; 47 | } -------------------------------------------------------------------------------- /Marathon.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | int time1[5]; 5 | int enrgy_spent[5]; 6 | 7 | int main(){ 8 | int t,energy,distance,minutes,seconds; 9 | cin>>t; 10 | for(int i=0;i>energy>>distance; 12 | for(int j=0;j<5;j++){ 13 | cin>>minutes>>seconds>>enrgy_spent[j]; 14 | time1[j]=minutes*60+seconds; 15 | } 16 | int arr[distance+1][energy+1]; 17 | for(int j=0;j 48 | using namespace std; 49 | 50 | int d,e; 51 | int a[5][2]; 52 | int dp[1000][1000]; // dp array of dist , energy 53 | 54 | int solve(int d, int e){ 55 | // base case 56 | if(d == 0)return 0; 57 | if(dp[d][e] != 99999)return dp[d][e]; 58 | // recursive case 59 | int ans = INT_MAX - 100000; 60 | for(int i =0; i<5; i++){ 61 | if(e >= a[i][1]){ 62 | int temp = a[i][0] + solve(d-1, e - a[i][1]); 63 | ans = min(ans,temp); 64 | } 65 | } 66 | return dp[d][e] = ans; 67 | } 68 | 69 | int main(){ 70 | 71 | cin>>d>>e; 72 | 73 | for(int i = 0; i<5; i++) 74 | cin>>a[i][0]>>a[i][1]; // corresponds to time and energy 75 | 76 | for(int i = 0; i<1000; i++)for(int j =0; j<1000; j++)dp[i][j] = 99999; 77 | 78 | cout< 2 | using namespace std; 3 | int n,c; 4 | int ans=100000; 5 | class queue{ 6 | int q[100000]; 7 | int front=0; 8 | int back=0; 9 | public: 10 | bool empty(){ 11 | if(front-back==0) return true; 12 | return false; 13 | } 14 | int top(){ 15 | if(back-front==0) return -1; 16 | return q[front]; 17 | } 18 | void push(int x){ 19 | if(back<100000) q[back++]=x; 20 | } 21 | void pop(){ 22 | front++; 23 | } 24 | }; 25 | 26 | int min(int a,int b){ 27 | if(a>b) return b; 28 | else return a; 29 | } 30 | 31 | int max(int a,int b){ 32 | if(a>b) return a; 33 | return b; 34 | } 35 | 36 | bool valid(int i,int j,int** matrix){ 37 | if(i<0 || i>=n || j<0 || j>=n || matrix[i][j]==0) return false; 38 | return true; 39 | } 40 | 41 | void bfs(int** mat,int** vis,int** res,int x,int y){ 42 | queue q; 43 | q.push(x); 44 | q.push(y); 45 | int temp=100000; 46 | while(!q.empty()){ 47 | int i=q.top();q.pop(); 48 | int j=q.top();q.pop(); 49 | int cnt=0; 50 | for(int k=0;k0) cnt++; 52 | } 53 | if(cnt>=c) return; 54 | int val=vis[i][j]; 55 | if(valid(i+1,j,mat)){ 56 | vis[i+1][j]=val+1; 57 | q.push(i+1); 58 | q.push(j); 59 | } 60 | if(valid(i-1,j,mat)){ 61 | vis[i-1][j]=val+1; 62 | q.push(i-1); 63 | q.push(j); 64 | } 65 | if(valid(i,j+1,mat)){ 66 | vis[i][j+1]=val+1; 67 | q.push(i); 68 | q.push(j+1); 69 | } 70 | if(valid(i,j-1,mat)){ 71 | vis[i][j-1]=val+1; 72 | q.push(i); 73 | q.push(j-1); 74 | } 75 | } 76 | } 77 | 78 | void clear(int** vis){ 79 | for(int i=0;i>t; 89 | while(t--){ 90 | cin>>n>>c; 91 | int **matrix; 92 | int **vis; 93 | queue q; 94 | int **res=new int*[c]; 95 | for(int i=0;i> res[i][0] >> res[i][1]; 98 | res[i][0]--; 99 | res[i][1]--; 100 | } 101 | matrix=new int*[n]; 102 | vis=new int*[n]; 103 | for(int i=0;i>matrix[i][j]; 108 | vis[i][j]=0; 109 | } 110 | } 111 | for(int i=0;i 2 | #include 3 | using namespace std; 4 | int n; 5 | int ans=100000; 6 | int a[9]; 7 | int vis[9]; 8 | 9 | // ins 0 is continuing ans refuiling same type of cars until fuel>0 10 | // ins 1 is going back to the gas station 11 | // int 2 is going to the disel station 12 | 13 | void helper(int n,int dis,int remf,int pind,int cars,int ins,int gas){ 14 | if(n==cars){ 15 | if(ans>dis){ 16 | ans=dis; 17 | } 18 | return; 19 | } 20 | if(remf<=0) return; 21 | if(ins==0 && gas==0){ 22 | for(int k=1;k<=n;k++){ 23 | if(vis[k]==0 && a[k]==1){ 24 | vis[k]=1; 25 | helper(n,dis+abs(pind-k),remf-1,k,cars+1,0,0); 26 | helper(n,dis+abs(pind-k),2,k,cars+1,1,0); 27 | helper(n,dis+abs(pind-k),2,k,cars+1,2,0); 28 | vis[k]=0; 29 | } 30 | } 31 | } 32 | if(ins==0 && gas==1){ 33 | for(int k=n;k>=1;k--){ 34 | if(vis[k]==0 && a[k]==2){ 35 | vis[k]=1; 36 | helper(n,dis+abs(pind-k),remf-1,k,cars+1,0,1); 37 | helper(n,dis+abs(pind-k),2,k,cars+1,1,1); 38 | helper(n,dis+abs(pind-k),2,k,cars+1,2,1); 39 | vis[k]=0; 40 | } 41 | } 42 | } 43 | if(ins==1){ 44 | helper(n,dis+pind,2,0,cars,0,0); 45 | } 46 | if(ins==2){ 47 | helper(n,dis+(n+1-pind),2,n+1,cars,0,1); 48 | } 49 | } 50 | 51 | int main(){ 52 | int t; 53 | cin >> t; 54 | while(t--){ 55 | cin >> n; 56 | ans=100000; 57 | for(int i=0;i<=n;i++) vis[i]=0; 58 | for(int i=1;i<=n;i++){ 59 | cin >> a[i]; 60 | } 61 | helper(n,0,2,0,0,0,0); 62 | if(ans==100000){ 63 | helper(n,n+1,2,n+1,0,0,1); 64 | } 65 | cout << ans << endl; 66 | } 67 | } -------------------------------------------------------------------------------- /SamsungMobile(salesBooth).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int starti,endi; 4 | int n; 5 | int arr[100000]; 6 | //QUEUE 7 | void push(int x){ 8 | arr[endi]=x; 9 | endi++; 10 | } 11 | 12 | int pop(){ 13 | starti++; 14 | return arr[starti-1]; 15 | } 16 | 17 | int size(){ 18 | return endi-starti; 19 | } 20 | 21 | int maxi(int countall[5]){ 22 | int minindex; 23 | int min=0; 24 | for(int i=0;i<5;i++){ 25 | if(countall[i]>=min){ 26 | min=countall[i]; 27 | minindex=i; 28 | } 29 | } 30 | return minindex+1; 31 | } 32 | 33 | void dfs1(int ** matrix,int **visited,int value,int i,int j){ 34 | visited[i][j]=1; 35 | int x[4]={1,-1,0,0}; 36 | int y[4]={0,0,-1,1}; 37 | for(int k=0;k<4;k++){ 38 | int l=i+x[k]; 39 | int m=j+y[k]; 40 | if(l>=0 && l=0 && m=0 && l=0 && m0){ 70 | x=pop(); 71 | y=pop(); 72 | if(matrix[x][y]==0){ 73 | //cout << x << " " << y << endl; 74 | for(int k=0;k<4;k++){ 75 | int i=x+l[k]; 76 | int j=y+t[k]; 77 | if(i>=0 && i=0 && j=0 && i=0 && j>t; 110 | while(t--){ 111 | cin>>n; 112 | int **matrix; 113 | int **visited; 114 | starti=0; 115 | endi=0; 116 | matrix=new int*[n]; 117 | visited=new int*[n]; 118 | for(int i=0;i>matrix[i][j]; 123 | visited[i][j]=0; 124 | } 125 | } 126 | for(int i=0;i5) 144 | matrix[i][j]=matrix[i][j]-5; 145 | } 146 | } 147 | int count=0; 148 | for(int i=0;i 2 | #include 3 | using namespace std; 4 | int n; 5 | int tc[25]; 6 | int th[25]; 7 | int ans; 8 | 9 | void dfs(int i,int a,int b,int c,int cost){ 10 | if(cost>ans) return; 11 | int tot=a+b+c; 12 | if(i==n-1){ 13 | if(totth[i]){ 20 | if(th[i]>b+c) a=tot-th[i]; 21 | if(th[i]>c) b=th[i]-c>=b?0:b+c-th[i]; 22 | dfs(i+1,0,a,b,cost); 23 | } 24 | } 25 | 26 | int main(){ 27 | int t; 28 | cin >> t; 29 | while(t--){ 30 | cin >> n; 31 | ans=100000; 32 | for(int i=0;i> th[i] >> tc[i]; 34 | } 35 | dfs(0,0,0,0,0); 36 | cout << ans << endl; 37 | } 38 | } -------------------------------------------------------------------------------- /TunnelConstruction.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int n,m; 4 | int s_x,s_y,l; 5 | int arr[50][50]; 6 | int vis[50][50]; 7 | int directions[8][4]; 8 | int answer[50][50]; 9 | 10 | void setDirections(){ 11 | // 0 left 1 top 2 right 3 bottom 12 | directions[0][0]=0; 13 | directions[0][1]=0; 14 | directions[0][2]=0; 15 | directions[0][3]=0; 16 | directions[1][0]=1; 17 | directions[1][1]=1; 18 | directions[1][2]=1; 19 | directions[1][3]=1; 20 | directions[2][0]=0; 21 | directions[2][1]=1; 22 | directions[2][2]=0; 23 | directions[2][3]=1; 24 | directions[3][0]=1; 25 | directions[3][1]=0; 26 | directions[3][2]=1; 27 | directions[3][3]=0; 28 | directions[4][0]=0; 29 | directions[4][1]=1; 30 | directions[4][2]=1; 31 | directions[4][3]=0; 32 | directions[5][0]=0; 33 | directions[5][1]=0; 34 | directions[5][2]=1; 35 | directions[5][3]=1; 36 | directions[6][0]=1; 37 | directions[6][1]=0; 38 | directions[6][2]=0; 39 | directions[6][3]=1; 40 | directions[7][0]=1; 41 | directions[7][1]=1; 42 | directions[7][2]=0; 43 | directions[7][3]=0; 44 | } 45 | 46 | bool isvalid(int x,int y){ 47 | if(x>=0 && x=0 && y> t; 76 | setDirections(); 77 | while(t--){ 78 | cin >> n >> m; 79 | cin >> s_x >> s_y >> l; 80 | for(int i=0;i> arr[i][j]; 83 | vis[i][j]=false; 84 | answer[i][j]=0; 85 | } 86 | } 87 | int ans=0; 88 | if(arr[s_x][s_y]==0) cout << ans << endl; 89 | else{ 90 | dfs(s_x,s_y,0); 91 | for(int i=0;i 2 | using namespace std; 3 | int n; 4 | int x[13]; 5 | int y[13]; 6 | int w[5]; 7 | int g[13][13]; 8 | int vis[13]; 9 | 10 | int abs(int a){ 11 | if(a<0) return -1*a; 12 | return a; 13 | } 14 | 15 | int min(int a,int b){ 16 | if(adist[index]+g[index][j]){ 41 | dist[j]=dist[index]+g[index][j]; 42 | } 43 | } 44 | } 45 | return dist[1]; 46 | } 47 | 48 | int main() { 49 | int t; 50 | cin >> t; 51 | while(t--){ 52 | cin >> n; 53 | cin >> x[0] >> y[0] >> x[1] >> y[1] ; 54 | int k=2; 55 | for(int j=0;j> x[k] >> y[k]; 57 | k++; 58 | cin >> x[k] >> y[k]; 59 | cin >> w[k-1]; 60 | k++; 61 | } 62 | for(int i=0;i