733 - Follow the Folding Dot

All about problems in Volume 7. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

Post Reply
Jody
New poster
Posts: 1
Joined: Wed Jun 19, 2002 3:55 pm

733 - Follow the Folding Dot

Post by Jody »

#include <iostream>
#include <cmath>
using namespace std;

int refreshPos(double dimX, double dimY, double x, double y, char InitialPos){
double half_dimX=dimX/2;
double half_dimY=dimY/2;
if(x<half_dimX && y<half_dimY && InitialPos=='T')
return 1;
if(x>half_dimX && y<half_dimY && InitialPos=='T')
return 2;
if(x<half_dimX && y>half_dimY && InitialPos=='T')
return 3;
if(x>half_dimX && y>half_dimY && InitialPos=='T')
return 4;
if(x<half_dimX && y<half_dimY && InitialPos=='B')
return 5;
if(x>half_dimX && y<half_dimY && InitialPos=='B')
return 6;
if(x<half_dimX && y>half_dimY && InitialPos=='B')
return 7;
if(x>half_dimX && y>half_dimY && InitialPos=='B')
return 8;
return 0;
}

void main(){
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(6);
unsigned long paper=1;

while(true){

double x,y,dimX,dimY;
char command1, command2, InitialPos;
int dotPos=0;
unsigned long page=1;


if(!(cin>>dimX))
break;

cin>>dimY;
cin>>x>>y>>InitialPos;
dotPos=refreshPos(dimX,dimY,x,y,InitialPos);
page=1;
int count=0;

cout<<"Paper number "<<paper<<endl;
cout<<"Beginning paper dimensions "<<dimX<< " X " <<dimY<<endl;
cout<<"Dot is on ";
if(InitialPos=='T')
cout<<"TOP";
else cout<<"BOTTOM";
cout<<" of page "<<page<<". Position "<<x<<" X "<<y<<endl;
cout<<endl;

while(cin>>command1){

dotPos=refreshPos(dimX,dimY,x,y,InitialPos);

if(command1=='S')
break;

cin>>command2;

// Calc. new dimension
if(command1=='L' || command1=='R')
dimX/=2;
else dimY/=2;

if(command1=='L'){
if(command2=='U'){
switch(dotPos){
case 1:
case 3:
case 5:
case 7: x=dimX-x;
page=(pow(2,count)-page+1)+pow(2,count);
if( InitialPos == 'T' )
InitialPos='B';
else InitialPos='T';
break;
default: x-=dimX;
}
}else

if(command2=='O'){
switch(dotPos){
case 1:
case 3:
case 5:
case 7:
x=dimX-x;
page=(pow(2,count)-page+1);
if( InitialPos == 'T' )
InitialPos='B';
else InitialPos='T';
break;
default: x-=dimX;
page+=pow(2,count);
}
}
}

if(command1=='R'){
if(command2=='U'){
switch(dotPos){
case 2:
case 4:
case 6:
case 8:
x=dimX-(x-dimX);
page=(pow(2,count)-page+1)+pow(2,count);
if( InitialPos == 'T' )
InitialPos='B';
else InitialPos='T';
break;
default: ;
}
}else

if(command2=='O'){
switch(dotPos){
case 2:
case 4:
case 6:
case 8: x=dimX-(x-dimX);
page=(pow(2,count)-page+1);
if( InitialPos == 'T' )
InitialPos='B';
else InitialPos='T';
break;
default: page+=pow(2,count);
}
}
}

if(command1=='T'){
if(command2=='U'){
switch(dotPos){
case 1:
case 2:
case 5:
case 6: y=dimY-y;
page=(pow(2,count)-page+1)+pow(2,count);
if( InitialPos == 'T' )
InitialPos='B';
else InitialPos='T';
break;
default: y-=dimY;
}
}else

if(command2=='O'){
switch(dotPos){
case 1:
case 2:
case 5:
case 6: y=dimY-y;
page=(pow(2,count)-page+1);
if( InitialPos == 'T' )
InitialPos='B';
else InitialPos='T';
break;
default: y-=dimY;
page+=pow(2,count);
}
}
}

if(command1=='B'){
if(command2=='U'){
switch(dotPos){
case 3:
case 4:
case 7:
case 8: y=dimY-(y-dimY);
page=(pow(2,count)-page+1)+pow(2,count);
if( InitialPos == 'T' )
InitialPos='B';
else InitialPos='T';
break;
default: ;
}
}else

if(command2=='O'){
switch(dotPos){
case 3:
case 4:
case 7:
case 8: y=dimY-(y-dimY);
page=(pow(2,count)-page+1);
if( InitialPos == 'T' )
InitialPos='B';
else InitialPos='T';
break;
default: page+=pow(2,count);
}
}

}
count++;

}


cout<<"After folding paper. Paper dimensions: "<<dimX<< " X " <<dimY<<endl;
cout<<"Dot is on ";
if(InitialPos=='T')
cout<<"TOP";
else cout<<"BOTTOM";
cout<<" of page "<<page<<". Position "<<x<<" X "<<y<<endl;
cout<<endl;

paper++;
}

}[cpp][/cpp]
turingcomplete
New poster
Posts: 11
Joined: Wed Oct 09, 2002 7:31 pm
Contact:

