A
cube has its 6 faces numbered from 1-6 (three of its faces are shown in
the diagram). There are six possible orientations on the cube
(U,D,N,E,W,S) as shown in the diagram, of which only 4 are possible on
any one face (for example, on face 1, N and S are impossible
orientations)
An electronic remote control bug
is on a face of a cube at some orientation. On your command it can crawl
around the cube. The possible commands are F,B,L,R.
Command F : it moves to the adjacent face in the same direction it is facing
.Command B : it turns around by 180° and moves forward by one face.
Command L: it turns left and moves forward by one face
Command R: it turns right and moves forward by one face.
The faces 4,5,6 are opposite to faces 1,2,3 respectively.
For
example, if it has orientation U on face 1, on command L, it goes to
face 5 and has orientation N, and from there on command F , it will move
to face 4 and will have orientation E.
Given a
sequence of commands, and the final position (face and orientation) of
the bug (after executing the commands in the sequence), we need to
determine the initial position (face and orientation) of the bug.
The length of the string of command letters <=50
One
string of 2 characters giving the face and orientation of the bug after
it executes the instructions. The first character is a number between 1
and 6, denoting the face, and the second character is the orientation
(from the set {U, D, N, S, E, W}) of the bug after executing the
commands
One string of command letters. This is a sequence of letters from the set of valid commands {F, B, L, R}
One
string of two characters denoting the position of the bug before it
executes the instructions. The first character gives the face number
(1,2,3,...,6) and the second giving the orientation (E,W,U,D,N,S ) the
bug was facing before it executed the instructions
Example 1
Input
1U
FFF
Output
3N
Explanation
If
the bug starts at 3N, it will move to 4D, 6S and 1U if a command F is
given at each position. Hence, if it starts at 3N, after 3 consecutive F
commands, it will be at 1U, which is the given final position. Hence
the output is 3N.
Example 2
Input
4W
LRB
Output
3E
Explanation
If
the bug starts at 3E, after the L command, it goes to 4D, and after the
R command, it goes to 2S. From there, on the B command, it goes to 4W,
which is the end position. Hence the output is 3E.
Program:
#include<stdio.h>
#include<string.h>
int main()
{
char c1,c2,a[6],com[]={"WDSEUNWDSEUN"},s[51],b[10];
int i=0,j,k,t,n;
scanf("%c%c",&c1,&c2);
scanf("%s",s);
n=strlen(s);
t=c1-48;
for(i=n-1;i>=0;i--)
{
for(j=t-1,k=0;j<t+4;j++,k++)
a[k]=com[j];
for(j=0;j<5;j++)
{
if(c2==a[j])
{
c2=com[t+1];
if(s[i]=='B')
c2=com[t+4];
t=t+j+1;
if(t>6)
t-=6;
if(s[i]=='R' || s[i]=='L')
{
if(s[i]=='R')
{
switch(c2)
{
case 'U': strcpy(b,"WSUEND"); break;
case 'D': strcpy(b,"ENDWSU"); break;
case 'E': strcpy(b,"UENDWS"); break;
case 'W': strcpy(b,"DWSUEN"); break;
case 'S': strcpy(b,"SDENUW"); break;
case 'N': strcpy(b,"NUWSDE"); break;
}
}
else if(s[i]=='L')
{
switch(c2)
{
case 'D': strcpy(b,"WSUEND"); break;
case 'U': strcpy(b,"ENDWSU"); break;
case 'W': strcpy(b,"UENDWS"); break;
case 'E': strcpy(b,"DWSUEN"); break;
case 'N': strcpy(b,"SDENUW"); break;
case 'S': strcpy(b,"NUWSDE"); break;
}
}
c2=b[t-1];
}
break;
}
}
}
printf("%d%c\n",t,c2);
return 0;
}
Building Blocks
Problem Description
The
Mathematics teacher wanted to introduce a new competition to the
students to sharpen their skills in optimization. He drew a rectangular M
×N grid on the ground and filled it with some non-negative integers on
each cell of the grid. The cell named (i,j) is at the intersection of i
th row and j th column. He gave the following challenge to the students:
1. On each cell of the grid, you can pile any number of cube blocks.
2. Each layer must be rectangular (with no gaps) and rest p supported completely by the immediate below layer.
3. The number of blocks on each cell should not exceed the number written on the cell on the ground.
4. In each layer, the cell above (1,1) must be covered.
The challenge is to pile up the maximum number of blocks subject to the above conditions.
For example if the bottom grid was as follows:
the maximum number of blocks you can pile is 6 with one layer covering all the cells from (1,1) to (3,2).
Constraints
1 <= M,N <= 50
Maximum value in each cell is 50
The
first line will contain two comma separated integers M,N giving the
size of the grid, where M is the number of rows and N is the number of
columns.
The next M lines will each contain comma separated N non-negative integers giving the numbers in the grid cells.
Output
One line containing the number of blocks that can be piled according to the rules.
Explanation
Example 1:
Input:
3,4
5,4,9,3
4,3,5,6
2,2,1,1
Output:
32
Explanation:
One example of the maximum number of blocks that could be piled on the grid is shown below:
The total number of blocks is 32. Hence the output is 32.
Example 2:
Input
4,7
27,26,28,14,15,38,0
38,40,35,2,20,43,39
18,48,43,2,47,18,26
38,2,29,23,14,31,32
Output
242
Explanation
The number of rows is 4. The number of columns is 7. The values in the cells of the grid are
| 27 |
26 |
28 |
14 |
15 |
38 |
0 |
| 38 |
40 |
35 |
2 |
20 |
43 |
39 |
| 18 |
48 |
43 |
2 |
47 |
18 |
26 |
| 38 |
2 |
29 |
23 |
14 |
31 |
32 |
One possible maximal placing of the blocks is
| 27 |
26 |
26 |
2 |
2 |
2 |
0 |
| 27 |
26 |
26 |
2 |
2 |
2 |
0 |
| 18 |
18 |
18 |
2 |
2 |
2 |
0 |
| 2 |
2 |
2 |
2 |
2 |
2 |
0 |
As there are 242 blocks in this maximal placing, the output is 242.
Program:
#include<stdio.h>
int a[10][10];
void del(int i,int j,int s,int f,int r,int c)
{
int k,l;
if(s==0)
{
for(k=0;k<r;k++)
for(l=j;l<c;l++)
a[k][l]=f-1;
}
else
{
for(k=i;k<r;k++)
for(l=0;l<c;l++)
a[k][l]=f-1;
}
}
int rsum(int i,int j,int r,int c)
{
int k,l,s=0;
for(k=0;k<i;k++)
for(l=0;l<c;l++)
s+=a[k][l];
return s;
}
int csum(int i,int j,int r,int c)
{
int k,l,s=0;
for(k=0;k<r;k++)
for(l=0;l<j;l++)
s+=a[k][l];
return s;
}
int main()
{
int i,j,k,r,c,max=0,r1,c1,s,t1,t2,sum=0;
char ch;
scanf("%d%c%d",&r,&ch,&c);
r1=r;
c1=c;
for(i=0;i<r;i++)
for(j=0;j<c;j++)
{
scanf("%d",&a[i][j]);
if(j<c-1)
scanf("%c",&ch);
if(a[i][j] > max)
max=a[i][j];
}
for(k=1;k<=max;k++)
{
for(i=0;i<r1;i++)
for(j=0;j<c1;j++)
{
if(a[i][j] < k)
{
t1=rsum(i,j,r1,c1);
t2=csum(i,j,r1,c1);
s=(t1 > t2) ? 1 : 0;
del(i,j,s,k,r1,c1);
if(s==1)
r1=i;
else
c1=j;
}
}
}
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
sum+=a[i][j];
}
printf("%d",sum);
return 0;
}