├── README.md ├── string compression.cpp ├── detect cycle undirected graph.cpp ├── detect cycle directed graph.cpp ├── piller pipe.cpp ├── sum level K.cpp ├── bipartite vertices with same colour.cpp ├── necklace.cpp ├── aggressive cow.cpp ├── laughing gas.cpp ├── omnious number.cpp ├── burst balloon 2.cpp ├── frog jump.cpp ├── convex hull.cpp ├── burst balloon 1.cpp ├── wormhole Floyd Warshall.cpp ├── doctor probability.cpp ├── 2D Mtrix.cpp ├── marathon.cpp ├── sinkhole.cpp ├── chess piece.cpp ├── treeCutting.cpp ├── mr lee.cpp ├── oil mine.cpp ├── old phone.cpp ├── jewel maze.cpp ├── wormhole DFS.cpp ├── crow pot.cpp ├── toggle column.cpp ├── endoscopy.cpp ├── mr kim.cpp ├── fishermen.cpp ├── research team.cpp ├── spaceship bomb.cpp └── Two Problems Mixed.cpp /README.md: -------------------------------------------------------------------------------- 1 | # Samsung Problems for R&D India 2 | 3 | ### MUST DO PROBLEMS FOR ONLINE 3-HOUR TEST 4 | * Bipatite Graph 5 | * Detect and Print Cycle in Graph 6 | * Burst Balloon 7 | * Endoscopy 8 | * Mr Lee 9 | * Mr Kim 10 | * Research Team / Rare Elements 11 | * Spaceship / Bomb Explosion 12 | * Wormhole 13 | * Omnious Number 14 | 15 | Rest problems are also asked in various Samsung India 3-hour Coding Test. 16 | 17 | ### Other Helpful Resources 18 | * 19 | * 20 | * GeeksForGeeks Samsung Coding Section 21 | 22 | #### For any changes or additional questions, please feel free to raise a PR 23 | -------------------------------------------------------------------------------- /string compression.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int lengthOfString(char *str){ 5 | int len = 0, i = 0; 6 | while(str[i] != '\0'){ 7 | len++; 8 | i++; 9 | } 10 | return len; 11 | } 12 | 13 | void mergeAplha(char *str){ 14 | int lenString = lengthOfString(str); 15 | int hash[26] = {0}; 16 | 17 | for(int i=0; i= '0' && str[i] <= '9'){ 23 | temp = (temp * 10) + (str[i]-'0'); 24 | i++; 25 | } 26 | i--; 27 | 28 | hash[letter - 'a'] += temp; 29 | } 30 | 31 | for(int i=0; i<26; i++){ 32 | if(hash[i] != 0){ 33 | char temp = i + 'a'; 34 | cout << temp << " " << hash[i] << " "; 35 | } 36 | } 37 | 38 | } 39 | 40 | int main() { 41 | char s[100]; 42 | cin >> s; 43 | mergeAplha(s); 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /detect cycle undirected graph.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int a[100][100], n; 5 | 6 | bool findcycle(int st, bool visited[], int parent, int &prev){ 7 | visited[st]=true; 8 | 9 | for(int j=0;j> n >> m; 40 | 41 | int x,y; 42 | while(m--){ 43 | cin>>x>>y; 44 | a[x][y]=1; 45 | a[y][x]=1; 46 | } 47 | 48 | bool visited[n] = {false}; 49 | int parent = -1, prev = -1; 50 | findcycle(0,visited,parent,prev); 51 | 52 | return 0; 53 | } -------------------------------------------------------------------------------- /detect cycle directed graph.cpp: -------------------------------------------------------------------------------- 1 | /* https://sapphireengine.com/@/6tfphj */ 2 | /* https://cses.fi/problemset/task/1678 */ 3 | #include 4 | using namespace std; 5 | 6 | int graph[100][100]; 7 | int n; 8 | 9 | bool dfs( int node , bool *visited , bool *inloop , int &prev ){ 10 | visited[node] = 1; 11 | inloop[node] = 1; 12 | for(int i=0;i>n; 50 | for(int i=0;i>t; 57 | int x,y; 58 | for(int i=0;i>x>>y; 60 | graph[x][y]=1; 61 | } 62 | 63 | bool visited[n] = {false}; 64 | 65 | cout< 21 | using namespace std; 22 | 23 | void solve(int a[], int vis[], int p1, int p2, int n, int &ans){ 24 | if(p1 == p2){ 25 | if(p1 > ans){ 26 | ans = p1; 27 | } 28 | } 29 | 30 | 31 | for(int i=0 ; i>n; 44 | int a[n]; 45 | 46 | for(int i=0 ; i>a[i]; 48 | } 49 | 50 | int vis[n] = {0}; 51 | int ans = -1; 52 | solve(a,vis,0,0,n,ans); 53 | 54 | cout< 2 | using namespace std; 3 | 4 | typedef long long int LL; 5 | 6 | #define scll(x) scanf("%lld",&x); 7 | #define sci(x) scanf("%d",&x); 8 | #define prll(x) printf("%lld\n",x); 9 | #define pri(x) printf("%d\n",x); 10 | 11 | /* 12 | 3 13 | (0(5(6()())(-4()(9()())))(7(1()())(3()()))) 14 | */ 15 | 16 | int main() 17 | { 18 | int arr[2][100]={0}; 19 | int size = 0; 20 | int x; 21 | string str; 22 | 23 | cin>>x; 24 | cin>>str; 25 | int level=0, temp=0, commit=0, neg=0; 26 | for(int i=0; i arr[0][i]){ 64 | temp0=arr[0][j]; 65 | temp1=arr[1][j]; 66 | arr[0][j]=arr[0][i]; 67 | arr[1][j]=arr[1][i]; 68 | arr[0][i]=temp0; 69 | arr[1][i]=temp1; 70 | } 71 | 72 | } 73 | } 74 | 75 | int sum = 0; 76 | for(int i=0;i 11 | using namespace std; 12 | int n; 13 | int arr[100][100]={0}; 14 | 15 | bool isBiPartite(int i,int color[]){ 16 | bool flag = true; 17 | for(int j=0;j> n;//No of nodes in graph 33 | int color[n];//For coloring of graph 34 | 35 | for(int i=0;i> arr[i][j];//Input graph adjacency matrix 39 | } 40 | } 41 | 42 | for(int i=0;i 18 | #include 19 | #include 20 | 21 | using namespace std; 22 | 23 | int solve(string s, int n) 24 | { 25 | unordered_map mpp; 26 | mpp[0] = -1; 27 | int balance = 0; 28 | int maxLen = 0; 29 | for (int i = 0; i < s.length(); i++) 30 | { 31 | if (s[i] == 'R') 32 | { 33 | balance--; 34 | } 35 | else 36 | { 37 | balance++; 38 | } 39 | 40 | if (mpp.find(balance) != mpp.end()) 41 | { 42 | if (i >= n && mpp[balance] < n) 43 | { 44 | int len = i - mpp[balance]; 45 | maxLen = max(maxLen, len); 46 | } 47 | } 48 | else 49 | { 50 | mpp[balance] = i; 51 | } 52 | } 53 | return s.length() - maxLen; 54 | } 55 | 56 | int main(int argc, char const *argv[]) 57 | { 58 | int testCases; 59 | cin >> testCases; 60 | 61 | for (int i = 0; i < testCases; i++) 62 | { 63 | int n; 64 | string s; 65 | cin >> n; 66 | cin >> s; 67 | 68 | int res = solve(s, n); 69 | cout << "#" << i << " " << res << endl; 70 | } 71 | return 0; 72 | } 73 | -------------------------------------------------------------------------------- /aggressive cow.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | https://paste.ubuntu.com/p/ZtGjSfVwTV/ 3 | https://ide.codingblocks.com/s/16346 4 | https://www.youtube.com/watch?v=TC6snf6KPdE 5 | */ 6 | 7 | /* 8 | You are given an array of integers which represents positions available and an integer c(cows). 9 | Now you have to choose c positions such that minimum difference between cows is maximized. 10 | For example, 11 | 1 3 5 8 10 12 | c=3 13 | 14 | Output: 4 15 | 1 5 10 16 | */ 17 | 18 | #include 19 | using namespace std; 20 | 21 | bool Comperator(int x,int a[],int n,int k){// We want to know if we can get at least x distance with k cows 22 | int currentCows = 1; 23 | int leftmost = 0; 24 | 25 | for(int i=1;i= x){ 27 | leftmost = i; 28 | ++currentCows; 29 | if(currentCows == k) 30 | return 1; 31 | } 32 | } 33 | return 0; 34 | } 35 | 36 | int main() 37 | { 38 | int t; cin >> t; 39 | for(int i=0;i> n >> k; 41 | int a[100000]; 42 | for(int j=0;j> a[j]; 44 | } 45 | 46 | sort(a,a+n); 47 | 48 | int l = 0; 49 | int r = a[n-1] - a[0] + 1; 50 | // If we can do with x distance then obviosult we can do it with <=x. 51 | // So We need to update it 52 | //True True True True True True True . .. . . False False False ==> We want to find the last true 53 | 54 | while(r - l > 0){ 55 | int m = (l + r + 1) /2; 56 | 57 | if(Comperator(m,a,n,k)==true){ 58 | l = m; // l is true now 59 | } 60 | else 61 | r = m-1; // R is false now 62 | } 63 | 64 | cout << l << '\n'; 65 | } 66 | } -------------------------------------------------------------------------------- /laughing gas.cpp: -------------------------------------------------------------------------------- 1 | //https://www.cnblogs.com/kingshow123/p/practicec1.html 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | typedef struct 9 | { 10 | int x; 11 | int y; 12 | int level; 13 | }data; 14 | int mv[4][2] = {{1,0},{0,1},{-1,0},{0,-1}}; 15 | //int mv[8][2] = {{-2,1},{-1,2},{1,2},{2,1},{2,-1},{1,-2},{-1,-2},{-2,-1}}; 16 | 17 | int main() 18 | { 19 | //freopen("test.txt","r",stdin); 20 | int T; 21 | cin>>T; 22 | for(int t=1; t<=T; t++) 23 | { 24 | int n,m; 25 | int r,c; 26 | cin>>n>>m; 27 | int a[m+1][n+1]; 28 | memset(a,0,sizeof(int)*m*n); 29 | for(int i=1; i<=m; i++) 30 | { 31 | for(int j=1; j<=n; j++) 32 | { 33 | cin>>a[i][j]; 34 | } 35 | } 36 | 37 | cin>>r>>c; 38 | data d,d1,d2; 39 | queue qt; 40 | int tmx,tmy,tml; 41 | d.x = c; 42 | d.y = r; 43 | d.level = 1; 44 | qt.push(d); 45 | a[d.x][d.y] = 2; 46 | while(!qt.empty()) 47 | { 48 | d1 = qt.front(); 49 | qt.pop(); 50 | for(int k=0; k<4; k++) 51 | { 52 | tmx = d1.x + mv[k][0]; 53 | tmy = d1.y + mv[k][1]; 54 | tml = d1.level + 1; 55 | if(a[tmx][tmy] == 1) 56 | { 57 | d2.x = tmx; 58 | d2.y = tmy; 59 | d2.level = tml; 60 | a[d2.x][d2.y] = 2; 61 | qt.push(d2); 62 | } 63 | } 64 | } 65 | cout<<"Case #"< 26 | using namespace std; 27 | 28 | int numberOminous(int a, int b, int k, int *delNos, int n){ 29 | int count = 0; 30 | for(int i = a; i <= b; i++){ 31 | int temp = i; 32 | int digitArray[10] = {0}; 33 | 34 | while(temp){ 35 | digitArray[temp%10]++; 36 | temp /= 10; 37 | } 38 | 39 | int rougeK = 0; 40 | for(int i=0; i> a >> b >> k; 55 | int n; 56 | cin >> n; 57 | int *delNos = new int[n]; 58 | for(int i=0; i> delNos[i]; 60 | } 61 | 62 | cout << numberOminous(a, b, k, delNos, n) << "\n"; 63 | 64 | return 0; 65 | } 66 | -------------------------------------------------------------------------------- /burst balloon 2.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | https://www.youtube.com/watch?v=IFNibRVgFBo - Tushar Roy 3 | 4 | Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented by array nums. 5 | You are asked to burst all the balloons. If the you burst balloon i you will get nums[left] * nums[i] * nums[right] coins. 6 | Here left and right are adjacent indices of i. After the burst, the left and right then becomes adjacent. 7 | 8 | Find the maximum coins you can collect by bursting the balloons wisely. 9 | Input: [3,1,5,8] 10 | Output: 167 11 | */ 12 | 13 | int maxCoins(vector& nums) { 14 | /* O(n^3) Time and O(n^2) Space */ 15 | int size = nums.size(); 16 | if(size == 0) 17 | return 0; 18 | 19 | int i, j, k; 20 | vector< vector > dp(size, vector(size, 0)); 21 | 22 | for(int len = 1; len <= size; len++){ 23 | for(i = 0; i <= size - len; i++){ 24 | j = len + i - 1; 25 | for(k = i; k <= j; k++){ 26 | /* Left/Right Value has default 1 but holds prev and after ballon value if k is in middle */ 27 | int leftValue = 1; 28 | int rightValue = 1; 29 | 30 | if(i != 0) 31 | leftValue = nums[i-1]; 32 | if(j != size-1) 33 | rightValue = nums[j+1]; 34 | 35 | /* Before and After - current k balloon is last to burst select the left side and right side to burst */ 36 | int before = 0; 37 | int after = 0; 38 | 39 | if(i != k) 40 | before = dp[i][k-1]; 41 | if(j != k) 42 | after = dp[k+1][j]; 43 | 44 | dp[i][j] = max(dp[i][j], 45 | leftValue * nums[k] * rightValue + before + after); 46 | } 47 | } 48 | } 49 | return dp[0][size-1]; 50 | } 51 | -------------------------------------------------------------------------------- /frog jump.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | https://www.careercup.com/question?id=14989765 3 | http://ideone.com/oXdBaF 4 | 5 | Given a 2 D matrix where 1 represent the places where the frog can jump and 0 represent the empty spaces, 6 | the frog can move freely in horizontal direction (on 1’s only) without incurring any cost (jump). 7 | A vertical jump from a given point of the matrix to other point on the matrix can be taken (on 1’s only) 8 | with cost as the number of jumps taken. 9 | Given a source and destination, the frog has to reach the destination minimizing the cost (jump). 10 | */ 11 | 12 | #include 13 | using namespace std; 14 | #define QS 1000005 15 | 16 | struct Point{ 17 | int x, y; 18 | }; 19 | 20 | int n, sX, sY, tX, tY; 21 | int mat[105][105], dis[105][105], vis[105][105]; 22 | 23 | Point queue[QS]; 24 | int front = 0, rear = 0; 25 | 26 | int dirX[] = {1,0,-1,0}; 27 | int dirY[] = {0,1,0,-1}; 28 | 29 | bool isValid(int i, int j){ 30 | return (i>=0 && i=0 && j> n; 72 | for(int i=0; i> mat[i][j]; 75 | vis[i][j] = 0; 76 | dis[i][j] = 0; 77 | } 78 | } 79 | 80 | cin >> sX >> sY >> tX >> tY; 81 | 82 | calculateFrogJump(); 83 | return 0; 84 | } -------------------------------------------------------------------------------- /convex hull.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given random points in a 2-D plane, construct a convex polygon with minimum area of covering and 3 | which encompasses all the given points. 4 | */ 5 | #include 6 | int cou = 0; 7 | 8 | struct Point{ 9 | int x, y; 10 | }; 11 | 12 | int orientation(Point p, Point q, Point r){ 13 | int val = (q.y - p.y) * (r.x - q.x) - 14 | (q.x - p.x) * (r.y - q.y); 15 | 16 | if (val == 0) return 0; 17 | return (val > 0)? 1: 2; 18 | } 19 | 20 | bool cmp(Point &a, Point &b){ 21 | if(a.x==b.x&&a.y==b.y) 22 | cou++; 23 | 24 | if(a.x == b.x) 25 | return a.y < b.y; 26 | else 27 | return a.x < b.x; 28 | } 29 | 30 | bool myFunc(Point &a, Point &b){ 31 | return (a.x==b.x && a.y==b.y); 32 | } 33 | 34 | void convexHull(Point *points, int n){ 35 | cou = 0; 36 | if (n < 3){ 37 | cout << "-1"; 38 | return; 39 | } 40 | 41 | vector hull; 42 | 43 | int l = 0; 44 | for (int i = 1; i < n; i++) 45 | if (points[i].x < points[l].x) 46 | l = i; 47 | 48 | int p = l, q; 49 | do{ 50 | hull.push_back(points[p]); 51 | 52 | q = (p+1)%n; 53 | 54 | for (int i = 0; i < n; i++) 55 | { 56 | if (orientation(points[p], points[i], points[q]) == 2) 57 | q = i; 58 | } 59 | p = q; 60 | 61 | } while (p != l); 62 | 63 | sort(hull.begin(), hull.end(), cmp); 64 | 65 | auto ip = unique(hull.begin(), hull.end(), myFunc); 66 | 67 | hull.resize(std::distance(hull.begin(), ip)); 68 | 69 | if(n < 4 && cou > 0 || hull.size() < 3){ 70 | cout << "-1"; 71 | return; 72 | } 73 | else{ 74 | for (int i = 0; i < hull.size(); i++){ 75 | if(i != hull.size() - 1) 76 | cout << hull[i].x << " " << hull[i].y << ", "; 77 | else 78 | cout << hull[i].x << " " << hull[i].y; 79 | } 80 | } 81 | } 82 | 83 | int main(){ 84 | int t, n; 85 | cin >> t; 86 | while(t--){ 87 | cin >> n; 88 | Point *points = new Point[n]; 89 | 90 | for(int i=0; i> points[i].x >> points[i].y; 92 | } 93 | 94 | convexHull(points, n); 95 | cout << "\n"; 96 | } 97 | return 0; 98 | } -------------------------------------------------------------------------------- /burst balloon 1.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | There are n balloons and n bullets and each balloon is assigned with a particular number (point). 3 | Whenever a particular balloon is shot the no of points increases by 4 | 1.the multiplication of point assigned to balloon on left and that of right side. 5 | 2.point assigned to left if no right exists 6 | 3.point assigned to right if no left exists. 7 | 4.the point assigned to itself if no other balloon exists. 8 | 9 | You have to output the maximum no of points possible. 10 | 11 | Input-1 12 | 1 2 3 4 13 | 14 | Output-1 15 | 20 16 | 17 | Input-2 18 | 1 0 2 3 0 4 19 | 20 | Output-2 21 | 34 22 | */ 23 | 24 | #include 25 | using namespace std; 26 | 27 | int maxcoins(int A[],int siz) 28 | { 29 | int nums[siz+2]; 30 | int n=1, points = 0; 31 | 32 | for(int i=0;i0) 35 | { 36 | nums[n] = A[i]; 37 | n++; 38 | } 39 | else 40 | points += (n - 1 > 0 ? nums[n - 1] : 1) * (i + 1 < siz ? A[i + 1] : 1); // balloons with zero points should be removed first using previous and next balloon points 41 | 42 | } 43 | nums[0] = nums[n] = 1; 44 | n++; 45 | 46 | // dp[i][j]: maximum points that can be collected from balloons from index i to j, (i and j exclusive) 47 | int dp[n][n] = {}; 48 | 49 | for(int j=2;j> siz; 78 | int A[siz]; 79 | for(int i=0;i> A[i]; 81 | 82 | int ans = maxcoins(A,siz); 83 | cout << ans << endl; 84 | return 0; 85 | } 86 | 87 | /* 88 | 5 89 | 1 2 3 5 4 90 | */ 91 | -------------------------------------------------------------------------------- /wormhole Floyd Warshall.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | struct node{ 4 | int x, y; 5 | }; 6 | 7 | int abs(int x){ 8 | return (x<0) ? -x : x; 9 | } 10 | 11 | int finddist(struct node *first,struct node *second){ 12 | int dstx = abs(first->x - second->x); 13 | int dsty = abs(first->y - second->y); 14 | return dstx + dsty; 15 | } 16 | 17 | int main(){ 18 | int test; 19 | cin >> test; 20 | for(int t=1;t<=test;t++){ 21 | int warmholes; 22 | cin>>warmholes; 23 | 24 | /* Initialise Code */ 25 | int nodes = 2 * warmholes + 2; 26 | struct node *location[nodes]; 27 | 28 | int cost[nodes][nodes]; 29 | for(int i=0;i>sx>>sy; 38 | struct node *src=new struct node; 39 | src->x=sx; 40 | src->y=sy; 41 | location[0]=src; 42 | 43 | //destination 44 | int dox,doy; 45 | cin>>dox>>doy; 46 | struct node *temp=new node; 47 | temp->x=dox; 48 | temp->y=doy; 49 | location[nodes-1]=temp; 50 | 51 | //warmhole 52 | for(int i=0;i>six>>siy; 55 | struct node *temp=new node; 56 | temp->x=six; 57 | temp->y=siy; 58 | 59 | int dix,diy; 60 | cin>>dix>>diy; 61 | struct node *temp1=new node; 62 | temp1->x=dix; 63 | temp1->y=diy; 64 | 65 | int w; 66 | cin>>w; 67 | 68 | location[2*i+1]=temp; 69 | location[2*i+2]=temp1; 70 | cost[2*i+1][2*i+2]=w; 71 | cost[2*i+2][2*i+1]=w; 72 | } 73 | 74 | // Initialise cost matrix 75 | for(int i=0;i 27 | using namespace std; 28 | 29 | void docProb(double **graph, int nodes, int time, int curNode, double p, double *answer){ 30 | if(time <= 0){ 31 | answer[curNode] += p; 32 | return; 33 | } 34 | 35 | for(int i=1; i<=nodes; i++){ 36 | if(graph[curNode][i] != 0){ 37 | p *= graph[curNode][i]; 38 | docProb(graph, nodes, time - 10, i, p, answer); 39 | p /= graph[curNode][i]; 40 | } 41 | } 42 | 43 | } 44 | 45 | int main(){ 46 | int t; 47 | cin >> t; 48 | while(t--){ 49 | int nodes, edges, time; 50 | cin >> nodes >> edges >> time; 51 | 52 | double **arr = new double*[nodes]; 53 | for(int i=1; i<=nodes; i++){ 54 | arr[i] = new double[nodes]; 55 | for(int j=1; j<=nodes; j++){ 56 | arr[i][j] = 0; 57 | } 58 | } 59 | 60 | int from, to; 61 | double prob; 62 | for(int i=0; i> from >> to >> prob; 64 | arr[from][to] = prob; 65 | } 66 | 67 | /* Initalise answer and function call */ 68 | double answer[nodes] = {0.0}; 69 | docProb(arr, nodes, time, 1, 1.0, answer); 70 | 71 | /* Select max Probability node */ 72 | double finalProb = 0.0; 73 | int finalDivison = 0; 74 | 75 | for(int i=1; i<=nodes; i++){ 76 | if(answer[i] > finalProb){ 77 | finalProb = answer[i]; 78 | finalDivison = i; 79 | } 80 | } 81 | cout << finalDivison << " " << finalProb << "\n"; 82 | } 83 | return 0; 84 | } -------------------------------------------------------------------------------- /2D Mtrix.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int t; 8 | cin>>t; 9 | while(t--) 10 | { 11 | int n; 12 | bool isNoPath = false; 13 | cin>>n; 14 | char arr[n][n]; 15 | pair dp[n][n]; 16 | for(int i=0;i>arr[i][j]; 21 | dp[i][j].first = 0; 22 | dp[i][j].second = 0; 23 | } 24 | } 25 | arr[0][0]='0'; 26 | arr[n-1][n-1]='0'; 27 | 28 | //Move to last coulumn 29 | for(int i=n-2;i>=0;i--) 30 | { 31 | if(arr[i][n-1]=='x') 32 | { 33 | for(int j=i;j>=0;j--) 34 | { 35 | dp[j][n-1].first = dp[j][n-1].second = INT_MIN; 36 | } 37 | break; 38 | } 39 | else 40 | { 41 | dp[i][n-1].first = dp[i+1][n-1].first+(arr[i][n-1]-48); 42 | dp[i][n-1].second = 1; 43 | } 44 | } 45 | //Move to last row 46 | for(int i=n-2;i>=0;i--) 47 | { 48 | if(arr[n-1][i]=='x') 49 | { 50 | for(int j=i;j>=0;j--) 51 | { 52 | dp[n-1][j].first = dp[n-1][j].second = INT_MIN; 53 | } 54 | break; 55 | } 56 | else 57 | { 58 | dp[n-1][i].first = dp[n-1][i+1].first+(arr[n-1][i]-48); 59 | dp[n-1][i].second = 1; 60 | } 61 | } 62 | 63 | //Move to Remaining matrix 64 | for(int i=n-2;i>=0;i--) 65 | { 66 | for(int j=n-2;j>=0;j--) 67 | { 68 | if(arr[i][j]=='x') 69 | { 70 | dp[i][j].first = dp[i][j].second = INT_MIN; 71 | } 72 | else 73 | { 74 | int maxi = max(dp[i][j+1].first,max(dp[i+1][j+1].first,dp[i+1][j].first)); 75 | int path = 0; 76 | if(dp[i][j+1].first==maxi) 77 | path += dp[i][j+1].second; 78 | if(dp[i+1][j+1].first==maxi) 79 | path += dp[i+1][j+1].second; 80 | if(dp[i+1][j].first==maxi) 81 | path += dp[i+1][j].second; 82 | if(maxi==INT_MIN) 83 | { 84 | isNoPath = true; 85 | break; 86 | } 87 | dp[i][j].first = maxi+(arr[i][j]-48); 88 | dp[i][j].second = path; 89 | } 90 | } 91 | } 92 | if(isNoPath) 93 | cout<<"0"<<" "<<"0"< 33 | using namespace std; 34 | 35 | class Pace 36 | { 37 | public: 38 | int index; 39 | int time; 40 | int energy; 41 | Pace() {} 42 | Pace(int index, int time, int energy) : index(index), time(time), energy(energy) {} 43 | }; 44 | 45 | int solve(int remainingD, int remainingH, int index, Pace paces[5], int curTime, int minTime) 46 | { 47 | if (remainingD < 0) 48 | return (int)1e9; 49 | if (remainingD == 0) 50 | { 51 | if (remainingH < 0) 52 | return (int)1e9; 53 | return curTime; 54 | } 55 | if (remainingH < 0) 56 | return (int)1e9; 57 | if (index >= 5) 58 | return (int)1e9; 59 | 60 | for (int distance = 0; distance <= remainingD; distance++) 61 | { 62 | int time = curTime + solve(remainingD - distance, 63 | remainingH - (paces[index].energy * distance), 64 | index + 1, 65 | paces, 66 | paces[index].time * distance, 67 | minTime); 68 | minTime = min(minTime, time); 69 | } 70 | return minTime; 71 | } 72 | 73 | int main() 74 | { 75 | ios::sync_with_stdio(false); 76 | cin.tie(nullptr); 77 | 78 | int t; 79 | cin >> t; 80 | 81 | for (int testCase = 1; testCase <= t; testCase++) 82 | { 83 | int D, H; 84 | cin >> D >> H; 85 | 86 | Pace paces[5]; 87 | for (int i = 0; i < 5; i++) 88 | { 89 | int minv, sec, energy; 90 | cin >> minv >> sec >> energy; 91 | paces[i] = Pace(i, minv * 60 + sec, energy); 92 | } 93 | 94 | int minTime = (int)1e9; 95 | minTime = solve(D, H, 0, paces, 0, minTime); 96 | 97 | cout << "#" << testCase << " " << minTime / 60 << " " << minTime % 60 << "\n"; 98 | } 99 | 100 | return 0; 101 | } 102 | -------------------------------------------------------------------------------- /sinkhole.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | https://www.geeksforgeeks.org/samsung-competency-test-25-aug-19/ 3 | */ 4 | /* 5 | ----IIT(ISM) Dhanbad---- 6 | Author: Siddhant Choudhary 7 | 8 | --samsumg coding test-- 9 | 10 | Q.There is a large plot with various sinkholes present. 11 | Since one sinkhole can combine with another sinkhole, it is required to get 12 | at most one sinkhole while occupying the plot. You have to find the maximum 13 | square-area formed with at most one sinkhole present. 14 | If there are two plots with the same area then print the one with 15 | lesser sinkhole present otherwise if the sinkholes are also same then print 16 | anyone. For each case, you have to print the bottom leftmost coordinate and 17 | top rightmost point. Please keep in mind that the plot starts with (1, 1). 18 | 19 | Time limit= 1sec and Memory limit– 256Mb 20 | 21 | Input: First line will give the number of test cases. For each test case, we 22 | will be given the size of the plot matrix N x M (where 1<=N, M<=1000). Next 23 | line will give the number of sinkholes present in the matrix K (1<=K<=N+M). 24 | Next, K-lines will give the coordinates of the sinkholes. 25 | 26 | Output: For each test case, you have to print the number of the test case 27 | and the coordinates of the resultant square. 28 | i.e. #i xb yb xt yt (ith test case, xb=bottomost left x-coordinate, 29 | yb=bottomost left y-coordinate, xt= topmost right x-coordinate, 30 | yt= topmost right y-coordinate) 31 | 32 | * time complexity of my approach = O(n*m*log(min(m,n))) * 33 | */ 34 | 35 | #include 36 | using namespace std; 37 | 38 | #define INT_MAX 100000; 39 | 40 | int xb,yb,xt,yt; 41 | 42 | int fun(int dp[][1001], int N, int M, int k){ 43 | int msum = INT_MAX; 44 | for(int i=0; i<=N-k; i++){ 45 | for(int j=0; j<=M-k; j++){ 46 | int sum = dp[i+k][j+k]-dp[i+k][j]-dp[i][j+k]+dp[i][j]; 47 | if(sum < msum){ 48 | msum = sum; 49 | if(msum <=1){ 50 | xb = i+k; 51 | yb = j+1; 52 | xt = i+1; 53 | yt = j+k; 54 | } 55 | } 56 | } 57 | } 58 | return msum; 59 | } 60 | 61 | int main() { 62 | ios_base::sync_with_stdio(0); 63 | cin.tie(0); 64 | /* input */ 65 | int N,M; 66 | cin>>N>>M; 67 | int A[N][M]; 68 | for(int i=0; i>K; 75 | for(int i=0; i>x>>y; 78 | A[x-1][y-1]=1; 79 | } 80 | 81 | int dp[1001][1001]; /* dp[i][j] = sum of submatrix top-left(0,0) to bottom-right(i,j) */ 82 | for(int i=0; i<1001; i++){ 83 | for(int j=0; j<1001; j++){ 84 | dp[i][j]=0; 85 | } 86 | } 87 | for(int i=1; i<=N; i++){ 88 | for(int j=1; j<=M; j++){ 89 | dp[i][j] = dp[i-1][j]+dp[i][j-1]-dp[i-1][j-1]+A[i-1][j-1]; 90 | } 91 | } 92 | /* applying binary search */ 93 | int l=1, r=min(M,N); 94 | int ones; 95 | while(l 1 means we need to decrease the submatix size 99 | if(ones >1){ 100 | r = mid; 101 | } 102 | //else increase the submatrix size 103 | else{ 104 | l = mid+1; 105 | } 106 | } 107 | /* output */ 108 | cout< 45 | #include 46 | #include 47 | #include 48 | 49 | using namespace std; 50 | typedef struct 51 | { 52 | int x; 53 | int y; 54 | int level; 55 | }data; 56 | int mv[8][2] = {{-2,1},{-1,2},{1,2},{2,1},{2,-1},{1,-2},{-1,-2},{-2,-1}}; 57 | 58 | int main(){ 59 | int T; 60 | cin>>T; 61 | for(int t=1; t<=T; t++) 62 | { 63 | int n,m,r1,c1,r2,c2; 64 | cin>>n>>m; 65 | int a[n+1][m+1]; 66 | memset(a,0,sizeof(int)*(n+1)*(m+1)); 67 | cin>>r1>>c1>>r2>>c2; 68 | data d,d1,d2; 69 | queue qt; 70 | d.x = r1; 71 | d.y = c1; 72 | d.level = 0; 73 | qt.push(d); 74 | a[d.x][d.y] = 2; 75 | int tmx,tmy,tml; 76 | int steps = 0; 77 | bool f = false; 78 | while(!qt.empty()) 79 | { 80 | if(f) 81 | { 82 | break; 83 | } 84 | d1 = qt.front(); 85 | qt.pop(); 86 | for(int k=0; k<8; k++) 87 | { 88 | tmx = d1.x + mv[k][0]; 89 | tmy = d1.y + mv[k][1]; 90 | tml = d1.level + 1; 91 | 92 | if(tmx>=1 && tmx<=n && tmy>=1 && tmy<=m && a[tmx][tmy] == 0) 93 | { 94 | if(tmx == r2 && tmy == c2) 95 | { 96 | steps = tml; 97 | f = true; 98 | break; 99 | } 100 | d2.x = tmx; 101 | d2.y = tmy; 102 | d2.level = tml; 103 | qt.push(d2); 104 | a[d2.x][d2.y] = 2; 105 | } 106 | } 107 | } 108 | if(!f) 109 | { 110 | steps = -1; 111 | } 112 | cout<<"Case #"< length of road (5 <= n <= 1000) 17 | left[i] -> n length array of left side tree heights 18 | right[i] -> n length array of right side tree heights 19 | 20 | E.g. 21 | Input: 22 | 1 23 | 5 24 | 0 3 2 1 0 25 | 0 3 2 1 0 26 | 27 | Output: 28 | #1 11 29 | 30 | */ 31 | 32 | #include 33 | #include 34 | #include 35 | #include 36 | #include 37 | 38 | using namespace std; 39 | 40 | int n; 41 | int dp[1001][1001]; 42 | 43 | int solve(vector> &trees, int treeVal, int pos) { 44 | if (treeVal == 0) { 45 | return n - pos; 46 | } 47 | 48 | if (dp[treeVal][pos] != -1) { 49 | return dp[treeVal][pos]; 50 | } 51 | 52 | if (trees[treeVal].empty()) { 53 | return dp[treeVal][pos] = solve(trees, treeVal - 1, pos); 54 | } 55 | 56 | vector &line = trees[treeVal]; 57 | int t = line.size(); 58 | int cost = INT_MAX; 59 | 60 | int L = line.front(); 61 | int R = line.back(); 62 | 63 | if (pos < L) { 64 | int dist = R - pos; 65 | int nPos = R; 66 | 67 | int curr = t + dist + solve(trees, treeVal - 1, nPos); 68 | cost = min(cost, curr); 69 | } else if (pos > R) { 70 | int dist = pos - L; 71 | int nPos = L; 72 | 73 | int curr = t + dist + solve(trees, treeVal - 1, nPos); 74 | cost = min(cost, curr); 75 | } else { 76 | int dist1 = (pos - L) + (R - L); 77 | int nPos1 = R; 78 | int curr1 = t + dist1 + solve(trees, treeVal - 1, nPos1); 79 | 80 | int dist2 = (R - pos) + (R - L); 81 | int nPos2 = L; 82 | int curr2 = t + dist2 + solve(trees, treeVal - 1, nPos2); 83 | 84 | cost = min({cost, curr1, curr2}); 85 | } 86 | 87 | return dp[treeVal][pos] = cost; 88 | } 89 | 90 | int main(int argc, char const *argv[]) { 91 | int testCases; 92 | cin >> testCases; 93 | 94 | for (int t = 1; t <= testCases; t++) { 95 | cin >> n; 96 | 97 | vector> trees(1001); 98 | memset(dp, -1, sizeof(dp)); 99 | 100 | int a; 101 | for (int i = 0; i < n; i++) { 102 | cin >> a; 103 | if (a > 0) 104 | trees[a].push_back(i); 105 | } 106 | for (int i = 0; i < n; i++) { 107 | cin >> a; 108 | if (a > 0) 109 | trees[a].push_back(i); 110 | } 111 | 112 | for (int i = 0; i < 1001; i++) { 113 | if (trees[i].empty()) { 114 | continue; 115 | } 116 | sort(trees[i].begin(), trees[i].end()); 117 | } 118 | 119 | int minCost = solve(trees, 1000, 0); 120 | cout << "#" << t << " " << minCost << endl; 121 | } 122 | 123 | return 0; 124 | } -------------------------------------------------------------------------------- /mr lee.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Mr. Lee has to travel various offices abroad to assist branches of each place. But he has a problem. 3 | The airfare would be real high as all offices he has to visit are in foreign countries. He wants to visit every 4 | location only one time and return home with the lowest expense. Help this company-caring man calculate the lowest expense. 5 | 6 | 7 | Input format 8 | Several test cases can be included in the inputs. T, the number of cases is given in the first row of the inputs. 9 | After that, the test cases as many as T (T ≤ 30) are given in a row. N, the number of offices to visit is given on 10 | the first row per each test case. At this moment, No. 1 office is regarded as his company (Departure point). 11 | (1 ≤ N ≤ 12) Airfares are given to move cities in which branches are located from the second row to N number rows. 12 | i.e. jth number of ith row is the airfare to move from ith city to jth city. If it is impossible to move between 13 | two cities, it is given as zero. 14 | 15 | Output format 16 | Output the minimum airfare used to depart from his company, visit all offices, and then return his company on the 17 | first row per each test case. 18 | 19 | Example of Input 20 | 21 | 3 22 | 5 23 | 0 14 4 10 20 24 | 14 0 7 8 7 25 | 4 5 0 7 16 26 | 11 7 9 0 2 27 | 18 7 17 4 0 28 | 5 29 | 9 9 2 9 5 30 | 6 3 5 1 5 31 | 1 8 3 3 3 32 | 6 0 9 6 8 33 | 6 6 9 4 8 34 | 3 35 | 0 2 24 36 | 3 0 2 37 | 0 4 0 38 | 39 | Example of Output 40 | 41 | 30 42 | 18 43 | CUSTOM - 31 <- 4 44 | */ 45 | #include 46 | #include 47 | using namespace std; 48 | 49 | int N, result; 50 | 51 | void minCostMrLee(int **arr, bool *visited, int count, int cost, int src){ 52 | // Base Case 53 | if(count == N-1){ 54 | /* Corner Case if no path exists from last city */ 55 | if(arr[src][0] != 0) 56 | result = min(result, cost + arr[src][0]); 57 | return; 58 | } 59 | 60 | // Main Case 61 | for(int i=0; i> t; 73 | while(t--){ 74 | cin >> N; 75 | int **arr = new int*[N]; 76 | for(int i=0; i> arr[i][j]; 85 | } 86 | } 87 | 88 | result = INT_MAX; 89 | 90 | visited[0] = true; 91 | 92 | minCostMrLee(arr, visited, 0, 0, 0); 93 | result != INT_MAX ? cout << result << "\n" : cout << "-1\n"; 94 | } 95 | return 0; 96 | } 97 | 98 | /* 99 | #include 100 | using namespace std; 101 | int arr[1000][1000]; 102 | bool visited[1000]; 103 | int ans = 1000000; 104 | void dfs(int n,int count,int cost,int last) 105 | { 106 | //cout<>t; 131 | while(t--) 132 | { 133 | int n; 134 | ans = 1000000; 135 | cin>>n; 136 | for(int i=0;i>arr[i][j]; 143 | } 144 | } 145 | dfs(n,1,0,0); 146 | cout< 28 | #include 29 | using namespace std; 30 | 31 | int companies, mines, ANS; 32 | 33 | int MIN(int x, int y){ 34 | return (x>=y) ? y : x; 35 | } 36 | 37 | int MAX(int x, int y){ 38 | return (x>=y) ? x : y; 39 | } 40 | 41 | void calculateOilMines(int i, int *oilMines, bool *visited, int minV, int maxV, int sum, 42 | int nodes, int &ANS){ 43 | // Base Case 44 | if(visited[i]){ 45 | int newMin = MIN(sum, minV); 46 | int newMax = MAX(sum, maxV); 47 | 48 | if(nodes == companies - 1){ 49 | ANS = min(ANS, newMax - newMin); 50 | } 51 | return; 52 | } 53 | 54 | // Main Case 55 | visited[i] = 1; 56 | int j = (i + 1) % mines; 57 | 58 | calculateOilMines(j, oilMines, visited, minV, maxV, sum + oilMines[i], nodes, ANS); 59 | 60 | int newMin = MIN(sum, minV); 61 | int newMax = MAX(sum, maxV); 62 | 63 | calculateOilMines(j, oilMines, visited, newMin, newMax, oilMines[i], nodes + 1, ANS); 64 | 65 | visited[i] = 0; 66 | return; 67 | } 68 | 69 | int main() { 70 | // your code goes here 71 | int t; 72 | cin >> t; 73 | while(t--){ 74 | cin >> companies >> mines; 75 | int *oilMines = new int[mines + 1]; 76 | bool *visited = new bool[mines + 1]; 77 | 78 | for(int i=0; i> oilMines[i]; 80 | visited[i] = 0; 81 | } 82 | 83 | ANS = INT_MAX; 84 | for(int i=0; i 94 | using namespace std; 95 | int ans=9999; 96 | void findMinDiff(int company[],int n) 97 | { 98 | int mini = 9999; 99 | int maxi=0; 100 | for(int i=0;i(maxi-mini)) 106 | ans = maxi-mini; 107 | } 108 | void findAns(int end,int curr,int oil[],int company[],int m,int n,int num) 109 | { 110 | if((curr+1)%m==end) 111 | { 112 | for(int j = num;j>t; 131 | while(t--) 132 | { 133 | ans=9999; 134 | int n,m; 135 | cin>>n>>m; 136 | int oil[m]; 137 | for(int i=0;i>oil[i]; 139 | int company[n]; 140 | for(int i=0;i 2 operations. (Each touch is considered an operation),br> If you have to type 5 -> '1+4=' that requires 4 operations. There could be other ways to make '5'. 62 | */ 63 | 64 | #include 65 | #include 66 | using namespace std; 67 | int *working,*operations; 68 | int answer=INT_MAX; 69 | int n,m,o; 70 | int eval(int prev,int curr,int op){ 71 | if(prev==-10000000){ 72 | return curr; 73 | } 74 | 75 | if(op==1){ 76 | return prev+curr; 77 | } 78 | if(op==2){ 79 | return prev-curr; 80 | } 81 | if(op==3){ 82 | return prev*curr; 83 | } 84 | if(op==4){ 85 | if(curr==0){ 86 | return -1; 87 | }else{ 88 | return prev/curr; 89 | } 90 | } 91 | } 92 | bool isDone(int prev,int curr,int Operation,int target){ 93 | if(Operation==4 && curr==0){ 94 | return false; 95 | } 96 | 97 | if(eval(prev,curr,Operation)==target) 98 | return 1; 99 | return false; 100 | } 101 | void findMinTouch(int prev,int curr,int ooperation,int tou,int t) 102 | { if(ooperation!=-1 && curr!=-10000000) 103 | { 104 | bool k=isDone(prev,curr,ooperation,t); 105 | if(k && toutou+1) 108 | answer=tou+1; 109 | } 110 | } 111 | if(prev==t && touo) return ; 121 | 122 | for(int i=0;i>t; 150 | int count = 0; 151 | while(t--){ 152 | answer=INT_MAX; 153 | cin>>n>>m>>o; 154 | working=new int[n + 2]; 155 | for(int i=0;i>working[i]; 157 | } 158 | operations=new int[m + 2]; 159 | for(int i=0;i>operations[i]; 161 | } 162 | 163 | int target; 164 | cin>>target; 165 | 166 | findMinTouch(-10000000,-10000000,-1,0,target); 167 | count++; 168 | cout<<"#" << count << ": " << answer< 31 | #include 32 | #define MAX 21 33 | using namespace std; 34 | 35 | int n, ans; 36 | 37 | bool isValid(int i, int j){ 38 | return (i>=0 && i=0 && j= ans){ 55 | ans = value; 56 | 57 | for(int i=0; i> t; 96 | while(t--){ 97 | cin >> n; 98 | int **maze = new int * [n + 1]; 99 | for(int i=0; i> maze[i][j]; 117 | visited[i][j] = 0; 118 | path[i][j] = 0; 119 | } 120 | } 121 | 122 | ans = INT_MIN; 123 | 124 | int sX = 0, sY = 0; 125 | visited[sX][sY] = 1; 126 | 127 | // printMatrix(maze); 128 | 129 | if(maze[sX][sY] == 2) 130 | jewelMaze(maze, sX, sY, 1, visited, path); 131 | else 132 | jewelMaze(maze, sX, sY, 0, visited, path); 133 | 134 | 135 | cout << "Jewel collected : " << ans << endl; 136 | 137 | cout << "Path traversed --> " << endl; 138 | printMatrix(path); 139 | 140 | cout << endl; 141 | } 142 | return 0; 143 | } -------------------------------------------------------------------------------- /wormhole DFS.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | https://www.careercup.com/question?id=5677905146281984 3 | https://github.com/arjunsk/samsung_interview/blob/master/wormhole.cpp 4 | https://gist.github.com/gunpreet34/b58c38b3556271059182244676ba06a1 5 | https://hack.codingblocks.com/contests/c/782/870 6 | https://discuss.codingblocks.com/t/test-case-of-minimum-time-traversal-problem/12944 7 | 8 | http://ideone.com/Sbx7MA 9 | */ 10 | 11 | /* 12 | There is one spaceship. X and Y co-odinate of source of spaceship and destination spaceship is given. 13 | There are N number of warmholes; each warmhole has 5 values. First 2 values are starting co-ordinate 14 | of warmhole and after that value no. 3 and 4 represents ending co-ordinate of warmhole and last 5th 15 | value is represents cost to pass through this warmhole. Now these warmholes are bi-directional. Now 16 | the to go from (x1,y1) to (x2,y2) is abs(x1-x2)+abs(y1-y2). The main problem here is to find minimum 17 | distance to reach spaceship from source to destination co-ordinate using any number of warm-hole. 18 | It is ok if you wont use any warmhole. 19 | */ 20 | #include 21 | #include 22 | using namespace std; 23 | 24 | int ANS = INT_MAX, n, temp = 0; 25 | int w[35][5]; 26 | int mask[35]; 27 | 28 | int abs(int i){ 29 | return (i>=0) ? i : -1*i; 30 | } 31 | 32 | int min(int x, int y){ 33 | return (x>=y) ? y : x; 34 | } 35 | 36 | int dist(int sX, int sY, int tX, int tY){ 37 | return abs(sX-tX) + abs(sY-tY); 38 | } 39 | 40 | void wormhole(int sX, int sY, int tX, int tY, int value){ 41 | ANS = min(ANS, dist(sX, sY, tX, tY) + value); 42 | 43 | for(int i=0; i> t; 63 | while(t--){ 64 | ANS = INT_MAX; 65 | cin >> n; 66 | cin >> sX >> sY >> tX >> tY; 67 | for(int i=0; i> w[i][j]; 71 | } 72 | } 73 | 74 | wormhole(sX, sY, tX, tY, 0); 75 | cout << ANS << endl; 76 | } 77 | return 0; 78 | } 79 | 80 | // #include 81 | // using namespace std; 82 | 83 | // int n, a[2001][6]; 84 | 85 | // int min(int x , int y){ 86 | // return(x>=y) ? y : x ; 87 | // } 88 | 89 | // int abs(int x){ 90 | // return (x > 0) ? x : -x ; 91 | // } 92 | 93 | // int dist(int x1 , int y1 , int x2 , int y2){ 94 | // return abs(x2-x1) + abs(y2 - y1); 95 | // } 96 | 97 | // void wormhole ( int x1 , int y1 , bool *visited , int &ans , int val ) { 98 | // if ( x1 == a[n+1][0] && y1 == a[n+1][1] ){ 99 | // ans = min ( ans , val); 100 | // return ; 101 | // } 102 | 103 | // for ( int i = 1 ; i <= n + 1 ;i++) { 104 | // if (!visited[i]) { 105 | // visited[i] = true ; 106 | 107 | // //entry 108 | // wormhole ( a[i][2] , a[i][3] , visited , ans , val + dist ( x1 , y1 , a[i][0] , a[i][1] ) + a[i][4]); 109 | 110 | // //exit 111 | // wormhole ( a[i][0] , a[i][1] , visited , ans , val + dist ( x1 , y1 , a[i][2] , a[i][3] ) + a[i][4]); 112 | 113 | // visited[i] = false; 114 | // } 115 | // } 116 | // } 117 | 118 | // int main(){ 119 | // int t; cin >> t ; 120 | // for (int i = 1; i <= t ; i++){ 121 | // cin >> n; 122 | // int sx, sy, dx,dy; 123 | // cin>>sx>>sy>>dx>>dy; 124 | 125 | // a[0][0] = sx ; a[0][1] = sy ; a[0][2] = sx ; a[0][3] = sy ; a[0][4] = 0 ; 126 | 127 | // for ( int i = 1 ; i <= n ;i++ ){ 128 | // cin >> a[i][0] >> a[i][1] >> a[i][2] >> a[i][3] >> a[i][4]; 129 | // } 130 | 131 | // a[n+1][0] = dx ; a[n+1][1] = dy ; a[n+1][2] = dx ; a[n+1][3] = dy ; a[n+1][4] = 0 ; 132 | 133 | // int ans ; 134 | // bool visited[n+2] = { false }; 135 | 136 | // ans = dist (a[0][0] , a[0][1] , a[n+1][0] , a[n+1][1]); 137 | // visited[0] = true ; 138 | 139 | // wormhole(sx ,sy , visited , ans , 0); 140 | 141 | // cout << "#" << i << " : " << ans << endl; 142 | // } 143 | // return 0; 144 | // } -------------------------------------------------------------------------------- /crow pot.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | https://www.careercup.com/question?id=5166688897073152 3 | http://forums.codeguru.com/showthread.php?560529-Core-Java-programming 4 | https://codereview.stackexchange.com/questions/136165/the-thirsty-crow 5 | */ 6 | 7 | /* 8 | There are N pots. Every pots has some water in it. They may be partially filled. 9 | Every pot is associated with overflow number O which tell how many minimum no. of stones required 10 | for that pot to overflow. The crow know O1 to On(overflow no. for all the pots). Crow wants some K 11 | pots to be overflow. So the task is minimum number of stones he can make K pots overflow in worst case. 12 | 13 | Array of overflow no--. {1,...,On} 14 | Number of pots--n 15 | No of pots to overflow-- k 16 | 17 | Let say two pots are there with overflow no.s {5,58}, and crow has to overflow one pot(k=1). 18 | So crow will put 5 stones in pot with overflow no.(58), it will not overflow, then he will put in 19 | pot with overflow no.(5), hence the total no. of stones to make overflow one pot is=10. 20 | */ 21 | #include 22 | #include 23 | using namespace std; 24 | 25 | int n, k; 26 | 27 | void merger(int *overflow_numbers, int l, int m, int r){ 28 | int i,j,k; 29 | int n1=m-l+1; 30 | int n2=r-m; 31 | int L[n1], R[n2]; 32 | for(int i=0; i0; i--){ 104 | overflow_numbers[i] = max(0,overflow_numbers[i]-overflow_numbers[i-1]); 105 | } 106 | 107 | for(int i=0; i> n; 115 | int *arr = new int[n + 1]; 116 | for(int i=0;i>arr[i]; 118 | } 119 | cin >> k; 120 | 121 | merge_sort(arr, 0, n-1); 122 | 123 | cout << minCrowPotStoneSecond(arr); 124 | return 0; 125 | } 126 | 127 | /* 128 | #include 129 | using namespace std; 130 | long long dp[1005][1005]; 131 | 132 | long long solve(int arr[], int n, int p) 133 | { 134 | long long ans = INT_MAX; 135 | for(int i=1;i<=n;i++) 136 | { 137 | for(int j=1;j<=p;j++) 138 | { 139 | dp[i][j] = INT_MAX; 140 | } 141 | } 142 | for(int i=n;i>0;i--) 143 | { 144 | for(int z=1; z<=p;z++) 145 | { 146 | if(z==1) 147 | { 148 | dp[i][z] = (n-i+1)*arr[i]; 149 | continue; 150 | } 151 | for(int k = i+1;k<=n;k++) 152 | { 153 | dp[i][z] = min(dp[i][z], dp[k][z-1] + arr[i]*(k-i)); 154 | } 155 | } 156 | } 157 | for(int i=1;i<=n;i++) 158 | { 159 | ans = min(ans, dp[i][p]); 160 | } 161 | return ans; 162 | } 163 | 164 | int main() 165 | { 166 | int t; 167 | cin>>t; 168 | while(t--) 169 | { 170 | int n; 171 | cin>>n; 172 | int k; 173 | cin>>k; 174 | int over[n+1]; 175 | for(int i=1;i<=n;i++) 176 | { 177 | cin>>over[i]; 178 | } 179 | sort(over+1, over+n+1); 180 | cout << solve(over, n, k)< 23 | 24 | using namespace std; 25 | int grid[100][100]; 26 | int temp[100][100]; 27 | int maxi=-1; 28 | 29 | /* restores original given matrix*/ 30 | void restore(int grid[][100],int r,int c){ 31 | 32 | for(int i=0;i= r - index; i++){ 83 | data[index] = arr[i]; 84 | combinationUtil(arr, data, i+1, end, index+1, r,row,col); 85 | } 86 | } 87 | 88 | 89 | void printCombination(int arr[], int n, int r,int row,int col) { 90 | int data[r]; 91 | combinationUtil(arr, data, 0, n-1, 0, r,row,col); 92 | } 93 | 94 | int main(){ 95 | int t=0; 96 | cin>>t; 97 | int testcase=0; 98 | while(t--){ 99 | testcase++; 100 | 101 | int c,r=0; 102 | cin>>r>>c; 103 | int s=0; 104 | cin>>s; 105 | int arr[c]; 106 | for(int j=0;j>grid[i][j]; 113 | temp[i][j]=grid[i][j]; 114 | } 115 | } 116 | 117 | maxi=caleng(temp,r,c); // if initial energy is max so this is corner case ...... 118 | 119 | if(s%2==0&&s>=c){ 120 | for(int i=2;i<=c;i=i+2){ 121 | printCombination(arr, c, i,r,c); 122 | restore(grid,r,c); 123 | } 124 | } 125 | 126 | // if flips are even and flips are greater than colums then check for all columns.... 127 | if(s%2!=0&&s>=c){ 128 | for(int i=1;i<=c;i=i+2){ 129 | printCombination(arr, c, i,r,c); 130 | restore(grid,r,c); 131 | } 132 | } 133 | 134 | // if flips are odd and flips are greater than colums then check for all columns.... 135 | if(s%2==0&&s 162 | 163 | */ -------------------------------------------------------------------------------- /endoscopy.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | https://www.hackerearth.com/problem/algorithm/question-7-4 3 | */ 4 | 5 | #include 6 | using namespace std; 7 | 8 | struct Node{ 9 | bool left, right, up, down; 10 | }; 11 | 12 | struct Point{ 13 | int x, y; 14 | }; 15 | 16 | int n, m, sX, sY, len; 17 | int arr[1005][1005]; 18 | int vis[1005][1005], dis[1005][1005]; 19 | 20 | int result; 21 | Node pipes[1005][1005]; 22 | Point queue[1000005]; 23 | 24 | bool isValid(int i, int j){ 25 | return (i>=0 && i=0 && j> t; 111 | while(t--){ 112 | result = 1; 113 | cin >> n >> m >> sX >> sY >> len; 114 | 115 | for(int i=0; i> arr[i][j]; 118 | 119 | if( arr[i][j] == 1 ){ 120 | pipes[i][j].left = true; 121 | pipes[i][j].right = true; 122 | pipes[i][j].up = true; 123 | pipes[i][j].down = true; 124 | } 125 | else if( arr[i][j] == 2 ){ 126 | pipes[i][j].left = false; 127 | pipes[i][j].right = false; 128 | pipes[i][j].up = true; 129 | pipes[i][j].down = true; 130 | } 131 | else if( arr[i][j] == 3 ){ 132 | pipes[i][j].left = true; 133 | pipes[i][j].right = true; 134 | pipes[i][j].up = false; 135 | pipes[i][j].down = false; 136 | } 137 | else if( arr[i][j] == 4 ){ 138 | pipes[i][j].left = false; 139 | pipes[i][j].right = true; 140 | pipes[i][j].up = true; 141 | pipes[i][j].down = false; 142 | } 143 | else if( arr[i][j] == 5 ){ 144 | pipes[i][j].left = false; 145 | pipes[i][j].right = true; 146 | pipes[i][j].up = false; 147 | pipes[i][j].down = true; 148 | } 149 | else if( arr[i][j] == 6 ){ 150 | pipes[i][j].left = true; 151 | pipes[i][j].right = false; 152 | pipes[i][j].up = false; 153 | pipes[i][j].down = true; 154 | } 155 | else if( arr[i][j] == 7 ){ 156 | pipes[i][j].left = true; 157 | pipes[i][j].right = false; 158 | pipes[i][j].up = true; 159 | pipes[i][j].down = false; 160 | } 161 | else{ 162 | pipes[i][j].left = false; 163 | pipes[i][j].right = false; 164 | pipes[i][j].up = false; 165 | pipes[i][j].down = false; 166 | } 167 | 168 | } 169 | } 170 | 171 | bfs(); 172 | cout << result << "\n"; 173 | } 174 | return 0; 175 | } -------------------------------------------------------------------------------- /mr kim.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | https://www.careercup.com/question?id=5745468609921024 3 | https://gist.github.com/gunpreet34/d0e45eedd61dadbf42fe6540da98facf 4 | https://ideone.com/SlO2P5 - BitMasking Solution 5 | http://ideone.com/tdNRtQ - TARGET_SAMSUNG 6 | */ 7 | 8 | /* 9 | Mr. Kim has to deliver refrigerators to N customers. From the office, he is going to visit all the customers and then return to his home. 10 | Each location of the office, his home, and the customers is given in the form of integer coordinates (x,y) (0≤x≤100, 0≤y≤100) . 11 | The distance between two arbitrary locations (x1, y1) and (x2, y2) is computed by |x1-x2| + |y1-y2|, where |x| denotes the absolute value 12 | of x; for instance, |3|=|-3|=3. The locations of the office, his home, and the customers are all distinct. You should plan an optimal way 13 | to visit all the N customers and return to his among all the possibilities. 14 | You are given the locations of the office, Mr. Kim’s home, and the customers; the number of the customers is in the range of 5 to 10. 15 | Write a program that, starting at the office, finds a (the) shortest path visiting all the customers and returning to his home. 16 | Your program only have to report the distance of a (the) shortest path. 17 | 18 | Constraints 19 | 20 | 5≤N≤10. Each location (x,y) is in a bounded grid, 0≤x≤100, 0≤y≤100, and x, y are integers. 21 | 22 | Input 23 | 24 | You are given 10 test cases. Each test case consists of two lines; the first line has N, the number of the customers, and the 25 | following line enumerates the locations of the office, Mr. Kim’s home, and the customers in sequence. Each location consists of 26 | the coordinates (x,y), which is reprensented by ‘x y’. 27 | 28 | Output 29 | 30 | Output the 10 answers in 10 lines. Each line outputs the distance of a (the) shortest path. Each line looks like ‘#x answer’ 31 | where x is the index of a test case. ‘#x’ and ‘answer’ are separated by a space. 32 | 33 | I/O Example 34 | 35 | Input (20 lines in total. In the first test case, the locations of the office and the home are (0, 0) and (100, 100) respectively, 36 | and the locations of the customers are (70, 40), (30, 10), (10, 5), (90, 70), (50, 20).) 37 | 38 | 5 ← Starting test case #1 39 | 40 | 0 0 100 100 70 40 30 10 10 5 90 70 50 20 41 | 42 | 6 ← Starting test case #2 43 | 44 | 88 81 85 80 19 22 31 15 27 29 30 10 20 26 5 14 45 | 46 | 10 ← Starting test case #3 47 | 48 | 39 9 97 61 35 93 62 64 96 39 36 36 9 59 59 96 61 7 64 43 43 58 1 36 49 | 50 | Output (10 lines in total) 51 | 52 | #1 200 53 | 54 | #2 304 55 | 56 | #3 366 57 | */ 58 | #include 59 | #include 60 | using namespace std; 61 | int x[20],y[20],n,ans; 62 | 63 | int abs(int i){//Absolute function 64 | if(i>0){ 65 | return i; 66 | } 67 | return -i; 68 | } 69 | 70 | int dist(int i, int j){//Calc dist between 2 points 71 | int x1 = x[i], x2 = x[j]; 72 | int y1 = y[i], y2 = y[j]; 73 | 74 | return (abs(x1-x2) + abs(y1-y2)); 75 | } 76 | 77 | void optimalPath(int x,bool visited[],int nodes,int value){ 78 | if(n == nodes){//If number of nodes equal n then set value of answer 79 | ans = min(ans,value + dist(x,n+1)); 80 | } 81 | for(int i=1;i<=n;i++){ 82 | if(!visited[i]){ 83 | visited[i] = true; 84 | optimalPath(i,visited,nodes+1,value + dist(x,i));//Dfs call 85 | visited[i] = false; 86 | } 87 | } 88 | } 89 | 90 | int main(){ 91 | int tCases; 92 | cin >> tCases;//For testcases 93 | for(int i=0;i> n; 96 | cin >> x[n+1] >> y[n+1] >> x[0] >> y[0];//Input destination and source x,y coordinates 97 | for(int i=1;i<=n;i++){//Input drop off location coordinates 98 | cin >> x[i] >> y[i]; 99 | } 100 | bool visited[n+2]={false}; 101 | optimalPath(0,visited,0,0); 102 | cout << "#" << i+1 << " " << ans << endl; 103 | } 104 | return 0; 105 | } 106 | 107 | /* 108 | #include 109 | #include 110 | using namespace std; 111 | 112 | bool marked[105][105]; 113 | 114 | struct point { 115 | int x; 116 | int y; 117 | }; 118 | 119 | int absDiff(point i, point j) 120 | { 121 | int xd = (i.x > j.x) ? i.x - j.x : j.x - i.x ; 122 | int yd = (i.y > j.y) ? i.y - j.y : j.y - i.y ; 123 | return xd + yd ; 124 | } 125 | 126 | int solve(point a, point b, point arr[], point &end, int nodes) 127 | { 128 | int curDist = absDiff(a, b); 129 | marked[b.x][b.y] = 1; 130 | int dist = 0, minDist = 1000007 ; 131 | for(int i = 0; i < nodes; i++){ 132 | if(marked[arr[i].x][arr[i].y] == 0){ 133 | dist = solve(b, arr[i], arr, end, nodes); 134 | if(dist < minDist) 135 | minDist = dist ; 136 | } 137 | } 138 | marked[b.x][b.y] = 0; 139 | if(minDist == 1000007){ 140 | minDist = absDiff(b, end); 141 | } 142 | 143 | return curDist + minDist ; 144 | } 145 | 146 | int main() 147 | { 148 | int test; 149 | cin >> test ; 150 | for(int l = 1; l <= test; l++){ 151 | 152 | int nodes; 153 | cin >> nodes ; 154 | point start, end; 155 | point arr[nodes + 3]; 156 | 157 | cin >> start.x >> start.y ; 158 | cin >> end.x >> end.y ; 159 | 160 | for(int u = 0; u < nodes; u++){ 161 | cin >> arr[u].x >> arr[u].y ; 162 | } 163 | 164 | int dist = 0, minDist = 100007 ; 165 | for(int i = 0; i < nodes; i++){ 166 | memset(marked, 0, sizeof(bool) * (nodes + 2) * (nodes + 2)); 167 | dist = solve(start, arr[i], arr, end, nodes); 168 | if(dist < minDist) 169 | minDist = dist ; 170 | } 171 | 172 | cout << "#" << l << " " << minDist << endl ; 173 | } 174 | return 0; 175 | } 176 | */ -------------------------------------------------------------------------------- /fishermen.cpp: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | using namespace std; 4 | #define MAX 3 5 | 6 | int fishspot[100]; // fishing spots 7 | int gate[MAX]; // position of gates 8 | int fishermen[MAX]; // no of fishermen at each gate 9 | int N; // total no of fishing spots 10 | int visited[MAX]; // occupied fishing spots 11 | int Answer; // result 12 | 13 | // To unmark spots occupied by fishermen of gate# index 14 | class GFG 15 | { 16 | public : 17 | void reset_fishspot(int index) 18 | { 19 | int i; 20 | for (i = 1; i <= N; i++) 21 | if (fishspot[i] == index + 1) 22 | fishspot[i] = 0; 23 | } 24 | 25 | // Calculate minimum distance while 26 | // allotting spots to fishermen of gate# index. 27 | // Returns number of positions possible 28 | // with minimum disance. 29 | // pos1, pos2 is used to return positions 30 | int calculate_distance(int index, int*pos1, 31 | int *pos2, int *score) 32 | { 33 | int i, sum = 0, left_min = 999999, 34 | right_min = 999999, 35 | left, right, npos = 0; 36 | *pos1 = *pos2 = *score = 0; 37 | 38 | left = right = gate[index]; 39 | 40 | // Allot spots to all fishermen except 41 | // last based on minimum distance 42 | for (i = 1; i < fishermen[index]; i++) 43 | { 44 | if (fishspot[gate[index]] == 0) 45 | { 46 | sum++; 47 | fishspot[gate[index]] = index + 1; 48 | } 49 | else 50 | { 51 | left_min = right_min = 999999; 52 | 53 | while ((left > 0) && (fishspot[left] > 0)) 54 | left--; 55 | 56 | while ((right <= N) && 57 | (fishspot[right] > 0)) 58 | right++; 59 | 60 | if ((left > 0) && (fishspot[left] == 0)) 61 | left_min = gate[index] - left + 1; 62 | 63 | if ((right <= N) && (fishspot[right] == 0)) 64 | right_min = right - gate[index] + 1; 65 | 66 | if (right_min == left_min) 67 | { 68 | // Place 2 fishermen, if avaiable 69 | if ((fishermen[index] - i - 1) > 1) 70 | { 71 | fishspot[left] = fishspot[right] = index + 1; 72 | sum += (2 * left_min); 73 | i++; 74 | 75 | // If all fishermen are alloted spots 76 | if (i == fishermen[index]) 77 | { 78 | npos = 1; 79 | *score = sum; 80 | return npos; 81 | } 82 | } 83 | else 84 | { 85 | sum += left_min; 86 | fishspot[left] = index + 1; 87 | } 88 | } 89 | else if (left_min < right_min) 90 | { 91 | sum += left_min; 92 | fishspot[left] = index + 1; 93 | } 94 | else if (right_min < left_min) 95 | { 96 | sum += right_min; 97 | fishspot[right] = index + 1; 98 | } 99 | } 100 | } 101 | 102 | left_min = right_min = 99999; 103 | 104 | // Allot spot to last fishermen 105 | while ((left > 0) && (fishspot[left] > 0)) 106 | left--; 107 | 108 | if ((left > 0) && (fishspot[left] == 0)) 109 | left_min = gate[index] - left + 1; 110 | 111 | while ((right <= N) && (fishspot[right] > 0)) 112 | right++; 113 | 114 | if ((right <= N) && (fishspot[right] == 0)) 115 | right_min = right - gate[index] + 1; 116 | 117 | if ((sum + left_min) == (sum + right_min)) 118 | { 119 | npos = 2; 120 | *pos1 = left; 121 | *pos2 = right; 122 | *score = sum + left_min; 123 | } 124 | else if ((sum + left_min) > 125 | (sum + right_min)) 126 | { 127 | npos = 1; 128 | *score = sum + right_min; 129 | fishspot[right] = index + 1; 130 | } 131 | else if ((sum + left_min) < 132 | (sum + right_min)) 133 | { 134 | npos = 1; 135 | *score = sum + left_min; 136 | fishspot[left] = index + 1; 137 | } 138 | return npos; 139 | } 140 | 141 | // Solve is used to select next gate 142 | // and generate all combinations. 143 | void solve(int index, int sum, int count) 144 | { 145 | int npos, pos1, pos2, score, i; 146 | 147 | visited[index] = 1; 148 | 149 | if (sum > Answer) 150 | return; 151 | 152 | npos = calculate_distance(index, &pos1, 153 | &pos2, &score); 154 | sum += score; 155 | 156 | if (count == MAX) 157 | { 158 | if (Answer > sum) 159 | Answer = sum; 160 | 161 | return; 162 | } 163 | 164 | if (npos == 1) 165 | { 166 | for (i = 0; i < MAX; i++) 167 | { 168 | if (visited[i] == 0) 169 | { 170 | solve(i, sum, count + 1); 171 | visited[i] = 0; 172 | reset_fishspot(i); 173 | } 174 | } 175 | } 176 | 177 | else if (npos == 2) 178 | { 179 | fishspot[pos1] = index + 1; 180 | for (i = 0; i < MAX; i++) 181 | { 182 | if (visited[i] == 0) 183 | { 184 | solve(i, sum, count + 1); 185 | visited[i] = 0; 186 | reset_fishspot(i); 187 | } 188 | } 189 | 190 | fishspot[pos1] = 0; 191 | fishspot[pos2] = index + 1; 192 | for (i = 0; i < MAX; i++) 193 | { 194 | if (visited[i] == 0) 195 | { 196 | solve(i, sum, count + 1); 197 | visited[i] = 0; 198 | reset_fishspot(i); 199 | } 200 | } 201 | fishspot[pos2] = 0; 202 | } 203 | } 204 | }; 205 | 206 | // Driver Code 207 | int main() 208 | { 209 | GFG g; 210 | 211 | int i; 212 | /*scanf("%d", &N); // for input 213 | 214 | for (i = 0; i < MAX; i++) 215 | { 216 | scanf("%d %d", &gate[i], &fishermen[i]); 217 | visited[i] = 0; 218 | }*/ 219 | 220 | N = 10; // total no of fishing spots 221 | 222 | // position of gates(1-based indexing) 223 | gate[0] = 4; 224 | gate[1] = 6; 225 | gate[2] = 10; 226 | 227 | //no of fishermen at each gate 228 | fishermen[0] = 5; //gate1 229 | fishermen[1] = 2; //gate 2 230 | fishermen[2] = 2; //gate 3 231 | 232 | for (i = 1; i <= N; i++) 233 | fishspot[i] = 0; 234 | 235 | Answer = 999999; 236 | 237 | for (i = 0; i < MAX; i++) 238 | { 239 | g.solve(i, 0, 1); 240 | visited[i] = 0; 241 | g.reset_fishspot(i); 242 | } 243 | 244 | cout << Answer << endl; 245 | 246 | return 0; 247 | } 248 | // This code is contributed by SoM15242 249 | -------------------------------------------------------------------------------- /research team.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | https://www.careercup.com/question?id=5707238197952512 3 | https://practice.geeksforgeeks.org/problems/how-to-solve-this-bfs-problem-asked-in-samsung 4 | https://discuss.codechef.com/t/samsung-question-geeksforgeeks/17092 5 | 6 | 1 Bsed -> https://sapphireengine.com/@/4q0evk - Ajay Verma 7 | 0 Bsed -> https://sapphireengine.com/@/iha4kq - Thusoo 8 | */ 9 | 10 | /* 11 | A Research team want to establish a research center in a region where they found some rare-elements. 12 | They want to make it closest to all the rare-elements as close as possible so that they can reduce 13 | overall cost of research over there. It is given that all the rare-element’s location is connected 14 | by roads. It is also given that Research Center can only be build on road. Team decided to assign 15 | this task to a coder. If you feel you have that much potential. 16 | 17 | Here is the Task :- Find the shortest of the longest distance of research center from given locations 18 | of rare-elements. 19 | 20 | Locations are given in the matrix cell form where 1 represents roads and 0 no road. 21 | Number of rare-element and their location was also given(number<=5) and order of square matrix 22 | was less than equal to (20). 23 | */ 24 | 25 | /* 26 | For this you have to implement bfs for every position where road exist to find the distance of 27 | every research center or do Vice-versa. for each position store maximum distance of all distances 28 | to research center and the compare each maximum distance to find minimum of them 29 | 30 | Input - 31 | 6 32 | 5 2 33 | 4 3 34 | 3 4 35 | 1 1 0 0 0 36 | 1 1 0 0 0 37 | 1 1 1 1 1 38 | 1 1 1 0 1 39 | 1 1 1 1 1 40 | 8 2 41 | 5 6 42 | 6 4 43 | 1 1 1 1 1 1 0 0 44 | 1 1 1 1 1 1 1 0 45 | 1 1 0 1 0 1 1 0 46 | 1 1 1 1 0 1 1 0 47 | 1 1 1 1 1 1 1 0 48 | 1 1 1 1 1 1 1 0 49 | 0 0 0 0 0 0 0 0 50 | 0 0 0 0 0 0 0 0 51 | 10 3 52 | 8 2 53 | 5 3 54 | 7 1 55 | 0 0 0 1 1 1 1 1 1 0 56 | 1 1 1 1 1 1 1 1 1 0 57 | 1 0 0 1 0 0 0 0 1 0 58 | 1 1 1 1 1 1 1 1 1 1 59 | 1 1 1 1 0 1 0 0 1 1 60 | 1 1 1 1 0 1 0 0 1 1 61 | 1 1 1 1 0 1 0 0 1 1 62 | 1 1 1 1 1 1 1 1 1 1 63 | 1 1 1 0 0 1 0 0 1 1 64 | 1 1 1 1 1 1 1 1 1 1 65 | 15 4 66 | 11 15 67 | 15 9 68 | 1 2 69 | 14 3 70 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 71 | 1 0 1 1 1 1 1 1 1 1 1 1 1 0 1 72 | 1 0 1 0 0 0 1 0 0 0 0 1 1 0 1 73 | 1 0 1 0 0 0 1 0 0 0 0 1 1 0 1 74 | 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 75 | 1 0 1 0 0 0 1 0 0 0 0 1 1 0 1 76 | 1 0 1 0 0 0 1 1 1 1 1 1 1 1 1 77 | 1 0 1 0 0 0 1 0 0 0 0 1 1 0 1 78 | 1 0 1 0 0 0 1 0 0 0 0 1 1 0 1 79 | 1 0 1 0 0 0 1 0 0 0 0 1 1 0 1 80 | 1 0 1 0 0 0 1 0 0 0 0 1 1 0 1 81 | 1 0 1 0 0 0 1 0 0 0 0 1 1 0 1 82 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 83 | 0 0 1 0 0 0 1 1 1 1 1 1 1 0 1 84 | 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 85 | 20 4 86 | 13 6 87 | 20 4 88 | 1 2 89 | 17 16 90 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 91 | 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 92 | 1 0 1 0 0 0 0 0 0 0 1 0 0 1 1 0 0 0 0 0 93 | 1 0 1 0 0 0 0 0 0 0 1 0 0 1 1 0 0 0 0 0 94 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 95 | 1 0 1 0 0 0 0 0 0 0 1 0 0 1 1 1 0 0 0 0 96 | 1 0 1 0 0 0 0 0 0 0 1 0 0 1 1 1 0 0 0 0 97 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 98 | 1 0 1 0 0 0 0 0 0 0 1 0 0 1 1 1 0 0 1 1 99 | 1 0 1 0 0 0 0 0 0 0 1 0 0 1 1 1 0 0 1 1 100 | 1 0 1 0 0 0 0 0 0 0 1 0 0 1 1 1 0 0 1 1 101 | 1 0 1 0 0 0 0 0 0 0 1 0 0 1 1 1 0 0 1 1 102 | 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 1 1 103 | 1 0 1 0 0 0 0 0 0 0 1 0 0 0 1 1 0 0 1 1 104 | 1 0 1 0 0 0 0 0 0 0 1 0 0 0 1 1 0 0 1 1 105 | 1 0 1 0 0 0 0 0 0 0 1 0 0 0 1 1 0 0 1 1 106 | 1 0 1 0 0 0 0 0 0 0 1 0 0 0 1 1 0 0 1 1 107 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 108 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 109 | 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 110 | 5 2 111 | 2 1 112 | 3 5 113 | 1 0 1 1 1 114 | 1 1 1 0 1 115 | 0 1 1 0 1 116 | 0 1 0 1 1 117 | 1 1 1 0 1 118 | 119 | Output - 120 | 1 121 | 2 122 | 2 123 | 12 124 | 15 125 | 4 126 | */ 127 | 128 | #include 129 | int Answer = 9999; 130 | int region[22][22]; 131 | int visited[22][22]; 132 | int N, C; 133 | int location[5][2]; 134 | int rear = -1; 135 | int front = -1; 136 | struct queue { 137 | int row; 138 | int col; 139 | }Q[10000]; 140 | 141 | void init() 142 | { 143 | int m,n; 144 | rear = -1; 145 | front = -1; 146 | for(m = 0; m < 22; m++) 147 | { 148 | for(n = 0; n < 22; n++) 149 | { 150 | visited[m][n] = 0; 151 | } 152 | } 153 | 154 | for(m = 0; m < 10000; m++) 155 | { 156 | Q[m].row = 0; 157 | Q[n].col = 0; 158 | } 159 | } 160 | 161 | void discover(int row, int col, int val) 162 | { 163 | int l, m, k; 164 | int cnt = 0; 165 | 166 | for(k = 0; k < C; k++) 167 | { 168 | if(visited[location[k][0]][location[k][1]] > 0) 169 | cnt++; 170 | } 171 | if(cnt >= C) 172 | return; 173 | 174 | if((row-1) >= 1 && visited[row-1][col] == 0 && (region[row-1][col] == 1 || region[row-1][col] == 3)) 175 | { 176 | visited[row-1][col] = val+1; 177 | ++rear; 178 | Q[rear].row = row-1; 179 | Q[rear].col = col; 180 | } 181 | if((row+1) <= N && visited[row+1][col] == 0 && (region[row+1][col] == 1 || region[row+1][col] == 3)) 182 | { 183 | visited[row+1][col] = val+1; 184 | ++rear; 185 | Q[rear].row = row+1; 186 | Q[rear].col = col; 187 | } 188 | if((col-1) >= 1 && visited[row][col-1] == 0 && (region[row][col-1] == 1 || region[row][col-1] == 3)) 189 | { 190 | visited[row][col-1] = val+1; 191 | ++rear; 192 | Q[rear].row = row; 193 | Q[rear].col = col-1; 194 | } 195 | if((col+1) <= N && visited[row][col+1] == 0 && (region[row][col+1] == 1 || region[row][col+1] == 3)) 196 | { 197 | visited[row][col+1] = val+1; 198 | ++rear; 199 | Q[rear].row = row; 200 | Q[rear].col = col+1; 201 | } 202 | 203 | while(front temp) 262 | Answer = temp; 263 | } 264 | 265 | } 266 | } 267 | printf("#%d %d\n", test_case+1, Answer-1); 268 | } 269 | return 0; 270 | } 271 | 272 | -------------------------------------------------------------------------------- /spaceship bomb.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | https://www.careercup.com/question?id=5652067409461248 3 | https://www.geeksforgeeks.org/samsung-interview-experience-set-28-campus/ 4 | 5 | http://ideone.com/JXMl4L 6 | https://ide.geeksforgeeks.org/tiyLThcuSN -> Zero 7 | https://ide.geeksforgeeks.org/3Ks1tpOkwn 8 | 9 | *https://code.hackerearth.com/ea07cfD?key=1cb190b158c79639d66d35f7dfa8ef7a -> One 10 | 11 | Similr Problem - https://ide.codingblocks.com/s/95965 12 | 13 | 14 | You’ll be given a grid as below: 15 | 16 | 0 1 0 2 0 17 | 0 2 2 2 1 18 | 0 2 1 1 1 19 | 1 0 1 0 0 20 | 0 0 1 2 2 21 | 1 1 0 0 1 22 | x x S x x 23 | 24 | In the grid above, 25 | 1: This cell has a coin. 26 | 2: This cell has an enemy. 27 | 0: It contains nothing. 28 | 29 | The highlighted(yellow) zone is the control zone. S is a spaceship that we need to control so that we can get 30 | maximum coins. 31 | Now, S’s initial position will be at the center and we can only move it right or left by one cell or do not move. 32 | At each time, the non-highlighted part of the grid will move down by one unit. 33 | We can also use a bomb but only once. If we use that, all the enemies in the 5×5 region above the control zone 34 | will be killed. 35 | If we use a bomb at the very beginning, the grid will look like this: 36 | 37 | 0 1 0 2 0 38 | 0 0 0 0 1 39 | 0 0 1 1 1 40 | 1 0 1 0 0 41 | 0 0 1 0 0 42 | 1 1 0 0 1 43 | x x S x x 44 | 45 | As soon as, the spaceship encounters an enemy or the entire grid has come down, the game ends. 46 | For example, 47 | At the very first instance, if we want to collect a coin we should move left( coins=1). This is because when the 48 | grid comes down by 1 unit we have a coin on the second position and by moving left we can collect that coin. 49 | Next, we should move right to collect another coin (coins=2). 50 | After this, remain at the same position (coins=4). 51 | This is the current situation after collecting 4 coins. 52 | 53 | 0 1 0 2 0 0 1 0 0 0 54 | 0 2 2 2 1 -->after using 0 0 0 0 1 55 | x x S x x -->bomb x x S x x 56 | 57 | Now, we can use the bomb to get out of this situation. After this, we can collect at most 1 coin. So maximum coins=5. 58 | */ 59 | #include 60 | using namespace std; 61 | void updateMatrix(int row,char ** matrix){ 62 | if(row<0){ 63 | return; 64 | } 65 | int upLimit=max(0,row-4); 66 | for(int i=row;i>=upLimit;i--){ 67 | for(int j=0;j<=4;j++){ 68 | if(matrix[i][j]=='2'){ 69 | matrix[i][j]='0'; 70 | } 71 | } 72 | } 73 | } 74 | int findMaxPoints(int row,int col,int bombs,char ** matrix){ 75 | if(row<=0 || col<0 || col>=5){ 76 | return 0; 77 | } 78 | int answer=0; 79 | if(row>0 && matrix[row-1][col]!='2'){ 80 | answer=max(answer,(matrix[row-1][col]=='1'?1:0)+findMaxPoints(row-1,col,bombs,matrix)); 81 | } 82 | if(col>0 && row>0 && matrix[row-1][col-1]!='2'){ 83 | answer=max(answer,(matrix[row-1][col-1]=='1'?1:0)+findMaxPoints(row-1,col-1,bombs,matrix)); 84 | } 85 | if(col<4 && row>0 && matrix[row-1][col+1]!='2'){ 86 | answer=max(answer,(matrix[row-1][col+1]=='1'?1:0)+findMaxPoints(row-1,col+1,bombs,matrix)); 87 | } 88 | 89 | if(answer==0 && bombs>0){ 90 | updateMatrix(row-1,matrix); 91 | answer=findMaxPoints(row,col,bombs-1,matrix); 92 | } 93 | 94 | return answer; 95 | } 96 | int main(){ 97 | int t, row; 98 | cin >> t; 99 | int tNo = 0; 100 | while(t--){ 101 | cin >> row; 102 | char ** matrix=new char*[row + 2]; 103 | for(int i=0; i>matrix[i][j]; 107 | } 108 | } 109 | tNo++; 110 | cout<< "#" << tNo << " : " << findMaxPoints(row,2,1,matrix) << endl; 111 | } 112 | return 0; 113 | } 114 | 115 | /* 116 | No rech top 117 | 118 | #include 119 | using namespace std; 120 | int det[5][5]; 121 | int mat[13][5]; 122 | 123 | void detonate(int r) 124 | { 125 | for(int i=r;i>r-5 && i>=0;i--) 126 | { 127 | for(int j=0;j<5;j++) 128 | { 129 | det[r-i][j]=0; 130 | if(mat[i][j]==2) 131 | { 132 | det[r-i][j]=2; 133 | mat[i][j]=0; 134 | } 135 | } 136 | } 137 | } 138 | 139 | void undet(int r) 140 | { 141 | for(int i=r;i>r-5 && i>=0;i--) 142 | for(int j=0;j<5;j++) 143 | { 144 | if( det[r-i][j]==2) 145 | mat[i][j]=2; 146 | } 147 | } 148 | void func(int n,int pos,int c,int &max) 149 | { 150 | if(pos>4||pos<0||c<0) 151 | return; 152 | 153 | if(mat[n][pos]==2) 154 | c-=1; 155 | else if(mat[n][pos]==1) 156 | c+=1; 157 | 158 | if(n==0) 159 | { 160 | if(c>max) 161 | max=c; 162 | return; 163 | } 164 | else 165 | { 166 | func(n-1,pos+1,c,max); 167 | func(n-1,pos-1,c,max); 168 | func(n-1,pos,c,max); 169 | } 170 | } 171 | int main() 172 | { 173 | int t; 174 | cin>>t; 175 | int count=1; 176 | while(t--) 177 | { 178 | int n; 179 | cin>>n; 180 | for(int i=0;i>mat[i][j]; 183 | int max=-1,c; 184 | for(int j=0;j<5;j++) 185 | mat[n][j]=0; 186 | mat[n][2]=3; 187 | for(int j=n;j>=5;j--) 188 | { 189 | c=-1; 190 | detonate(j-1); 191 | func(n,2,0,c); 192 | if(c>max) 193 | max=c; 194 | undet(j-1); 195 | } 196 | if(max<0) 197 | max=-1; 198 | cout<<"#"< 206 | using namespace std; 207 | int det[5][5]; 208 | int mat[13][5]; 209 | 210 | void detonate(int r){ 211 | for(int i=r;i>r-5 && i>=0;i--){ 212 | for(int j=0;j<5;j++){ 213 | det[r-i][j]=0; 214 | if(mat[i][j]==2){ 215 | det[r-i][j]=2; 216 | mat[i][j]=0; 217 | } 218 | } 219 | } 220 | } 221 | 222 | void undet(int r){ 223 | for(int i=r;i>r-5 && i>=0;i--) 224 | for(int j=0;j<5;j++){ 225 | if( det[r-i][j]==2) 226 | mat[i][j]=2; 227 | } 228 | } 229 | 230 | void func(int n,int pos,int c,int &max){ 231 | if(pos>4||pos<0||c<0) 232 | return; 233 | 234 | if(mat[n][pos]==2) 235 | c-=1; 236 | else if(mat[n][pos]==1) 237 | c+=1; 238 | 239 | if(n==0){ 240 | if(c>max) 241 | max=c; 242 | return; 243 | } 244 | else{ 245 | func(n-1,pos+1,c,max); 246 | func(n-1,pos-1,c,max); 247 | func(n-1,pos,c,max); 248 | } 249 | } 250 | 251 | int main(){ 252 | int t; 253 | cin>>t; 254 | int count=1; 255 | while(t--){ 256 | int n; 257 | cin>>n; 258 | 259 | for(int i=0;i>mat[i][j]; 262 | for(int j=0;j<5;j++) 263 | mat[n][j]=0; 264 | mat[n][2]=3; 265 | 266 | int max=-1,c; 267 | for(int j=n;j>=5;j--){ 268 | c=-1; 269 | detonate(j-1); 270 | func(n,2,0,c); 271 | 272 | if(c>max) 273 | max=c; 274 | 275 | undet(j-1); 276 | } 277 | 278 | if(max<0) 279 | max=-1; 280 | 281 | cout<<"#"< No of CPUs 4 | E --> No of memory chips 5 | F --> No of boards 6 | d --> Selling price of CPU 7 | e --> Selling price of Memory chips 8 | 9 | We are given N Computer configurations like below : 10 | Di, Ei, Fi, SPi, which are the CPU, Chips, Boards and one unit selling price for ith computer respectively. 11 | Our task is to maximize the final cost. 12 | Constraints: 13 | 1. Can use at Max 3 different Configurations 14 | 2. We can use 1 configuration multiple times 15 | 3. Remaining Inventories can be sold on its selling price 16 | 17 | Input: 18 | T --> Number of test cases. 19 | D E F d e --> Inventories 20 | N --> Total Configuration Count 21 | Di Ei Fi SPi 22 | ... 23 | Dn En Fn SPn 24 | 25 | 1<=T<=10 26 | 1<= D, E, F <= 100 27 | 1<= d, e <=100000 28 | 1<=N<=8 29 | 30 | Output: 31 | First Line print the Case #testCaseNumber 32 | Second Line Print Maximum Cost per test case in each line. 33 | 34 | Sample Input: 35 | 1 --> Total Test Case 36 | 10 10 10 2 1 --> D E F d e 37 | 1 --> PC Configuration Count 38 | 1 2 2 3 --> D1 E1 F1 SP1 39 | 40 | Sample Output: 41 | Case #1 42 | 30 43 | 44 | 45 | Solution: 46 | 47 | #include 48 | 49 | using namespace std; 50 | 51 | #define rep(i,a,n) for(int i =a; i < n; i++) 52 | #define repe(i,a,n) for(int i =a; i <= n; i++) 53 | 54 | int D,E,F,d,e; 55 | int config; 56 | int answer = 0; 57 | 58 | struct configuration 59 | { 60 | int D,E,F,SPi; 61 | }; 62 | configuration m[9]; 63 | 64 | void solve(int index, int counta, int D, int E, int F, int cost ) 65 | { 66 | 67 | if(index >= config || counta == 3) 68 | { 69 | cost += D*d + E*e; 70 | if(cost > answer) 71 | answer = cost; 72 | return; 73 | } 74 | solve(index + 1, counta, D,E,F,cost); 75 | 76 | int i = 1; 77 | 78 | while(true) 79 | { 80 | if( D - m[index].D*i >= 0 && E - m[index].E*i >=0 && F - m[index].F*i >= 0 ) 81 | { 82 | solve(index+1,counta+1,D- m[index].D *i,E - m[index].E *i,F- m[index].F*i, cost+ m[index].SPi * i); 83 | ++i; 84 | } 85 | else 86 | { 87 | break; 88 | } 89 | } 90 | return; 91 | 92 | } 93 | 94 | int main() 95 | { 96 | int t; 97 | cin >> t; 98 | repe(_cases,1,t) 99 | { 100 | 101 | answer = 0; 102 | cin >> D >> E >> F >> d >> e; 103 | 104 | cin >> config; 105 | 106 | rep(i,0,config) 107 | { 108 | cin >> m[i].D >> m[i].E >> m[i].F >> m[i].SPi; 109 | } 110 | solve(0,0,D,E,F,0); 111 | cout << "Case #"<<_cases << "\n" << answer <<"\n"; 112 | 113 | } 114 | 115 | return 0; 116 | } 117 | --------------------------------------------------------------------- 118 | 119 | 120 | 121 | You want to cut a piece of paper by a certain fixed rule to make some pieces of white or 122 | blue colored square paper with various sizes. 123 | 124 | If the size of the entire paper is N×N (N = 2^K; 1 <= K <= 7; K = natural number), the cutting rules 125 | are as below. 126 | 127 | ‘If the entire piece of paper is not colored the same, cut the middle part horizontally and vertically 128 | to divide it into the same sized four pieces of paper, 129 | (N/2)×(N/2), as with I, II, III, IV in < FIG. 2 >. 130 | 131 | For each I, II, III and IV, cut and divide again in the same way if one entire piece of paper 132 | is not colored the same, and make them into the same sized four pieces of paper. Continue until each and 133 | every piece of paper has only one color of white or blue.’ 134 | 135 | When you finish, < FIG. 3 > shows the first division of < FIG. 1 > and < FIG. 4 > 136 | shows the final version of 9 pieces of white paper and 7 pieces of blue paper of various sizes. 137 | 138 | If the length of an edge of the first given piece of paper, N, and 139 | the color information (white or blue) inside each square are given, create a calculation program 140 | that assesses how many white/blue pieces of paper are. 141 | 142 | Time limit: 1 second (java: 2 seconds) 143 | 144 | [Input] 145 | 146 | Input may include many test cases. The number of test cases, T, is given on the first line of input and then the amount of T of test cases is given in a line. (T <= 30) 147 | The length of an edge of the first given piece of paper, N, is given for the first line of each test case. 148 | From the next line through to the amount of N lines, the color information is given separately as blanks. 0 indicates white and 1 indicates blue. 149 | 150 | [Output] 151 | 152 | For each test case, you should print "Case #T" in the first line where T means the case number. 153 | 154 | For each test case, you should output the number of white pieces of paper and blue pieces of paper separately as blanks on the first line of each test case. 155 | 156 | [I/O Example] 157 | Input 158 | 2 159 | 8 160 | 1 1 0 0 0 0 1 1 161 | 1 1 0 0 0 0 1 1 162 | 0 0 0 0 1 1 0 0 163 | 0 0 0 0 1 1 0 0 164 | 1 0 0 0 1 1 1 1 165 | 0 1 0 0 1 1 1 1 166 | 0 0 1 1 1 1 1 1 167 | 0 0 1 1 1 1 1 1 168 | 169 | 170 | 16 171 | 1 0 0 1 0 0 0 0 0 0 1 1 0 1 1 1 172 | 1 1 0 1 0 1 1 0 0 0 0 0 0 0 0 0 173 | 0 0 0 0 1 0 1 1 1 1 0 0 1 0 0 1 174 | 1 1 0 0 1 0 0 1 0 0 1 0 1 1 1 0 175 | 0 1 1 1 0 0 1 1 0 0 1 0 0 1 1 1 176 | 1 0 1 1 0 0 0 1 0 1 0 1 0 0 1 1 177 | 1 1 1 1 1 1 0 0 1 1 1 1 1 0 0 0 178 | 1 1 0 1 0 1 0 0 1 0 1 1 1 0 0 1 179 | 1 1 1 1 1 1 0 0 1 0 1 1 0 1 1 0 180 | 1 0 0 1 1 1 0 0 0 0 1 1 1 1 0 0 181 | 1 0 0 1 1 1 1 0 0 0 1 1 0 1 0 1 182 | 1 1 1 0 1 1 0 0 1 1 1 1 1 1 0 1 183 | 1 1 1 1 1 1 0 0 0 0 1 1 1 1 0 0 184 | 1 1 1 1 1 1 0 1 1 1 1 1 1 1 0 0 185 | 1 1 0 0 0 0 0 0 1 1 0 1 1 0 0 0 186 | 1 1 0 0 1 1 0 0 0 1 1 1 1 0 0 0 187 | 188 | 189 | 190 | Output 191 | 192 | Case #1 193 | 9 7 194 | 195 | Case #2 196 | 88 99 197 | 198 | 199 | 200 | Solution : 201 | #include 202 | #include 203 | #include 204 | using namespace std; 205 | #define debug(x) cout << '>' << #x << ':' << x << endl; 206 | const int maxn = 129; 207 | int white = 0, blue = 0; 208 | 209 | bool checkSame(bool arr[maxn][maxn], int sti, int stj, int size) 210 | { 211 | bool color = arr[sti][stj]; 212 | for(int i = sti; i < sti + size; i++){ 213 | for(int j = stj; j < stj + size; j++){ 214 | if(arr[i][j] != color){ 215 | return false; 216 | } 217 | } 218 | } 219 | return true; 220 | } 221 | 222 | void solve(bool arr[maxn][maxn], int size, int sti, int stj) 223 | { 224 | bool same = checkSame(arr, sti, stj, size); 225 | 226 | if(!same){ 227 | solve(arr, size / 2, sti, stj); 228 | solve(arr, size / 2, sti + size/2, stj); 229 | solve(arr, size / 2, sti, stj + size/2); 230 | solve(arr, size / 2, sti + size/2, stj + size/2); 231 | } 232 | else{ 233 | (arr[sti][stj]) ? ++blue : ++white ; 234 | } 235 | } 236 | 237 | int main() 238 | { 239 | int test ; 240 | cin >> test ; 241 | for(int l = 1; l <= test; l++){ 242 | white = 0; 243 | blue = 0; 244 | int size ; 245 | cin >> size; 246 | bool arr[maxn][maxn]; 247 | for(int i = 0; i < size; i++){ 248 | for(int j = 0; j < size; j++){ 249 | cin >> arr[i][j] ; 250 | } 251 | } 252 | solve(arr, size, 0, 0); 253 | cout << "Case #" << l << endl; 254 | cout << white << " " << blue << endl; 255 | } 256 | return 0; 257 | } 258 | // optimized approach using prefix sum of matrix 259 | 260 | #include 261 | using namespace std; 262 | const int maxn = 129; 263 | int white = 0, blue = 0; 264 | void solve(int arr[maxn][maxn], int size, int si,int sj) 265 | { 266 | if( size == 0) 267 | return; 268 | int sum = arr[size + si - 1][size + sj - 1]; 269 | if(sj - 1 >= 0) 270 | sum -= arr[size + si - 1][sj - 1]; 271 | if(si - 1 >= 0) 272 | sum -= arr[si - 1][sj + size - 1]; 273 | if( si - 1 >= 0 && sj - 1 >= 0) 274 | sum += arr[si - 1][sj - 1]; 275 | if(si == 4 && sj == 1) 276 | cout<> test ; 297 | for(int l = 1; l <= test; l++){ 298 | white = 0; 299 | blue = 0; 300 | int size ; 301 | cin >> size; 302 | int arr[maxn][maxn]; 303 | for(int i = 0; i < size; i++){ 304 | for(int j = 0; j < size; j++){ 305 | int a; 306 | cin>>a; 307 | if( i == 0 && j == 0) 308 | arr[i][j] = a; 309 | else if( i == 0) 310 | arr[i][j] = a + arr[i][j - 1]; 311 | else if( j == 0) 312 | arr[i][j] = a + arr[i - 1][j]; 313 | else 314 | arr[i][j] = a + arr[i - 1][j] + arr[i][j - 1] - arr[i - 1][j - 1]; 315 | } 316 | } 317 | solve(arr, size, 0, 0); 318 | cout << "Case #" << l << endl; 319 | cout << white << " " << blue << endl; 320 | } 321 | return 0; 322 | } 323 | --------------------------------------------------------------------------------