733 - what am i missing?

Post by turingcomplete »

I can't tell why with my test cases but the judge gives "WA" for my solution. Can you give me a case where my program gives the wrong answer? What am I missing here?
turingcomplete
New poster
Posts: 11
Joined: Wed Oct 09, 2002 7:31 pm
Contact:

Post by turingcomplete »

[cpp]
///////////////////////////////////////////////////////////////////////////////
// solution to http://acm.uva.es/p/v7/733.html
// problem 733
// Wrong Answer
///////////////////////////////////////////////////////////////////////////////

// @BEGIN_OF_SOURCE_CODE
// @JUDGE_ID: xxxxxx 733 C++

// if you only use TRACE and printVector for debugging output then defining this
// will remove the output for submitting.
#define NDEBUG

#include <iostream>
#include <vector>
#include <cassert>
#include <string>
#include <cmath>

#ifdef WIN32
#include <sstream>
#else
#include <strstream>
typedef strstream stringstream;
#endif

#ifdef NDEBUG
#define TRACE(x) ((void)0)
#else

#define TRACE(x) cout<< #x ":\t'"<<( x )<<"'" << endl
#endif

#define MAX(a,b) ((a)>(b)?(a):(b))
#define MIN(a,b) ((a)<(b)?(a):(b))

using namespace std;

//-----------------------------------------------------------------------------
// Reads a line of input, and adds each value in the line to the vector
// Returns false when it reads EOF
// Returns an empty vector if the line is empty
// Lines with invalid terms are ignored after the first invalid term.
// Vector is cleared before values are added.
//
template<class T>
bool getInputLine(vector<T>& vec)
{
vec.clear();

if (cin.eof()) {
return false;
}

stringstream inLine;

cin.clear();
cin.get(*inLine.rdbuf());
if (cin.fail() && !cin.eof()) {
cin.clear();
}
cin.get(); // get rid of delimiter
while(!inLine.eof())
{
T field;
inLine >> field;
if(!inLine.fail()) {
vec.push_back(field);
}
}

return true;
}

long double toDouble(string s) {
stringstream ss;
ss << s << ends;
long double val;
ss >> val;
assert(!ss.fail());
return val;
}

string toString(long double val, int decimalPlaces) {
stringstream ss;
int placesBefore = MAX(0, (int) log10(val));
ss.precision(placesBefore + decimalPlaces);
ss.width(placesBefore + decimalPlaces + 1);
ss.setf(ios::fixed | ios::showpoint);
ss << val << ends;
return ss.str();

}

long double gWidth, gHeight, gX, gY;
bool gTop;
long gPageLoc, gNumPages;

void moveTopBottom(bool top, bool over) {
bool faceChange;
// height/width
long double height = gHeight / 2;
// top?
if (gY < height && top || gY > height && !top) {
gTop = !gTop;
faceChange = true;
} else {
faceChange = false;
}
// x/y
if (faceChange) {
if (top) {
gY = height - gY;
} else {
gY = gHeight - gY;
}
} else if (top) {
gY -= height;
}
// pageLoc
if (faceChange) {
if (over) {
gPageLoc = gNumPages - gPageLoc + 1;
} else {
gPageLoc += gNumPages;
}
} else {
if (over) {
gPageLoc += gNumPages;
}
}
// numPages
gNumPages *= 2;
// commit changes
gHeight = height;
}

