Professional Documents
Culture Documents
Samsung
Samsung
Samsung
Q1. 2D MATRIX
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
bool isNoPath = false;
cin>>n;
char arr[n][n];
pair<int,int> dp[n][n];
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
cin>>arr[i][j];
dp[i][j].first = 0;
dp[i][j].second = 0;
}
}
arr[0][0]='0';
arr[n-1][n-1]='0';
Output: 4
1 5 10
*/
#include<bits/stdc++.h>
using namespace std;
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
int currentCows = 1;
int leftmost = 0;
for(int i=1;i<n;++i){
if(a[i] - a[leftmost] >= x){
leftmost = i;
++currentCows;
if(currentCows == k)
return 1;
}
}
return 0;
}
int main()
{
int t; cin >> t;
for(int i=0;i<t;++i){
int n,k;cin >> n >> k;
int a[100000];
for(int j=0;j<n;++j){
cin >> a[j];
}
sort(a,a+n);
int l = 0;
int r = a[n-1] - a[0] + 1;
// If we can do with x distance then obviosult we can do it with <=x.
// So We need to update it
//True True True True True True True . .. . . False False False ==> We want to find the last true
if(Comperator(m,a,n,k)==true){
l = m; // l is true now
}
else
r = m-1; // R is false now
}
#include<iostream>
using namespace std;
int n;
int arr[100][100]={0};
int main(){
cin >> n;//No of nodes in graph
int color[n];//For coloring of graph
for(int i=0;i<n;i++){
color[i] = -1;
for(int j=0;j<n;j++){
cin >> arr[i][j];//Input graph adjacency matrix
}
}
for(int i=0;i<n;i++){
if(color[i]==-1){
color[i] = 0;//Color starting node(of connected/disconnected graph) as 0
if(!isBiPartite(i,color)){//If not bipartite then print -1
cout << "-1" << endl;
return 0;
}
}
}
for(int i=0;i<n;i++){
if(color[i] == 0){
cout << i << " ";//Print all nodes with color 0
}
}
return 0;
}
Input
1234
Output
20
*/
#include <iostream>
using namespace std;
for(int j=2;j<n;j++)
{
for(int left=0;left<n-j;left++)
{
int right = left+j;
for(int i = left+1;i<right;i++)
{
if(left==0 && right==n-1)
dp[left][right] = max(nums[left]*nums[i]*nums[right] + dp[left][i] + dp[i][right],dp[left][right]);
else
dp[left][right] = max(nums[left]*nums[right] + dp[left][i] + dp[i][right],dp[left][right]);
}
}
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
cout<<dp[i][j]<<" ";
}
cout<<endl;
}
}
return dp[0][n-1];
}
int main()
{
int siz;
cin >> siz;
int A[siz];
for(int i=0;i<siz;i++)
cin >> A[i];
/*
5
12354
*/
Find the maximum coins you can collect by bursting the balloons wisely.
Input: [3,1,5,8]
Output: 167
*/
int i, j, k;
vector< vector<int> > dp(size, vector<int>(size, 0));
if(i != 0)
leftValue = nums[i-1];
if(j != size-1)
rightValue = nums[j+1];
/* Before and After - current k balloon is last to burst select the left side and right side to burst */
int before = 0;
int after = 0;
if(i != k)
before = dp[i][k-1];
if(j != k)
after = dp[k+1][j];
dp[i][j] = max(dp[i][j],
leftValue * nums[k] * rightValue + before + after);
}
}
}
return dp[0][size-1];
}
Write a program to find out the minimum number moves to catch a piece.
[Input]
Several test cases can be included in the inputs. T, the number of cases is given in the first row of the
inputs. After that, the test cases as many as T (T ≤ 20) are given in a row.
N, the numbers of the rows and M, the number of columns of the chessboard are given in the first row of
each test case.
R & C is the location information of the attacking piece and S & K is the location of the defending pieces
and are given in the row at the second line. However, the location of the uppermost end of the left end
is (1, 1)
[Output]
For each test case, you should print "Case #T" in the first line where T means the case number.
For each test case, you should output the minimum number of movements to catch a defending piece at the
first line of each test case. If not moveable, output equals ‘-1’.
[I/O Example]
Input
2
99
3528
20 20
2379
Output
Case #1
2
Case #2
5
*/
#include <iostream>
#include <stdio.h>
#include <queue>
#include <string.h>
using namespace std;
typedef struct
{
int x;
int y;
int level;
}data;
int mv[8][2] = {{-2,1},{-1,2},{1,2},{2,1},{2,-1},{1,-2},{-1,-2},{-2,-1}};
int main(){
int T;
cin>>T;
for(int t=1; t<=T; t++)
{
int n,m,r1,c1,r2,c2;
cin>>n>>m;
int a[n+1][m+1];
memset(a,0,sizeof(int)*(n+1)*(m+1));
cin>>r1>>c1>>r2>>c2;
data d,d1,d2;
queue<data> qt;
d.x = r1;
d.y = c1;
d.level = 0;
qt.push(d);
a[d.x][d.y] = 2;
int tmx,tmy,tml;
int steps = 0;
bool f = false;
while(!qt.empty())
{
if(f)
{
break;
}
d1 = qt.front();
qt.pop();
for(int k=0; k<8; k++)
{
tmx = d1.x + mv[k][0];
tmy = d1.y + mv[k][1];
tml = d1.level + 1;
struct Point{
int x, y;
};
if (val == 0) return 0;
return (val > 0)? 1: 2;
}
if(a.x == b.x)
return a.y < b.y;
else
return a.x < b.x;
}
vector<Point> hull;
int l = 0;
for (int i = 1; i < n; i++)
if (points[i].x < points[l].x)
l = i;
int p = l, q;
do{
hull.push_back(points[p]);
q = (p+1)%n;
} while (p != l);
hull.resize(std::distance(hull.begin(), ip));
int main(){
int t, n;
cin >> t;
while(t--){
cin >> n;
Point *points = new Point[n];
convexHull(points, n);
cout << "\n";
}
return 0;
}
/*
There are N pots. Every pots has some water in it. They may be partially filled.
Every pot is associated with overflow number O which tell how many minimum no. of stones required
for that pot to overflow. The crow know O1 to On(overflow no. for all the pots). Crow wants some K
pots to be overflow. So the task is minimum number of stones he can make K pots overflow in worst case.
Let say two pots are there with overflow no.s {5,58}, and crow has to overflow one pot(k=1).
So crow will put 5 stones in pot with overflow no.(58), it will not overflow, then he will put in
pot with overflow no.(5), hence the total no. of stones to make overflow one pot is=10.
*/
#include<iostream>
#include<algorithm>
using namespace std;
int n, k;
i=0;
j=0;
k=l;
while(i<n1 && j<n2){
if(L[i]<=R[j]){
overflow_numbers[k]=L[i];
i++;
}else{
overflow_numbers[k]=R[j];
j++;
}
k++;
}
while(i<n1){
overflow_numbers[k]=L[i];
k++;
i++;
}
while(j<n2){
overflow_numbers[k]=R[j];
k++;
j++;
}
}
return stone;
}
int main(){
cin >> n;
int *arr = new int[n + 1];
for(int i=0;i<n;i++){
cin>>arr[i];
}
cin >> k;
merge_sort(arr, 0, n-1);
/*
#include<bits/stdc++.h>
using namespace std;
long long dp[1005][1005];
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
int k;
cin>>k;
int over[n+1];
for(int i=1;i<=n;i++)
{
cin>>over[i];
}
sort(over+1, over+n+1);
cout << solve(over, n, k)<<endl;
}
return 0;
}
*/
int graph[100][100];
int n;
bool dfs( int node , bool *visited , bool *inloop , int &prev ){
visited[node] = 1;
inloop[node] = 1;
for(int i=0;i<n;i++){
if( graph[node][i]){
if(!visited[i]){
if(dfs( i , visited , inloop , prev)){
if(i==prev)
cout<<i<<" " , prev=-1;
else if (prev!=-1)
cout<<i<<" ";
return true;
}
}
else if ( inloop[i] ){
prev=i;
return true;
}
}
}
inloop[node]=0;
return false;
}
return false;
}
int main(){
// Input nodes
cin>>n;
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
graph[i][j]=0;
// Input Edges
int t;
cin>>t;
int x,y;
for(int i=0;i<t;i++){
cin>>x>>y;
graph[x][y]=1;
}
cout<<checkCycle(visited)<<endl;
return 0;
}
#include <iostream>
using namespace std;
int a[100][100], n;
return true;
}
}
else if(a[st][j] == 1 && parent != j && visited[j] == true){
cout << st << " ";
prev = j;
return true;
}
}
return false;
}
int main(){
memset(a,0,sizeof(a));
int m;
cin >> n >> m;
int x,y;
while(m--){
cin>>x>>y;
a[x][y]=1;
a[y][x]=1;
}
return 0;
}
Note: If he reaches a point where there are no further nodes then he leaves the lab after 10 mins and the traveling
time is not considered and during that 10min at 10th min he will be in next division, so be careful
2
6 10 40
1 2 0.3 1 3 0.7 3 3 0.2 3 4 0.8 2 4 1 4 5 0.9 4 4 0.1 5 6 1.0 6 3 0.5 6 6 0.5
6 10 10
1 2 0.3 1 3 0.7 3 3 0.2 3 4 0.8 2 4 1 4 5 0.9 4 4 0.1 5 6 1.0 6 3 0.5 6 6 0.5
6 0.774000
3 0.700000
*/
#include<iostream>
using namespace std;
void docProb(double **graph, int nodes, int time, int curNode, double p, double *answer){
if(time <= 0){
answer[curNode] += p;
return;
}
int main(){
int t;
cin >> t;
while(t--){
int nodes, edges, time;
cin >> nodes >> edges >> time;
Q12. ENDOSCOPY
#include<iostream>
using namespace std;
struct Node{
bool left, right, up, down;
};
struct Point{
int x, y;
};
int result;
Node pipes[1005][1005];
Point queue[1000005];
void bfs(){
for(int i=0; i<n; i++){
for(int j=0; j<m; j++){
vis[i][j] = 0;
dis[i][j] = 0;
}
}
dis[sX][sY] = 1;
vis[sX][sY] = 1;
if( arr[sX][sY] == 0 ){
result = 0;
return;
}
queue[rear].x = sX;
queue[rear].y = sY;
rear = (rear + 1) % 1000005;
while(front != rear){
int p = queue[front].x;
int q = queue[front].y;
front = (front + 1) % 1000005;
/* Row Up */
if( isValid(p-1, q) && vis[p-1][q] == 0 && pipes[p-1][q].down && pipes[p][q].up ){
vis[p-1][q] = 1;
dis[p-1][q] = 1 + dis[p][q];
result++;
queue[rear].x = p-1;
queue[rear].y = q;
rear = (rear + 1) % 1000005;
}
/* Row Down */
if( isValid(p+1, q) && vis[p+1][q] == 0 && pipes[p+1][q].up && pipes[p][q].down ){
vis[p+1][q] = 1;
dis[p+1][q] = 1 + dis[p][q];
result++;
queue[rear].x = p+1;
queue[rear].y = q;
rear = (rear + 1) % 1000005;
}
/* Column Left */
if( isValid(p, q-1) && vis[p][q-1] == 0 && pipes[p][q-1].right && pipes[p][q].left ){
vis[p][q-1] = 1;
dis[p][q-1] = 1 + dis[p][q];
result++;
queue[rear].x = p;
queue[rear].y = q-1;
rear = (rear + 1) % 1000005;
}
/* Column Right */
if( isValid(p, q+1) && vis[p][q+1] == 0 && pipes[p][q+1].left && pipes[p][q].right ){
vis[p][q+1] = 1;
dis[p][q+1] = 1 + dis[p][q];
result++;
queue[rear].x = p;
queue[rear].y = q+1;
rear = (rear + 1) % 1000005;
}
}
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
//code
int t;
cin >> t;
while(t--){
result = 1;
cin >> n >> m >> sX >> sY >> len;
if( arr[i][j] == 1 ){
pipes[i][j].left = true;
pipes[i][j].right = true;
pipes[i][j].up = true;
pipes[i][j].down = true;
}
else if( arr[i][j] == 2 ){
pipes[i][j].left = false;
pipes[i][j].right = false;
pipes[i][j].up = true;
pipes[i][j].down = true;
}
else if( arr[i][j] == 3 ){
pipes[i][j].left = true;
pipes[i][j].right = true;
pipes[i][j].up = false;
pipes[i][j].down = false;
}
else if( arr[i][j] == 4 ){
pipes[i][j].left = false;
pipes[i][j].right = true;
pipes[i][j].up = true;
pipes[i][j].down = false;
}
else if( arr[i][j] == 5 ){
pipes[i][j].left = false;
pipes[i][j].right = true;
pipes[i][j].up = false;
pipes[i][j].down = true;
}
else if( arr[i][j] == 6 ){
pipes[i][j].left = true;
pipes[i][j].right = false;
pipes[i][j].up = false;
pipes[i][j].down = true;
}
else if( arr[i][j] == 7 ){
pipes[i][j].left = true;
pipes[i][j].right = false;
pipes[i][j].up = true;
pipes[i][j].down = false;
}
else{
pipes[i][j].left = false;
pipes[i][j].right = false;
pipes[i][j].up = false;
pipes[i][j].down = false;
}
}
}
bfs();
cout << result << "\n";
}
return 0;
}
Q13. FSIHERMAN
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
#define MAX 3
if (right_min == left_min)
{
// Place 2 fishermen, if avaiable
if ((fishermen[index] - i - 1) > 1)
{
fishspot[left] = fishspot[right] = index + 1;
sum += (2 * left_min);
i++;
visited[index] = 1;
if (count == MAX)
{
if (Answer > sum)
Answer = sum;
return;
}
if (npos == 1)
{
for (i = 0; i < MAX; i++)
{
if (visited[i] == 0)
{
solve(i, sum, count + 1);
visited[i] = 0;
reset_fishspot(i);
}
}
}
else if (npos == 2)
{
fishspot[pos1] = index + 1;
for (i = 0; i < MAX; i++)
{
if (visited[i] == 0)
{
solve(i, sum, count + 1);
visited[i] = 0;
reset_fishspot(i);
}
}
fishspot[pos1] = 0;
fishspot[pos2] = index + 1;
for (i = 0; i < MAX; i++)
{
if (visited[i] == 0)
{
solve(i, sum, count + 1);
visited[i] = 0;
reset_fishspot(i);
}
}
fishspot[pos2] = 0;
}
}
};
// Driver Code
int main()
{
GFG g;
int i;
/*scanf("%d", &N); // for input
Answer = 999999;
return 0;
}
// This code is contributed by SoM15242
Given a 2 D matrix where 1 represent the places where the frog can jump and 0 represent the empty spaces,
the frog can move freely in horizontal direction (on 1’s only) without incurring any cost (jump).
A vertical jump from a given point of the matrix to other point on the matrix can be taken (on 1’s only)
with cost as the number of jumps taken.
Given a source and destination, the frog has to reach the destination minimizing the cost (jump).
*/
#include <iostream>
using namespace std;
#define QS 1000005
struct Point{
int x, y;
};
Point queue[QS];
int front = 0, rear = 0;
void calculateFrogJump(){
queue[rear].x = sX;
queue[rear].y = sY;
rear = (rear + 1) % QS;
vis[sX][sY] = 1;
dis[sX][sY] = 0;
while(front != rear){
int p = queue[front].x;
int q = queue[front].y;
front = (front + 1) % QS;
vis[newX][newY] = 1;
queue[rear].x = newX;
queue[rear].y = newY;
rear = (rear + 1) % QS;
}
}
}
cout << dis[tX][tY];
}
int main(){
cin >> n;
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
cin >> mat[i][j];
vis[i][j] = 0;
dis[i][j] = 0;
}
}
calculateFrogJump();
return 0;
}
When locations of a maze and jewels are given, find out the greatest number of jewels you can get
without taking the same passage twice, and the path taken in this case.
Input
There can be more than one test case in the input file. The first line has T, the number of test cases.
Then the totally T test cases are provided in the following lines (T ≤ 10 ).
In each test case, In the first line, the size of the maze N (1 ≤ N ≤ 10) is given.
The maze is N×N square-shaped. From the second line through N lines, information of the maze is given.
“0” means a passage, “1” means a wall, and “2” means a location of a jewel. The entrance is located
on the upper-most left passage and the exit is located on the lower-most right passage.
There is no case where the path from the entrance to the exit doesn’t exist.
Output
From the first line through N lines, mark the path with 3 and output it. In N+1 line, output the
greatest number of jewels that can be picked up. Each test case must be output separately as a empty.
*/
#include<iostream>
#include<climits>
#define MAX 21
using namespace std;
int n, ans;
void jewelMaze(int **maze, int x, int y, int value, int **visited, int **path){
if(x == n-1 && y == n-1){
if(value >= ans){
ans = value;
if(isValid(newX, newY)){
}
}
int main(){
int t;
cin >> t;
while(t--){
cin >> n;
int **maze = new int * [n + 1];
for(int i=0; i<n; i++){
maze[i] = new int[n + 1];
}
ans = INT_MIN;
int sX = 0, sY = 0;
visited[sX][sY] = 1;
// printMatrix(maze);
if(maze[sX][sY] == 2)
jewelMaze(maze, sX, sY, 1, visited, path);
else
jewelMaze(maze, sX, sY, 0, visited, path);
When locations of a maze and jewels are given, find out the greatest number of jewels you can get
without taking the same passage twice, and the path taken in this case.
Input
There can be more than one test case in the input file. The first line has T, the number of test cases.
Then the totally T test cases are provided in the following lines (T ≤ 10 ).
In each test case, In the first line, the size of the maze N (1 ≤ N ≤ 10) is given.
The maze is N×N square-shaped. From the second line through N lines, information of the maze is given.
“0” means a passage, “1” means a wall, and “2” means a location of a jewel. The entrance is located
on the upper-most left passage and the exit is located on the lower-most right passage.
There is no case where the path from the entrance to the exit doesn’t exist.
Output
From the first line through N lines, mark the path with 3 and output it. In N+1 line, output the
greatest number of jewels that can be picked up. Each test case must be output separately as a empty.
*/
#include<iostream>
#include<climits>
#define MAX 21
using namespace std;
int n, ans;
void jewelMaze(int **maze, int x, int y, int value, int **visited, int **path){
if(x == n-1 && y == n-1){
if(value >= ans){
ans = value;
if(isValid(newX, newY)){
}
}
int main(){
int t;
cin >> t;
while(t--){
cin >> n;
int **maze = new int * [n + 1];
for(int i=0; i<n; i++){
maze[i] = new int[n + 1];
}
ans = INT_MIN;
int sX = 0, sY = 0;
visited[sX][sY] = 1;
// printMatrix(maze);
if(maze[sX][sY] == 2)
jewelMaze(maze, sX, sY, 1, visited, path);
else
jewelMaze(maze, sX, sY, 0, visited, path);
Q17. MR KIM
/*
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.
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) .
The distance between two arbitrary locations (x1, y1) and (x2, y2) is computed by |x1-x2| + |y1-y2|, where |x| denotes
the absolute value
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
to visit all the N customers and return to his among all the possibilities.
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.
Write a program that, starting at the office, finds a (the) shortest path visiting all the customers and returning to his
home.
Your program only have to report the distance of a (the) shortest path.
Constraints
5≤N≤10. Each location (x,y) is in a bounded grid, 0≤x≤100, 0≤y≤100, and x, y are integers.
Input
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
following line enumerates the locations of the office, Mr. Kim’s home, and the customers in sequence. Each location
consists of
the coordinates (x,y), which is reprensented by ‘x y’.
Output
Output the 10 answers in 10 lines. Each line outputs the distance of a (the) shortest path. Each line looks like ‘#x
answer’
where x is the index of a test case. ‘#x’ and ‘answer’ are separated by a space.
I/O Example
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,
and the locations of the customers are (70, 40), (30, 10), (10, 5), (90, 70), (50, 20).)
88 81 85 80 19 22 31 15 27 29 30 10 20 26 5 14
39 9 97 61 35 93 62 64 96 39 36 36 9 59 59 96 61 7 64 43 43 58 1 36
#1 200
#2 304
#3 366
*/
#include<iostream>
#include<climits>
using namespace std;
int x[20],y[20],n,ans;
int main(){
int tCases;
cin >> tCases;//For testcases
for(int i=0;i<tCases;i++){
ans=INT_MAX;//Set ans to max value
cin >> n;
cin >> x[n+1] >> y[n+1] >> x[0] >> y[0];//Input destination and source x,y coordinates
for(int i=1;i<=n;i++){//Input drop off location coordinates
cin >> x[i] >> y[i];
}
bool visited[n+2]={false};
optimalPath(0,visited,0,0);
cout << "#" << i+1 << " " << ans << endl;
}
return 0;
}
/*
#include <iostream>
#include <cstring>
using namespace std;
bool marked[105][105];
struct point {
int x;
int y;
};
int main()
{
int test;
cin >> test ;
for(int l = 1; l <= test; l++){
int nodes;
cin >> nodes ;
point start, end;
point arr[nodes + 3];
cout << "#" << l << " " << minDist << endl ;
}
return 0;
}
*/
Q18. MR LEE
/*
Mr. Lee has to travel various offices abroad to assist branches of each place. But he has a problem.
The airfare would be real high as all offices he has to visit are in foreign countries. He wants to visit every
location only one time and return home with the lowest expense. Help this company-caring man calculate the lowest
expense.
Input format
Several test cases can be included in the inputs. T, the number of cases is given in the first row of the inputs.
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
the first row per each test case. At this moment, No. 1 office is regarded as his company (Departure point).
(1 ≤ N ≤ 12) Airfares are given to move cities in which branches are located from the second row to N number rows.
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
two cities, it is given as zero.
Output format
Output the minimum airfare used to depart from his company, visit all offices, and then return his company on the
first row per each test case.
Example of Input
3
5
0 14 4 10 20
14 0 7 8 7
4 5 0 7 16
11 7 9 0 2
18 7 17 4 0
5
99295
63515
18333
60968
66948
3
0 2 24
302
040
Example of Output
30
18
CUSTOM - 31 <- 4
*/
#include<iostream>
#include<climits>
using namespace std;
int N, result;
void minCostMrLee(int **arr, bool *visited, int count, int cost, int src){
// Base Case
if(count == N-1){
/* Corner Case if no path exists from last city */
if(arr[src][0] != 0)
result = min(result, cost + arr[src][0]);
return;
}
// Main Case
for(int i=0; i<N; i++){
if(!visited[i] && arr[src][i] != 0){
visited[i] = true;
minCostMrLee(arr, visited, count + 1, cost + arr[src][i], i);
visited[i] = false;
}
}
}
int main(){
int t;
cin >> t;
while(t--){
cin >> N;
int **arr = new int*[N];
for(int i=0; i<N; i++){
arr[i] = new int[N];
}
bool *visited = new bool[N];
result = INT_MAX;
visited[0] = true;
/*
#include<iostream>
using namespace std;
int arr[1000][1000];
bool visited[1000];
int ans = 1000000;
void dfs(int n,int count,int cost,int last)
{
//cout<<last<<" ";
if(count==n)
{
//cout<<endl;
int cost1 = cost + arr[last][0];
if(cost1<ans)
ans = cost1;
}
for(int i=1;i<n;i++)
{
if(visited[i])
continue;
if(arr[last][i]==0)
continue;
visited[i]=true;
int cost1 = cost + arr[last][i];
dfs(n,count+1,cost1,i);
visited[i]=false;
}
return;
}
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
ans = 1000000;
cin>>n;
for(int i=0;i<n;i++)
visited[i]=false;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
cin>>arr[i][j];
}
}
dfs(n,1,0,0);
cout<<ans<<endl;
}
}
*/
Input
2
24
6 13 10 2
24
6 10 13 2
output
5
1
*/
#include <iostream>
#include <climits>
using namespace std;
void calculateOilMines(int i, int *oilMines, bool *visited, int minV, int maxV, int sum,
int nodes, int &ANS){
// Base Case
if(visited[i]){
int newMin = MIN(sum, minV);
int newMax = MAX(sum, maxV);
// Main Case
visited[i] = 1;
int j = (i + 1) % mines;
visited[i] = 0;
return;
}
int main() {
// your code goes here
int t;
cin >> t;
while(t--){
cin >> companies >> mines;
int *oilMines = new int[mines + 1];
bool *visited = new bool[mines + 1];
ANS = INT_MAX;
for(int i=0; i<mines; i++)
calculateOilMines(i, oilMines, visited, INT_MAX, INT_MIN, 0, 0, ANS);
You are given an old touch smartphone numbers having dial pad and calculator app.
Aim: The goal is to type a number on dialpad.
But as phone is old, some of the numbers and some operations can't be touched.
For eg. 2,3,5,9 keys are not responding , i.e you cannot use them
But you can always make a number using other numbers and operations in Calculator. There could be multiple ways
of making a number
Calculator have 1-9 and +,-,*,/,= as operations. Once you have made the number in Calculator you can copy the
number and use it.
#Input:#
There will be multiple Test cases .Each test case will consist of 4 lines
1) First line will consist of N,M,O
N: no of keys working in Dialpad (out of 0,1,2,3,4,5,6,7,8,9)
M:types of operations supported (+,-,*,/)
O: Max no of touches allowed
2) second line of input contains the digits that are working e.g 0,2,3,4,6.
3) Third line contains the valued describing operations, 1(+),2(-),3(*),4(/)
4) fourth line contains the number that we want to make .
#Output:#
Output contains 1 line printing the number of touches required to make the number
#Output:#
4
2
5
9
2
If you have to type 18-> 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'.
*/
#include<iostream>
#include<math.h>
using namespace std;
int *working,*operations;
int answer=INT_MAX;
int n,m,o;
int eval(int prev,int curr,int op){
if(prev==-10000000){
return curr;
}
if(op==1){
return prev+curr;
}
if(op==2){
return prev-curr;
}
if(op==3){
return prev*curr;
}
if(op==4){
if(curr==0){
return -1;
}else{
return prev/curr;
}
}
}
bool isDone(int prev,int curr,int Operation,int target){
if(Operation==4 && curr==0){
return false;
}
if(eval(prev,curr,Operation)==target)
return 1;
return false;
}
void findMinTouch(int prev,int curr,int ooperation,int tou,int t)
{ if(ooperation!=-1 && curr!=-10000000)
{
bool k=isDone(prev,curr,ooperation,t);
if(k && tou<o )
{
if(answer>tou+1)
answer=tou+1;
}
}
if(prev==t && tou<o && ooperation !=-1 && curr==-10000000)
{
answer=min(answer,tou);
}
if(ooperation==-1 && curr==t && tou<o )
{
answer=min(answer,tou);
}
if(tou>o) return ;
for(int i=0;i<m;i++)
{
if(curr==-10000000)
break;
if(curr==0 && ooperation==4) continue;
int val=eval(prev,curr,ooperation);
findMinTouch(val,-10000000,operations[i],tou+1,t);
}
for(int i=0;i<n;i++)
{
if(curr==-10000000)
{
findMinTouch(prev,working[i],ooperation,tou+1,t);
}
else
{
int val=abs(curr);
val=val*10+working[i];
if(curr<0){
val*=-1;
}
findMinTouch(prev,val,ooperation,tou+1,t);
}
}
}
int main(){
int t;
cin>>t;
int count = 0;
while(t--){
answer=INT_MAX;
cin>>n>>m>>o;
working=new int[n + 2];
for(int i=0;i<n;i++){
cin>>working[i];
}
operations=new int[m + 2];
for(int i=0;i<m;i++){
cin>>operations[i];
}
int target;
cin>>target;
findMinTouch(-10000000,-10000000,-1,0,target);
count++;
cout<<"#" << count << ": " << answer<<endl;
}
return 0;
}
Example-
Range: 24 to 12943
In three digit: 111, 113, 115, 311, 331, 333, 511, 533, 555 are not valid.
In four digit: All the numbers containing above 3 digit numbers are not valid.
#include <iostream>
using namespace std;
int rougeK = 0;
for(int i=0; i<n; i++){
rougeK += digitArray[delNos[i]];
}
}
return count;
}
int main() {
int a, b, k;
cin >> a >> b >> k;
int n;
cin >> n;
int *delNos = new int[n];
for(int i=0; i<n; i++){
cin >> delNos[i];
}
return 0;
}
INPUT :
1
5
12346
Output :
8
*/
#include <iostream>
using namespace std;
void solve(int a[], int vis[], int p1, int p2, int n, int &ans){
if(p1 == p2){
if(p1 > ans){
ans = p1;
}
}
int main(){
int n;
cin>>n;
int a[n];
cout<<ans;
}
Here is the Task :- Find the shortest of the longest distance of research center from given locations
of rare-elements.
Locations are given in the matrix cell form where 1 represents roads and 0 no road.
Number of rare-element and their location was also given(number<=5) and order of square matrix
was less than equal to (20).
*/
/*
For this you have to implement bfs for every position where road exist to find the distance of
every research center or do Vice-versa. for each position store maximum distance of all distances
to research center and the compare each maximum distance to find minimum of them
Input -
6
52
43
34
11000
11000
11111
11101
11111
82
56
64
11111100
11111110
11010110
11110110
11111110
11111110
00000000
00000000
10 3
82
53
71
0001111110
1111111110
1001000010
1111111111
1111010011
1111010011
1111010011
1111111111
1110010011
1111111111
15 4
11 15
15 9
12
14 3
111111111111111
101111111111101
101000100001101
101000100001101
101111111111111
101000100001101
101000111111111
101000100001101
101000100001101
101000100001101
101000100001101
101000100001101
111111111111111
001000111111101
001111111111111
20 4
13 6
20 4
12
17 16
11111111111111100000
10111111111111100000
10100000001001100000
10100000001001100000
11111111111111110000
10100000001001110000
10100000001001110000
11111111111111111111
10100000001001110011
10100000001001110011
10100000001001110011
10100000001001110011
10111111111111110011
10100000001000110011
10100000001000110011
10100000001000110011
10100000001000110011
11111111111111111111
11111111111111111111
11111111111000000000
52
21
35
10111
11101
01101
01011
11101
Output -
1
2
2
12
15
4
*/
#include <stdio.h>
int Answer = 9999;
int region[22][22];
int visited[22][22];
int N, C;
int location[5][2];
int rear = -1;
int front = -1;
struct queue {
int row;
int col;
}Q[10000];
void init()
{
int m,n;
rear = -1;
front = -1;
for(m = 0; m < 22; m++)
{
for(n = 0; n < 22; n++)
{
visited[m][n] = 0;
}
}
while(front<rear)
{
++front;
discover(Q[front].row, Q[front].col, visited[Q[front].row][Q[front].col]);
}
int main(void){
int T, test_case;
scanf("%d", &T);
for(test_case = 0; test_case < T; test_case++)
{
int i,j,k;
int x,y,c;
int temp = 0;
Answer = 9999;
init();
Answer = 9999;
for(i = 1; i <= N; i++)
{
for(j = 1; j <= N; j++)
{
init();
temp = 0;
if(region[i][j] == 1)
{
visited[i][j] = 1;
discover(i, j, 1);
for(k = 0; k < C; k++)
{
if(temp < visited[location[k][0]][location[k][1]])
temp = visited[location[k][0]][location[k][1]];
}
if(Answer > temp)
Answer = temp;
}
}
}
printf("#%d %d\n", test_case+1, Answer-1);
}
return 0;
}
Q24. SINKHOLE
Q.There is a large plot with various sinkholes present.
Since one sinkhole can combine with another sinkhole, it is required to get
at most one sinkhole while occupying the plot. You have to find the maximum
square-area formed with at most one sinkhole present.
If there are two plots with the same area then print the one with
lesser sinkhole present otherwise if the sinkholes are also same then print
anyone. For each case, you have to print the bottom leftmost coordinate and
top rightmost point. Please keep in mind that the plot starts with (1, 1).
Input: First line will give the number of test cases. For each test case, we
will be given the size of the plot matrix N x M (where 1<=N, M<=1000). Next
line will give the number of sinkholes present in the matrix K (1<=K<=N+M).
Next, K-lines will give the coordinates of the sinkholes.
Output: For each test case, you have to print the number of the test case
and the coordinates of the resultant square.
i.e. #i xb yb xt yt (ith test case, xb=bottomost left x-coordinate,
yb=bottomost left y-coordinate, xt= topmost right x-coordinate,
yt= topmost right y-coordinate)
int xb,yb,xt,yt;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
/* input */
int N,M;
cin>>N>>M;
int A[N][M];
for(int i=0; i<N; i++){
for(int j=0; j<M; j++){
A[i][j]=0;
}
}
int K;
cin>>K;
for(int i=0; i<K; i++){
int x,y;
cin>>x>>y;
A[x-1][y-1]=1;
}
01020
02221
02111
10100
00122
11001
xxSxx
The highlighted(yellow) zone is the control zone. S is a spaceship that we need to control so that we can get
maximum coins.
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.
At each time, the non-highlighted part of the grid will move down by one unit.
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
will be killed.
If we use a bomb at the very beginning, the grid will look like this:
01020
00001
00111
10100
00100
11001
xxSxx
As soon as, the spaceship encounters an enemy or the entire grid has come down, the game ends.
For example,
At the very first instance, if we want to collect a coin we should move left( coins=1). This is because when the
grid comes down by 1 unit we have a coin on the second position and by moving left we can collect that coin.
Next, we should move right to collect another coin (coins=2).
After this, remain at the same position (coins=4).
This is the current situation after collecting 4 coins.
0102001000
0 2 2 2 1 -->after using 0 0 0 0 1
x x S x x -->bomb x x S x x
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.
*/
#include<bits/stdc++.h>
using namespace std;
void updateMatrix(int row,char ** matrix){
if(row<0){
return;
}
int upLimit=max(0,row-4);
for(int i=row;i>=upLimit;i--){
for(int j=0;j<=4;j++){
if(matrix[i][j]=='2'){
matrix[i][j]='0';
}
}
}
}
int findMaxPoints(int row,int col,int bombs,char ** matrix){
if(row<=0 || col<0 || col>=5){
return 0;
}
int answer=0;
if(row>0 && matrix[row-1][col]!='2'){
answer=max(answer,(matrix[row-1][col]=='1'?1:0)+findMaxPoints(row-1,col,bombs,matrix));
}
if(col>0 && row>0 && matrix[row-1][col-1]!='2'){
answer=max(answer,(matrix[row-1][col-1]=='1'?1:0)+findMaxPoints(row-1,col-1,bombs,matrix));
}
if(col<4 && row>0 && matrix[row-1][col+1]!='2'){
answer=max(answer,(matrix[row-1][col+1]=='1'?1:0)+findMaxPoints(row-1,col+1,bombs,matrix));
}
return answer;
}
int main(){
int t, row;
cin >> t;
int tNo = 0;
while(t--){
cin >> row;
char ** matrix=new char*[row + 2];
for(int i=0; i<row; i++){
matrix[i]=new char[5];
for(int j=0;j<5;j++){
cin>>matrix[i][j];
}
}
tNo++;
cout<< "#" << tNo << " : " << findMaxPoints(row,2,1,matrix) << endl;
}
return 0;
}
/*
No rech top
#include <iostream>
using namespace std;
int det[5][5];
int mat[13][5];
void detonate(int r)
{
for(int i=r;i>r-5 && i>=0;i--)
{
for(int j=0;j<5;j++)
{
det[r-i][j]=0;
if(mat[i][j]==2)
{
det[r-i][j]=2;
mat[i][j]=0;
}
}
}
}
void undet(int r)
{
for(int i=r;i>r-5 && i>=0;i--)
for(int j=0;j<5;j++)
{
if( det[r-i][j]==2)
mat[i][j]=2;
}
}
void func(int n,int pos,int c,int &max)
{
if(pos>4||pos<0||c<0)
return;
if(mat[n][pos]==2)
c-=1;
else if(mat[n][pos]==1)
c+=1;
if(n==0)
{
if(c>max)
max=c;
return;
}
else
{
func(n-1,pos+1,c,max);
func(n-1,pos-1,c,max);
func(n-1,pos,c,max);
}
}
int main()
{
int t;
cin>>t;
int count=1;
while(t--)
{
int n;
cin>>n;
for(int i=0;i<n;i++)
for(int j=0;j<5;j++)
cin>>mat[i][j];
int max=-1,c;
for(int j=0;j<5;j++)
mat[n][j]=0;
mat[n][2]=3;
for(int j=n;j>=5;j--)
{
c=-1;
detonate(j-1);
func(n,2,0,c);
if(c>max)
max=c;
undet(j-1);
}
if(max<0)
max=-1;
cout<<"#"<<count<<" "<<max<<endl;
count++;
}
}
*/
/*
#include <iostream>
using namespace std;
int det[5][5];
int mat[13][5];
if(mat[n][pos]==2)
c-=1;
else if(mat[n][pos]==1)
c+=1;
if(n==0){
if(c>max)
max=c;
return;
}
else{
func(n-1,pos+1,c,max);
func(n-1,pos-1,c,max);
func(n-1,pos,c,max);
}
}
int main(){
int t;
cin>>t;
int count=1;
while(t--){
int n;
cin>>n;
for(int i=0;i<n;i++)
for(int j=0;j<5;j++)
cin>>mat[i][j];
for(int j=0;j<5;j++)
mat[n][j]=0;
mat[n][2]=3;
int max=-1,c;
for(int j=n;j>=5;j--){
c=-1;
detonate(j-1);
func(n,2,0,c);
if(c>max)
max=c;
undet(j-1);
}
if(max<0)
max=-1;
cout<<"#"<<count<<" "<<max<<endl;
count++;
}
}
*/
#include <iostream>
using namespace std;
int temp = 0;
while(i<lenString && str[i] >= '0' && str[i] <= '9'){
temp = (temp * 10) + (str[i]-'0');
i++;
}
i--;
int main() {
char s[100];
cin >> s;
mergeAplha(s);
return 0;
}
/*
3
(0(5(6()())(-4()(9()())))(7(1()())(3()())))
*/
int main()
{
int arr[2][100]={0};
int size = 0;
int x;
string str;
cin>>x;
cin>>str;
int level=0, temp=0, commit=0, neg=0;
for(int i=0; i<str.length(); i++){
if(str[i]=='(' || str[i]==')'){
if(commit == 1){
if(neg==1){
temp=temp*-1;
neg=0;
}
arr[0][size]=level;
arr[1][size]=temp;
size++;
temp=0;
}
if(str[i]=='('){
level++;
}
else if(str[i]==')'){
level--;
}
commit=0;
}
else if(str[i]=='-'){
neg=1;
commit = 1;
}
else{
temp= temp*10 + str[i] - '0';
commit = 1;
}
int temp0,temp1;
for(int i=0;i<size-1;i++){
for(int j=i+1; j<size; j++){
if(arr[0][j] > arr[0][i]){
temp0=arr[0][j];
temp1=arr[1][j];
arr[0][j]=arr[0][i];
arr[1][j]=arr[1][i];
arr[0][i]=temp0;
arr[1][i]=temp1;
}
}
}
int sum = 0;
for(int i=0;i<size;i++){
if(arr[0][i]==x+1) sum+=arr[1][i];
}
cout<<sum<<endl;
return 0;
}
A binary matrix of nxm was given, you have to toggle any column k number of times so that you can get the
maximum
number of rows having all 1’s.
100
101
100
if k=2, then we will toggle column 2 and 3 once and we will get rows 1 and 3 with 1 1 1 and 1 1 1 i.e. all 1’s so
answer is 2 as there are 2 rows with all 1’s.
if k=3, then we will toggle column 2 thrice and we will get row 2 with 1 1 1 i.e. all 1’s so answer is 1 as there
is 1 row with all 1’s.
*/
#include<iostream>
for(int i=0;i<r;i++){
for(int j=0;j<c;j++){
grid[i][j]=temp[i][j];
}
}
}
/* calculates energy*/
int caleng(int grid[][100], int r, int c){
int allone=0,eng=0;
for(int i=0;i<r;i++){
for(int j=0;j<c;j++){
if(grid[i][j]==1){
allone++;
}
if(allone==c){
eng++;
}
}
allone=0;
}
return eng;
}
void combinationUtil(int arr[], int data[], int start, int end, int index, int r,int row,int col){
if (index == r){
for (int j = 0; j < r; j++){
flip(grid,row,col,data[j]);
}
restore(grid,row,col);
return;
}
for (int i = start; i <= end && end - i + 1 >= r - index; i++){
data[index] = arr[i];
combinationUtil(arr, data, i+1, end, index+1, r,row,col);
}
}
int main(){
int t=0;
cin>>t;
int testcase=0;
while(t--){
testcase++;
int c,r=0;
cin>>r>>c;
int s=0;
cin>>s;
int arr[c];
for(int j=0;j<c;j++){
arr[j]=j;
}
for(int i=0;i<r;i++){
for(int j=0;j<c;j++){
cin>>grid[i][j];
temp[i][j]=grid[i][j];
}
}
if(s%2==0&&s>=c){
for(int i=2;i<=c;i=i+2){
printCombination(arr, c, i,r,c);
restore(grid,r,c);
}
}
// if flips are even and flips are greater than colums then check for all columns....
if(s%2!=0&&s>=c){
for(int i=1;i<=c;i=i+2){
printCombination(arr, c, i,r,c);
restore(grid,r,c);
}
}
// if flips are odd and flips are greater than colums then check for all columns....
if(s%2==0&&s<c){
for(int i=2;i<=s;i=i+2){
printCombination(arr, c, i,r,c);
restore(grid,r,c);
}
}
// if flips are lessthan columns and even then we dont need to check for all combinations
with even length eg if flips are 3 and colums are 5 than we won't be checking for {1,2,3,4,5}....or if we have 2 flips we
won't be checking for {1,2,3,4} types of combinations
if(s%2!=0&&s<c){
for(int i=1;i<=s;i=i+2){
printCombination(arr, c, i,r,c);
restore(grid,r,c);
}
}
cout<<"#"<<testcase<<" "<<maxi<<endl;
maxi=-1;
}
}
/* the idea/approch behind the problem is that if we have given flips as even then only even length combination of
flips will give max ans
for eg I have 50 filps and 5 columns then even size combinations are {1,2},{2,3},{3,4},{4,5},{1,3} and so on here length
of combination is 2 similarly check for combination length 4 and no need to check for 6 length combination becoz we
have only 5 columns
{1,2,3,4} is one of the combination that we need to check if we will check for all combinations like {1},{2} and so on all
possible combinations including odd length combinations we will encounter TLE
similarly if flips are odd we have to check for odd size combination length({1,2,3},{1},{2},{3}..... and so on ) this isn't
exactly bruteforce but kindoff this method won't lead to TLE thats for sure.here combinations lisited above are the
places where we have to flip column
<----------------------------------------HOPE THIS WILL HELP YOU FOR ALL THE SOLUTIONS TO SAMSUNG PROBLEMS
LIKE ENDOSCOPE WORMHOLES types AND MANY MORE DO LET ME KNOW--------------------------------------->
*/
mask[i] = 0;
}
}
}
int main() {
int t, sX, sY, tX, tY;
cin >> t;
while(t--){
ANS = INT_MAX;
cin >> n;
cin >> sX >> sY >> tX >> tY;
for(int i=0; i<n; i++){
mask[i] = 0;
for(int j=0; j<5; j++){
cin >> w[i][j];
}
}
// #include<iostream>
// using namespace std;
// int n, a[2001][6];
// void wormhole ( int x1 , int y1 , bool *visited , int &ans , int val ) {
// if ( x1 == a[n+1][0] && y1 == a[n+1][1] ){
// ans = min ( ans , val);
// return ;
// }
// //entry
// wormhole ( a[i][2] , a[i][3] , visited , ans , val + dist ( x1 , y1 , a[i][0] , a[i][1] ) + a[i][4]);
// //exit
// wormhole ( a[i][0] , a[i][1] , visited , ans , val + dist ( x1 , y1 , a[i][2] , a[i][3] ) + a[i][4]);
// visited[i] = false;
// }
// }
// }
// int main(){
// int t;cin >> t ;
// for (int i = 1; i <= t ; i++){
// cin >> n;
// int sx, sy, dx,dy;
// cin>>sx>>sy>>dx>>dy;
// int ans ;
// bool visited[n+2] = { false };
// cout << "#" << i << " : " << ans << endl;
// }
// return 0;
// }
int main(){
int test;
cin >> test;
for(int t=1;t<=test;t++){
int warmholes;
cin>>warmholes;
/* Initialise Code */
int nodes = 2 * warmholes + 2;
struct node *location[nodes];
int cost[nodes][nodes];
for(int i=0;i<nodes;i++){
for(int j=0;j<nodes;j++){
cost[i][j]=-1;
}
}
//source
int sx, sy;
cin>>sx>>sy;
struct node *src=new struct node;
src->x=sx;
src->y=sy;
location[0]=src;
//destination
int dox,doy;
cin>>dox>>doy;
struct node *temp=new node;
temp->x=dox;
temp->y=doy;
location[nodes-1]=temp;
//warmhole
for(int i=0;i<warmholes;i++){
int six, siy;
cin>>six>>siy;
struct node *temp=new node;
temp->x=six;
temp->y=siy;
int dix,diy;
cin>>dix>>diy;
struct node *temp1=new node;
temp1->x=dix;
temp1->y=diy;
int w;
cin>>w;
location[2*i+1]=temp;
location[2*i+2]=temp1;
cost[2*i+1][2*i+2]=w;
cost[2*i+2][2*i+1]=w;
}
// Floyd Warshall
for(int k=0;k<nodes;k++){
for(int i=0;i<nodes;i++){
for(int j=0;j<nodes;j++){
if(i==k||j==k)
continue;
cost[i][j]=min(cost[i][j],cost[i][k]+cost[k][j]);
}
}
}
cout << "#" << t << " : " << cost[0][nodes-1] << "\n";
}
return 0;
}
Q31.
Given below are the raw materials quantities and their respective selling price(if sold as raw).
D --> No of CPUs
E --> No of memory chips
F --> No of boards
d --> Selling price of CPU
e --> Selling price of Memory chips
We are given N Computer configurations like below :
Di, Ei, Fi, SPi, which are the CPU, Chips, Boards and one unit selling price for ith computer respectively.
Our task is to maximize the final cost.
Constraints:
1. Can use at Max 3 different Configurations
2. We can use 1 configuration multiple times
3. Remaining Inventories can be sold on its selling price
Input:
T --> Number of test cases.
D E F d e --> Inventories
N --> Total Configuration Count
Di Ei Fi SPi
...
Dn En Fn SPn
1<=T<=10
1<= D, E, F <= 100
1<= d, e <=100000
1<=N<=8
Output:
First Line print the Case #testCaseNumber
Second Line Print Maximum Cost per test case in each line.
Sample Input:
1 --> Total Test Case
10 10 10 2 1 --> D E F d e
1 --> PC Configuration Count
1 2 2 3 --> D1 E1 F1 SP1
Sample Output:
Case #1
30
Solution:
#include<iostream>
int D,E,F,d,e;
int config;
int answer = 0;
struct configuration
{
int D,E,F,SPi;
};
configuration m[9];
void solve(int index, int counta, int D, int E, int F, int cost )
{
int i = 1;
while(true)
{
if( D - m[index].D*i >= 0 && E - m[index].E*i >=0 && F - m[index].F*i >= 0 )
{
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);
++i;
}
else
{
break;
}
}
return;
int main()
{
int t;
cin >> t;
repe(_cases,1,t)
{
answer = 0;
cin >> D >> E >> F >> d >> e;
rep(i,0,config)
{
cin >> m[i].D >> m[i].E >> m[i].F >> m[i].SPi;
}
solve(0,0,D,E,F,0);
cout << "Case #"<<_cases << "\n" << answer <<"\n";
return 0;
}
Q32.
You want to cut a piece of paper by a certain fixed rule to make some pieces of white or
blue colored square paper with various sizes.
If the size of the entire paper is N×N (N = 2^K; 1 <= K <= 7; K = natural number), the cutting rules
are as below.
‘If the entire piece of paper is not colored the same, cut the middle part horizontally and vertically
to divide it into the same sized four pieces of paper,
(N/2)×(N/2), as with I, II, III, IV in < FIG. 2 >.
For each I, II, III and IV, cut and divide again in the same way if one entire piece of paper
is not colored the same, and make them into the same sized four pieces of paper. Continue until each and
every piece of paper has only one color of white or blue.’
When you finish, < FIG. 3 > shows the first division of < FIG. 1 > and < FIG. 4 >
shows the final version of 9 pieces of white paper and 7 pieces of blue paper of various sizes.
[Input]
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)
The length of an edge of the first given piece of paper, N, is given for the first line of each test case.
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.
[Output]
For each test case, you should print "Case #T" in the first line where T means the case number.
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.
[I/O Example]
Input
2
8
11000011
11000011
00001100
00001100
10001111
01001111
00111111
00111111
16
1001000000110111
1101011000000000
0000101111001001
1100100100101110
0111001100100111
1011000101010011
1111110011111000
1101010010111001
1111110010110110
1001110000111100
1001111000110101
1110110011111101
1111110000111100
1111110111111100
1100000011011000
1100110001111000
Output
Case #1
97
Case #2
88 99
Solution :
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define debug(x) cout << '>' << #x << ':' << x << endl;
const int maxn = 129;
int white = 0, blue = 0;
if(!same){
solve(arr, size / 2, sti, stj);
solve(arr, size / 2, sti + size/2, stj);
solve(arr, size / 2, sti, stj + size/2);
solve(arr, size / 2, sti + size/2, stj + size/2);
}
else{
(arr[sti][stj]) ? ++blue : ++white ;
}
}
int main()
{
int test ;
cin >> test ;
for(int l = 1; l <= test; l++){
white = 0;
blue = 0;
int size ;
cin >> size;
bool arr[maxn][maxn];
for(int i = 0; i < size; i++){
for(int j = 0; j < size; j++){
cin >> arr[i][j] ;
}
}
solve(arr, size, 0, 0);
cout << "Case #" << l << endl;
cout << white << " " << blue << endl;
}
return 0;
}
// optimized approach using prefix sum of matrix
#include <iostream>
using namespace std;
const int maxn = 129;
int white = 0, blue = 0;
void solve(int arr[maxn][maxn], int size, int si,int sj)
{
if( size == 0)
return;
int sum = arr[size + si - 1][size + sj - 1];
if(sj - 1 >= 0)
sum -= arr[size + si - 1][sj - 1];
if(si - 1 >= 0)
sum -= arr[si - 1][sj + size - 1];
if( si - 1 >= 0 && sj - 1 >= 0)
sum += arr[si - 1][sj - 1];
if(si == 4 && sj == 1)
cout<<sum<<endl;
if(sum == 0)
{
// cout<<si<<"white"<<sj<<" "<<size<<" "<<white<<endl;
white++;
return;
}
if(sum == size * size)
{
//cout<<si<<"blue"<<sj<<" "<<size<<" "<<blue<<endl;
blue++;
return;
}
solve(arr, size / 2, si, sj);
solve(arr, size / 2, si + size/2, sj);
solve(arr, size / 2, si, sj + size/2);
solve(arr, size / 2, si + size / 2, sj + size / 2);
}
int main() {
int test ;
cin >> test ;
for(int l = 1; l <= test; l++){
white = 0;
blue = 0;
int size ;
cin >> size;
int arr[maxn][maxn];
for(int i = 0; i < size; i++){
for(int j = 0; j < size; j++){
int a;
cin>>a;
if( i == 0 && j == 0)
arr[i][j] = a;
else if( i == 0)
arr[i][j] = a + arr[i][j - 1];
else if( j == 0)
arr[i][j] = a + arr[i - 1][j];
else
arr[i][j] = a + arr[i - 1][j] + arr[i][j - 1] - arr[i - 1][j - 1];
}
}
solve(arr, size, 0, 0);
cout << "Case #" << l << endl;
cout << white << " " << blue << endl;
}
return 0;
}