void moveLeftRight(bool left, bool over) {
bool faceChange;
// height/width
long double width = gWidth / 2;

// left
if (gX < width && left || gX > width && !left) {
gTop = !gTop;
faceChange = true;
} else {
faceChange = false;
}

// x/y
if (faceChange) {
if (left) {
gX = width - gX;
} else {
gX = gWidth - gX;
}
} else if (left) {
gX -= width;
}

// pageLoc
/// TRACE(faceChange);
/// TRACE(over);
if (faceChange) {
if (over) {
gPageLoc = gNumPages - gPageLoc + 1;
} else {
gPageLoc += gNumPages;
}
} else {
if (over) {
gPageLoc += gNumPages;
}
}

/// TRACE(gPageLoc);
// numPages
gNumPages *= 2;

// commit changes
gWidth = width;
}

int main() {
TRACE("started");

vector<long double> dimensions;
long numPapers = 0;
bool firstTime = true;
while (getInputLine(dimensions)) {
if (dimensions.empty()) {
continue;
}
if (!firstTime) {
cout << endl << endl;
} else {
firstTime = false;
}
assert(dimensions.size() == 2);
gWidth = dimensions[0];
gHeight = dimensions[1];
vector<string> position;
getInputLine(position);
assert(position.size() == 3);
gX = toDouble(position[0]);
gY = toDouble(position[1]);
assert(position[2].length() == 1);
gTop = position[2][0] == 'T';
gPageLoc = 1;
gNumPages = 1;

cout << "Paper number " << ++numPapers << endl;
cout << "Beginning paper dimensions " << toString(gWidth, 6) << " X " <<
toString(gHeight, 6) << endl;
cout << "Dot is on " << (gTop ? "TOP" : "BOTTOM") << " of page " << gPageLoc << ". "
"Position: " << toString(gX, 6) << " X " << toString(gY, 6) << endl << endl;

vector<string> fold;
while (true) {
getInputLine(fold);
assert(fold.size() == 1);
assert(fold[0].length() > 0 && fold[0].length() < 3);
if (fold[0][0] == 'S') {
break;
}
assert(fold[0].length() == 2);
TRACE(fold[0]);
if (fold[0][0] == 'T' || fold[0][0] == 'B') {
moveTopBottom(fold[0][0] == 'T', fold[0][1] == 'O');
} else {
moveLeftRight(fold[0][0] == 'L', fold[0][1] == 'O');
}
TRACE(gWidth);
TRACE(gHeight);
TRACE(gX);
TRACE(gY);
TRACE(gTop);
TRACE(gNumPages);
TRACE(gPageLoc);
}
cout << "After folding paper. Paper dimensions: " << toString(gWidth, 6) <<
" X " << toString(gHeight, 6) << endl;
cout << "Dot is on " << (gTop ? "TOP" : "BOTTOM") << " of page " << gPageLoc << ". "
"Position " << toString(gX, 6) << " X " << toString(gY, 6);
}
TRACE("end");
return 0;
}

[/cpp][/cpp]
turingcomplete
New poster
Posts: 11
Joined: Wed Oct 09, 2002 7:31 pm
Contact:

Post by turingcomplete »

You are missing a colon in the first print statement. That fixes it. Check the requirements.
turingcomplete
New poster
Posts: 11
Joined: Wed Oct 09, 2002 7:31 pm
Contact:

Post by turingcomplete »

As far as I can tell the following two programs are producing the exact same output, however one is accepted while the other one is WA. How can this be? I'm really at a loss as to why this is the case.
:x :x :x
turingcomplete
New poster
Posts: 11
Joined: Wed Oct 09, 2002 7:31 pm
Contact:

Post by turingcomplete »

[cpp]
///////////////////////////////////////////////////////////////////////////////
// solution to http://acm.uva.es/p/v7/733.html
// problem 733
// Wrong Answer
///////////////////////////////////////////////////////////////////////////////

// @BEGIN_OF_SOURCE_CODE
// @JUDGE_ID: xxxxxx 733 C++

// if you only use TRACE and printVector for debugging output then defining this
// will remove the output for submitting.
#define NDEBUG

#include <iostream>
#include <vector>
#include <cassert>
#include <string>
#include <cmath>

void ASSRT(bool cond) {
if (!cond) {
for(;;)
;
}
}
#ifdef WIN32
#include <sstream>
#else
#include <strstream>
typedef strstream stringstream;
#endif

#ifdef NDEBUG
#define TRACE(x) ((void)0)
#else

#define TRACE(x) cout<< #x ":\t'"<<( x )<<"'" << endl
#endif

#define MAX(a,b) ((a)>(b)?(a):(b))
#define MIN(a,b) ((a)<(b)?(a):(b))

using namespace std;

//-----------------------------------------------------------------------------
// Reads a line of input, and adds each value in the line to the vector
// Returns false when it reads EOF
// Returns an empty vector if the line is empty
// Lines with invalid terms are ignored after the first invalid term.
// Vector is cleared before values are added.
//
template<class T>
bool getInputLine(vector<T>& vec)
{
vec.clear();

if (cin.eof()) {
return false;
}

stringstream inLine;

cin.clear();
cin.get(*inLine.rdbuf());
if (cin.fail() && !cin.eof()) {
cin.clear();
}
cin.get(); // get rid of delimiter
while(!inLine.eof())
{
T field;
inLine >> field;
if(!inLine.fail()) {
vec.push_back(field);
}
}

return true;
}

long double toDouble(string s) {
stringstream ss;
ss << s << ends;
long double val;
ss >> val;
ASSRT(!ss.fail());
return val;
}

string toString(long double val, int decimalPlaces) {
stringstream ss;
//int placesBefore = MAX(0, (int) log10(val));
//ss.precision(placesBefore + decimalPlaces);
//ss.width(placesBefore + decimalPlaces + 1);
ss.setf(ios::fixed | ios::showpoint);
ss << val << ends;
return ss.str();

}

long double gWidth, gHeight, gX, gY;
bool gTop;
long gPageLoc, gNumPages;

void moveTopBottom(bool top, bool over) {
bool faceChange;
// height/width
long double height = gHeight / 2;
// top?
if (gY < height && top || gY > height && !top) {
gTop = !gTop;
faceChange = true;
} else {
faceChange = false;
}
// x/y
if (faceChange) {
if (top) {
gY = height - gY;
} else {
gY = gHeight - gY;
}
} else if (top) {
gY -= height;
}
// pageLoc
/// TRACE(faceChange);
/// TRACE(over);
// am i missing something here? sometimes it's right and sometimes it's not
if (faceChange) {
if (over) {
gPageLoc = gNumPages - (gPageLoc - 1);
} else {
gPageLoc = gNumPages - (gPageLoc - 1) + gNumPages;
}
} else {
if (over) {
gPageLoc += gNumPages;
}
}
// numPages
gNumPages *= 2;
// commit changes
gHeight = height;
}

void moveLeftRight(bool left, bool over) {
bool faceChange;
// height/width
long double width = gWidth / 2;

// left
if (gX < width && left || gX > width && !left) {
gTop = !gTop;
faceChange = true;
} else {
faceChange = false;
}

// x/y
if (faceChange) {
if (left) {
gX = width - gX;
} else {
gX = gWidth - gX;
}
} else if (left) {
gX -= width;
}

// pageLoc
/// TRACE(faceChange);
/// TRACE(over);
if (faceChange) {
if (over) {
gPageLoc = gNumPages - (gPageLoc - 1);
} else {
gPageLoc = gNumPages - (gPageLoc - 1) + gNumPages;
}
} else {
if (over) {
gPageLoc += gNumPages;
}
}

/// TRACE(gPageLoc);
// numPages
gNumPages *= 2;

// commit changes
gWidth = width;
}

int main() {
TRACE("started");

vector<long double> dimensions;
long numPapers = 0;
while (getInputLine(dimensions)) {
if (dimensions.empty()) {
continue;
}
ASSRT(dimensions.size() == 2);
gWidth = dimensions[0];
gHeight = dimensions[1];
vector<string> position;
getInputLine(position);
ASSRT(position.size() == 3);
gX = toDouble(position[0]);
gY = toDouble(position[1]);
ASSRT(gX < gWidth);
ASSRT(gY < gHeight);
ASSRT(position[2].length() == 1);
gTop = position[2][0] == 'T';
gPageLoc = 1;
gNumPages = 1;

cout << "Paper number " << ++numPapers << endl;
cout << "Beginning paper dimensions " << toString(gWidth, 6) << " X " <<
toString(gHeight, 6) << endl;
cout << "Dot is on " << (gTop ? "TOP" : "BOTTOM") << " of page " << gPageLoc << ". "
"Position: " << toString(gX, 6) << " X " << toString(gY, 6) << endl << endl;

vector<string> fold;
while (true) {
getInputLine(fold);
ASSRT(fold.size() == 1);
ASSRT(fold[0].length() > 0 && fold[0].length() < 3);
if (fold[0][0] == 'S') {
break;
}
ASSRT(fold[0].length() == 2);
TRACE(fold[0]);
if (fold[0][0] == 'T' || fold[0][0] == 'B') {
moveTopBottom(fold[0][0] == 'T', fold[0][1] == 'O');
} else {
moveLeftRight(fold[0][0] == 'L', fold[0][1] == 'O');
}
/// TRACE(gWidth);
/// TRACE(gHeight);
TRACE(gX);
TRACE(gY);
TRACE(gTop);
/// TRACE(gNumPages);
TRACE(gPageLoc);
}
cout << "After folding paper. Paper dimensions: " << toString(gWidth, 6) <<
" X " << toString(gHeight, 6) << endl;
cout << "Dot is on " << (gTop ? "TOP" : "BOTTOM") << " of page " << gPageLoc << ". "
"Position " << toString(gX, 6) << " X " << toString(gY, 6) << endl << endl;
}
TRACE("end");
return 0;
}
[/cpp]
turingcomplete
New poster
Posts: 11
Joined: Wed Oct 09, 2002 7:31 pm
Contact:

Post by turingcomplete »

[cpp]
// Accepted

#include <iostream>
#include <cmath>
using namespace std;

int refreshPos(double dimX, double dimY, double x, double y, char InitialPos){
double half_dimX=dimX/2;
double half_dimY=dimY/2;
if(x<half_dimX && y<half_dimY && InitialPos=='T')
return 1;
if(x>half_dimX && y<half_dimY && InitialPos=='T')
return 2;
if(x<half_dimX && y>half_dimY && InitialPos=='T')
return 3;
if(x>half_dimX && y>half_dimY && InitialPos=='T')
return 4;
if(x<half_dimX && y<half_dimY && InitialPos=='B')
return 5;
if(x>half_dimX && y<half_dimY && InitialPos=='B')
return 6;
if(x<half_dimX && y>half_dimY && InitialPos=='B')
return 7;
if(x>half_dimX && y>half_dimY && InitialPos=='B')
return 8;
return 0;
}

void main(){
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(6);
unsigned long paper=1;

while(true){

double x,y,dimX,dimY;
char command1, command2, InitialPos;
int dotPos=0;
unsigned long page=1;


if(!(cin>>dimX))
break;

cin>>dimY;
cin>>x>>y>>InitialPos;
dotPos=refreshPos(dimX,dimY,x,y,InitialPos);
page=1;
int count=0;

cout<<"Paper number "<<paper<<endl;
cout<<"Beginning paper dimensions "<<dimX<< " X " <<dimY<<endl;
cout<<"Dot is on ";
if(InitialPos=='T')
cout<<"TOP";
else cout<<"BOTTOM";
cout<<" of page "<<page<<". Position: "<<x<<" X "<<y<<endl;
cout<<endl;

while(cin>>command1){

dotPos=refreshPos(dimX,dimY,x,y,InitialPos);

if(command1=='S')
break;

cin>>command2;

// Calc. new dimension
if(command1=='L' || command1=='R')
dimX/=2;
else dimY/=2;

if(command1=='L'){
if(command2=='U'){
switch(dotPos){
case 1:
case 3:
case 5:
case 7: x=dimX-x;
page=(pow(2,count)-page+1)+pow(2,count);
if( InitialPos == 'T' )
InitialPos='B';
else InitialPos='T';
break;
default: x-=dimX;
}
}else

if(command2=='O'){
switch(dotPos){
case 1:
case 3:
case 5:
case 7:
x=dimX-x;
page=(pow(2,count)-page+1);
if( InitialPos == 'T' )
InitialPos='B';
else InitialPos='T';
break;
default: x-=dimX;
page+=pow(2,count);
}
}
}

if(command1=='R'){
if(command2=='U'){
switch(dotPos){
case 2:
case 4:
case 6:
case 8:
x=dimX-(x-dimX);
page=(pow(2,count)-page+1)+pow(2,count);
if( InitialPos == 'T' )
InitialPos='B';
else InitialPos='T';
break;
default: ;
}
}else

if(command2=='O'){
switch(dotPos){
case 2:
case 4:
case 6:
case 8: x=dimX-(x-dimX);
page=(pow(2,count)-page+1);
if( InitialPos == 'T' )
InitialPos='B';
else InitialPos='T';
break;
default: page+=pow(2,count);
}
}
}

if(command1=='T'){
if(command2=='U'){
switch(dotPos){
case 1:
case 2:
case 5:
case 6: y=dimY-y;
page=(pow(2,count)-page+1)+pow(2,count);
if( InitialPos == 'T' )
InitialPos='B';
else InitialPos='T';
break;
default: y-=dimY;
}
}else

if(command2=='O'){
switch(dotPos){
case 1:
case 2:
case 5:
case 6: y=dimY-y;
page=(pow(2,count)-page+1);
if( InitialPos == 'T' )
InitialPos='B';
else InitialPos='T';
break;
default: y-=dimY;
page+=pow(2,count);
}
}
}

if(command1=='B'){
if(command2=='U'){
switch(dotPos){
case 3:
case 4:
case 7:
case 8: y=dimY-(y-dimY);
page=(pow(2,count)-page+1)+pow(2,count);
if( InitialPos == 'T' )
InitialPos='B';
else InitialPos='T';
break;
default: ;
}
}else

if(command2=='O'){
switch(dotPos){
case 3:
case 4:
case 7:
case 8: y=dimY-(y-dimY);
page=(pow(2,count)-page+1);
if( InitialPos == 'T' )
InitialPos='B';
else InitialPos='T';
break;
default: page+=pow(2,count);
}
}

}
count++;

}


cout<<"After folding paper. Paper dimensions: "<<dimX<< " X " <<dimY<<endl;
cout<<"Dot is on ";
if(InitialPos=='T')
cout<<"TOP";
else cout<<"BOTTOM";
cout<<" of page "<<page<<". Position "<<x<<" X "<<y<<endl;
cout<<endl;

paper++;
}

}[/cpp]
Sasha_gud
New poster
Posts: 1
Joined: Wed May 26, 2004 9:57 pm

733 WA

Post by Sasha_gud »

I checked a lot of input data on my computer but still get WA. Can anyone help?

[cpp]

#include <stdio.h>

struct TPoint {
float x;
float y;
int N; // paper number
char side;
};

struct TPaper {
float x_size;
float y_size;
int layers;
};

void rotate_CW (TPaper &paper, TPoint &point) {
float temp;
temp = point.y;
point.y = point.x;
point.x = paper.y_size - temp;

temp = paper.x_size;
paper.x_size = paper.y_size;
paper.y_size = temp;
}

void rotate_CCW (TPaper &paper, TPoint &point) {
float temp;
temp = point.x;
point.x = point.y;
point.y = paper.x_size - temp;

temp = paper.x_size;
paper.x_size = paper.y_size;
paper.y_size = temp;
}

void rotate_180 (TPaper &paper, TPoint &point) {
point.x = paper.x_size - point.x;
point.y = paper.y_size - point.y;
}

void fold_it (TPaper &paper, TPoint &point, char side, char dir) {
switch(side) {
case 'T': {
if (dir == 'O') { // folding top over
point.N = paper.layers - point.N + 1;
paper.y_size = paper.y_size / 2;
point.y = paper.y_size - point.y;
if (point.side == 'T') point.side = 'B';
else point.side = 'T';
}
else if (dir == 'U') { // folding top under
point.N = 2*paper.layers - point.N + 1;
paper.y_size /= 2;
point.y = paper.y_size - point.y;
if (point.side == 'T') point.side = 'B';
else point.side = 'T';
};
}
break;
case 'B': {
if (dir == 'O') { // folding bottom over
point.N = point.N + paper.layers;
paper.y_size /= 2;
}
else if (dir == 'U') { // folding bottom under
paper.y_size /= 2;
};
}
break;
case 'L': {
if (dir == 'O') { // folding left over
point.N = paper.layers - point.N + 1;
paper.x_size /= 2;
point.x = paper.x_size - point.x;
if (point.side == 'T') point.side = 'B';
else point.side = 'T';
}
else if (dir == 'U') { // folding left under
point.N = 2*paper.layers - point.N + 1;
paper.x_size /= 2;
point.x = paper.x_size - point.x;
if (point.side == 'T') point.side = 'B';
else point.side = 'T';
};
}
break;
case 'R': {
if (dir == 'O') { // folding right over
point.N = point.N + paper.layers;
paper.x_size /= 2;
}
else if (dir == 'U') { // folding right under
paper.x_size /= 2;
};
}
break;
};
paper.layers = paper.layers * 2;
}

void fold (TPaper &paper, TPoint &point, char side, char dir) {
if (point.x < paper.x_size/2) {
if (point.y < paper.y_size/2) { // 2nd quarter
fold_it(paper, point, side, dir);
}
else if (point.y > paper.y_size/2) { // 3rd quarter
rotate_CW(paper, point);
if (side == 'T') side = 'R';
else if (side == 'R') side = 'B';
else if (side == 'B') side = 'L';
else side = 'T';

fold_it(paper, point, side, dir);
rotate_CCW(paper, point);
};
}
else if (point.x > paper.x_size/2) {
if (point.y < paper.y_size/2) { // 1nd quarter
rotate_CCW(paper, point);
if (side == 'R') side = 'T';
else if (side == 'T') side = 'L';
else if (side == 'L') side = 'B';
else side = 'R';
fold_it(paper, point, side, dir);
rotate_CW(paper, point);
}
else if (point.y > paper.y_size/2) { // 4rd quarter
rotate_180(paper, point);
if (side == 'T') side = 'B';
else if (side == 'B') side = 'T';
else if (side == 'L') side = 'R';
else side = 'L';

fold_it(paper, point, side, dir);
rotate_180(paper, point);
};
};
};

void main()
{
TPoint point;
TPaper paper;
int N = 1;
char A, B;

while (scanf("%f %f", &(paper.x_size), &(paper.y_size))==2) {
// reading paper info
paper.layers = 1;

printf("Paper number %d\n", N);
printf("Beginning paper dimensions %f X %f\n", paper.x_size, paper.y_size);

// reading point info
scanf("%f %f %c\n", &(point.x), &(point.y), &(point.side));
point.N = 1;

printf("Dot is on ");
if (point.side == 'T') printf ("TOP");
else printf ("BOTTOM");
printf (" of page 1. Position: %f X %f\n\n", point.x, point.y);

scanf("%c", &A);
while ( A != 'S' ) {
scanf("%c\n", &B);
fold(paper, point, A, B);
scanf("%c", &A);
}

N++; // inc paper number

printf("After folding paper. Paper dimensions: %f X %f\n", paper.x_size, paper.y_size);
printf("Dot is on ");
if (point.side == 'T') printf("TOP");
else printf("BOTTOM");
printf(" of page %d. Position %f X %f\n\n", point.N, point.x, point.y);
};
}


[/cpp]
Post Reply

Return to “Volume 7 (700-799)”