# | Problem | Pass Rate (passed user / total user) |
---|---|---|
12257 | Only children make choice! |
|
13522 | Infinitely Big Integer: Darray Implementation |
|
13523 | Infinitely Big Integer: Big Integer Implementation |
|
14281 | Polymorphic Run-Length Encoding Class |
|
14618 | Caillou's Tetris game (class) |
|
14620 | Polymorphic Pacman |
|
Description
"Cat or Dog? that's a good question. " Shakespeare never said, 2019.
In this questions there are three types of animals class have to be implemented. (Cat , Dog and Caog)
Coag is a kind of special animal mixed in Cat and Dog.
Dog can only throwball
Cat can only play with carton.
Caog do those things both.
when Dog / Caog playing throwball output "it looks happy!\n"
when Cat / Caog playing with carton output "it looks so happy!\n"
and also there's a zoo can count how many animals are in the zoo.
In this questions you have to implement 3 class (cat , dog and caog) based on the sample code.
Input
All input data would be finish in given main functions.
First number N would be N animals ( N < 10)
the following Ai numbers would types of animals.
And the T would be T instructions ( T < 30)
the following Ti would be index and instructions
Output
When Animals born Zoo will auto output which kind of animal is born and how many animals in the zoo.
When Animals dead Zoo will auto output which kind of animal isdeadand how many animals in the zoo.
when Dog / Caog playing throwball output "it looks happy!\n"
when Cat / Caog playing with carton output "it looks so happy!\n"
when Barking:
Cat would "meow!\n"
Dog would "woof!\n"
Caog would "woof!woof!meow!\n"
Sample Input Download
Sample Output Download
Partial Judge Code
12257.cppPartial Judge Header
12257.hTags
Discuss
Description
The problem "Infinitely Big Integer" is splitted into two subproblems:
- Darray Implementation (3/10)
- Big Integer Implementation (7/10)
You should solve this subproblem first.
Based on the definition of Dynamic Array in Problem 13520 (nthu.edu.tw), you need to implement a dynamic array with the following functions:
- int& operator[](int): access data like using array. Users and main.cpp should/will not access the index which is greater or equal to size.
- void pushback(int x): append the element x
- void popback(void): pop the last element. Don't do anything if the array is empty.
- void clear(void): clear the array (set size to 0) so that the next pushbacks will place elements in data[0],data[1] and so on.
- int length(void): return the current size.
- void resize(void): double the capacity and copy the data.
- ~Darray(): destructor
Note that main.cpp acts like a functional tester for your Darray. There's no need to understand it. You should test your Darray by yourself. Furthermore, a new function void popback(void)
is introduced in this problem.
class Darray {
public:
Darray() {
capacity = 100;
size = 0;
data = new int[capacity];
};
~Darray();
int& operator[](int);
void pushback(int x);
void popback(void);
void clear(void);
int length(void);
private:
void resize(void); // double the capacity
int *data;
int capacity;
int size;
};
Darray arr;
for (int i = 0; i < 5; i++) arr.pushback(i*i);
arr[2] += 100 + arr[3];
for (int i = 0; i < arr.length(); i++)
cout << arr[2] << ' '; // Print: 0 1 113 9 16
cout << endl << arr.length() << endl; // Print: 5
arr.clear();
cout << arr.length() << endl; // Print: 0
arr.pushback(9487); arr.pushback(9487);
arr.popback();
cout << arr.length() << ' ' << arr[0] << endl; // Print: 1 9487
std::vector
is prohibited (both subproblems 1 and 2) and will result in 0 score if you use it.
Input
This is handled by the main function.
1/3 of the test cases will not test popback()
. If you have problems with it, try writing an empty popback function so that it can be compiled successfully.
void popback() {
}
Output
This is handled by the main function.
Sample Input Download
Sample Output Download
Partial Judge Code
13522.cppPartial Judge Header
13522.hTags
Discuss
Description
The problem "Infinitely Big Integer" is splitted into two subproblems:
- Darray Implementation (3/10)
- Big Integer Implementation (7/10)
You should solve the former, Darray Implementation, first.
You may have practiced the problem Big Integer before. Instead of using traditional int, we store the integer in a char array. However, it still has size limitation because we declare a static array for the class. In this problem, we'll use the Darray in problem 13522 - Infinitely Big Integer: Darray Implementation as the data array in class INT to handle arbitary large numbers as long as the memory of the computer is enough.
The following is the structure of INT.
public:
void operator+=(INT&);
friend std::istream &operator>>(std::istream &, INT &);
friend std::ostream &operator<<(std::ostream &, INT &);
private:
Darray data;
};
For simplicity, you only need to implement three functions, <<, >> and +=. The streaming operators are used for cin and cout. The operator += does the same as the traditional operator +=. For instance, A += B, A will be the value of A+B and B remains the same. Note that all the parameters are passed by reference, which is different from the previous problem.
Your function.cpp should contain the implementation of both Darray and INT. If you passed the subproblem 1 first, just copy and paste the Darray. For example:
#include "function.h"
// Darray
void Darray::pushback(int x) { ... }
...
// INT
void INT::operator+= (INT &b) { ... }
...
It is okay if you have problems with the popback
function in subproblem 1 as long as the Darray has basic functionality to pass the test cases, because Darray in INT is fully controlled by you.
The following is an alternative main.cpp to test your code:
#include "function.h"
using namespace std;
int main() {
INT a, b;
cin >> a;
cin >> b;
a += b;
cout << "a + b = " << a << endl;
return 0;
}
Input
Output
Sample Input Download
Sample Output Download
Partial Judge Code
13523.cppPartial Judge Header
13523.hTags
Discuss
Description
The task is to define the class ‘RleCodec’ for run-length encoding:
The rule of run-length encoding is simple: Count the number of consecutive repeated characters in a string, and replace the repeated characters by the count and a single instance of the character. For example, if the input string is ‘AAADDDDDDDBBGGGGGCEEEE’, its run-length encoding will be ‘3A7DBB5GC4E’, because there are three A’s, seven D’s, … etc. Note that we do not need to encode runs of length one or two, since ‘2B’ and ‘1C’ are not shorter than ‘BB’ and ‘C’.
Abstract base class ‘Codec’
We first design the abstract base class ‘Codec’ as an interface, which contains two pure virtual functions encode() and decode():
class Codec { public: Codec(std::string s): encoded{false}, code_str{s} { } virtual ~Codec() { } // virtual destructor; do nothing virtual void encode() = 0; virtual void decode() = 0; void show(std::ostream& os) const { os << code_str; } bool is_encoded() const { return encoded; } protected: bool encoded; std::string code_str; };
TODOs: In ‘function.cpp’, please
- define two concrete derived classes from the abstract base class ‘Codec’ by overriding the two pure virtual functions:
- class DummyCodec: simply updating the coding status in encode() and decode() accordingly, and doing nothing else;
- class RleCodec: for the run-length encoding explained above;
- implement the function getCodec(), which returns the address of a Codec object according to the parameter type, where the type of the Codec can be "Dummy" or "RLE".
Codec* getCodec(const std::string& type, const std::string& is);
For more information, you should refer to the main.cpp and function.h.
Note
It is highly recommended to practice std::stringstream in this problem. Here is a simple code that shows you how to use stringstream:
#include <iostream>
#include <sstream> // header file for stringstream
using namespace std;
int main(){
string s;
stringstream ss;
getline(cin, s);
ss << s;
while(!ss.eof()){
string t;
getline(ss, t, ' ');
cout << t << "\n";
}
return 0;
}
/*
- Sample Input
This is a sentence for testing this code.
- Sample Output
This
is
a
sentence
for
testing
this
code.
*/
For more information, please refer to this article.
Input
The input contains a single line that consists of several characters.
Output
There are four lines.
The first and second lines are dummy encoding and decoding.
The third and fourth lines are RLE encoding and decoding.
Each line is followed by a new line character.
Sample Input Download
Sample Output Download
Partial Judge Code
14281.cppPartial Judge Header
14281.hTags
Discuss
Description
CSSA's most handsome president Chang Cailou recently felt that riding motorcycles outdoors was too boring, so he developed a special Tetris game and would like you to help him test this game.
The special Tetris game screen has 10 rows and 15 columns. Every second, a block will fall until it reaches the bottom or rests on another stationary block.
When all cells in a row are filled with stationary blocks, that row will be eliminated, and all blocks above will move down one row.
The game lasts for T seconds, during which 1 to N blocks will be placed. Each block will be placed into the game at a designated time point M, consisting of a 3x3 grid with its top-left corner at coordinates (X, Y) and identified by block type P. All blocks are guaranteed to have sufficient space for placement without exceeding game boundaries.
The K-th placed block is labeled with its placement order number K, where 0 indicates no block at the current position. Output the game screen at the time the game ends.
There are six types of block:
- IBlock
K00
K00
K00 - JBlock
KK0
0K0
0K0 - LBlock
K00
K00
KK0 - SBlock
0KK
KK0
000 - TBlock
KKK
0K0
000 - OBlock
K00
000
000
This problem is a partial judge.
Please implement functions defined in the header file.
All classes that inherit from Tetrominoes
- draw: place the block onto the scene
Scene
- Scene: constructor
- ~Scene: destructor
- update: Update the game state to reflect the situation at P-th second.
- checkBlock: Check whether the block is a stationary block.
- checkRow: Check whether the row needs to be eliminated.
- eliminate: Eliminate the row.
- print: Print the game screen.
Input
The first line contains three integers N, T, indicating N blocks will be placed during the game and the game lasts for T seconds.
The next N lines contain four integers Mi , Pi , Xi , Yi , indicating the i-th block will be placed at Mi seconds into the game, in a 3×3 grid with top-left corner at coordinates (Xi , Yi ) . Each placed block is guaranteed to have enough space for placement. The integer Pi indicates the block type.
Constraints
- 1 ≤ T ≤ 500
- 1 ≤ N, M ≤ T
- 1 ≤ P ≤ 6
- 1 ≤ X ≤ 10
- 1 ≤ Y ≤ 15
Subtasks
- Testcases 1 ~ 3: N = 1
- Testcases 4 ~ 6: No elimination operation
- Testcases 7 ~ 9: No additional restrictions
Output
The K-th placed block is labeled with its placement order number K, where 0 indicates no block at the current position. Output the game screen at the time the game ends.
Please remember to print "\n" at the end.
Sample Input Download
Sample Output Download
Partial Judge Code
14618.cppPartial Judge Header
14618.hTags
Discuss
Description
As a computer science student, one day you wake up and find yourself trapped inside the world of Pacman! You're stuck in a maze ( N × M ), relentlessly pursued by ghosts. Your mission is to eat all the dots scattered around the maze without being caught by the ghosts. Only after successfully clearing the dots can you escape safely back to your own world.
Task Implementation
You must modify the provided code at each part marked "TODO" to help Pacman evade the ghosts and clear all the dots.
Every round, your Pacman can:
-
Move to any of the four adjacent cells (up, down, left, right) that aren't walls.
-
Choose to stay still and not move this round.
Ghost Movement Logic
(Already implemented; modifying it is not recommended)
-
Ghost has a 50% chance to move randomly to an adjacent cell.
-
Ghost also has a 50% chance to move one step towards Pacman.
-
Ghost never remain stationary.
Important Notes
-
Avoid changing parts of the code not marked by "TODO" unless you're absolutely sure what you're doing.
-
Modifying ghost movement logic to avoid capture is strongly discouraged. The judge system will recreate ghost paths based on your provided random seed. If your output differs from the judge's expected ghost path, you will receive a "Wrong Answer."
Constraints
-
1 ≤ N, M ≤ 20
-
5 × N × M ≤ T ≤ 10 × N × M
-
It is guaranteed that no cell in the map is enclosed by walls on three sides (meaning every cell has at least two accessible paths).
-
It is guaranteed that Pacman can always clear all dots.
Subtasks
- Testcases 1 ~ 3: Ensures that ghost cannot touch pacman.
- Testcases 4 ~ 6: Except for the surrounding walls, no other grids are walls.
- Testcases 7 ~ 8: There is exactly one dot.
- Testcases 9 ~ 10: No additional restrictions
Testing on Your Computer
-
Uncomment the following two lines inside
int main()
in the provided code:// freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout);
-
Place your input data into a file named
input.txt
(make sure it’s in the same directory as your program). -
After execution, your program's output will be stored in
output.txt
.
Code
#include <bits/stdc++.h>
using namespace std;
int T, R, C;
vector<string> grid;
struct Position {
int x, y;
int operator-(const Position& other) const {
// TODO-01:
// Return the Manhattan distance between two coordinates (i.e., the sum of the differences of their x and y coordinates).
}
};
class Entity {
public:
Entity(Position pos): pos(pos) {}
virtual ~Entity() = default;
virtual void move() = 0;
Position getPosition() const { return pos; }
protected:
Position pos;
};
class Pacman : public Entity {
public:
Pacman(Position pos): Entity(pos) {}
void setGhostPos(const Position& pos) {
ghostPosition = pos;
}
void move() override {
// TODO-02:
// Implement how Pacman moves in each round.
}
private:
// TODO (optional):
// You can add some extra code here.
Position ghostPosition;
};
class Ghost : public Entity {
public:
Ghost(Position pos): Entity(pos){}
void setPacmanPos(const Position& pos) {
pacmanPosition = pos;
}
void move() override {
vector<Position> nbrs;
static const int dxs[4] = {-1, 1, 0, 0};
static const int dys[4] = {0, 0, -1, 1};
for (int i = 0; i < 4; i++) {
int nx = pos.x + dxs[i];
int ny = pos.y + dys[i];
if (nx < 0 || nx >= R || ny < 0 || ny >= C) continue;
if (grid[nx][ny] == '#') continue;
nbrs.push_back({nx, ny});
}
if (nbrs.empty()) return;
Position chosen = pos;
if (!nbrs.empty()) {
double p = (double)rand() / RAND_MAX;
if (p < 0.5) {
int best = INT_MAX;
for (Position& c : nbrs) {
int d = c - pacmanPosition;
if (d < best) {
best = d;
chosen = c;
}
}
} else {
int idx = rand() % nbrs.size();
chosen = nbrs[idx];
}
}
pos = chosen;
}
private:
Position pacmanPosition;
};
ostream& operator<<(ostream& os, const Position& p) {
os << p.x << ' ' << p.y;
return os;
}
ostream& operator<<(ostream& os, const Entity& e) {
os << e.getPosition();
return os;
}
// TODO (optional):
// You can add some extra code here.
class GameMap {
public:
GameMap(const vector<string>& g) {
// TODO-03:
// Store the dots from the map.
}
void eatDot(const Position& p) {
// TODO-04:
// Make Pacman eat the dot at his current position (if there is one).
}
};
int main() {
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
cin >> T >> R >> C;
grid.resize(R);
for (int i=0; i<R; i++) cin >> grid[i];
Position pacman_start, ghost_start;
for (int i=0; i<R; i++) for (int j=0; j<C; j++) {
if (grid[i][j]=='P') pacman_start = {i,j};
if (grid[i][j]=='G') ghost_start = {i,j};
}
unsigned GHOST_SEED = /* TODO-05: */ ;
// Choose any integer you like as a random seed! It will affect how the ghosts move.
// Note: make sure the number you pick fits within the range of an unsigned integer (0 to 4,294,967,295),
// or unexpected results may happen.
srand((unsigned)GHOST_SEED);
GameMap gameMap(grid);
Pacman pacman(pacman_start);
Ghost ghost(ghost_start);
for (int t=0; t<T; t++) {
pacman.setGhostPos(ghost.getPosition());
pacman.move();
gameMap.eatDot(pacman.getPosition());
ghost.setPacmanPos(pacman.getPosition());
ghost.move();
cout << pacman << ' ' << ghost << '\n';
}
cout << GHOST_SEED <<'\n';
return 0;
}
Pacman Simulation Visualizer
A visualization tool is provided to help you debug your solution.
Paste your program's input and output into the provided text fields, then click "Load Data" and "Play".
You'll see Pacman and the ghost move according to your data, along with the current game state (win/lose/invalid).
You can also generate custom test cases by entering a seed number and clicking "Generate Input."
Note! The "invalid" status in the Pacman Simulation Visualizer does not check whether the ghost's movement path is valid.
Input
(Input handling is already implemented in the provided code.)
The first line contains three integers N, M, and T, representing the dimensions of the maze ( N × M ) and the number of rounds T
The next N lines each contain M characters ci,j , where:
-
ci,j =
'#'
represents a wall. -
ci,j =
'.'
represents an empty space. -
ci,j =
'O'
represents a dot. -
ci,j =
'P'
represents Pacman's initial position. -
ci,j =
'G'
represents a ghost's initial position.
Output
(Output handling is already implemented in the provided code.)
Output ( T+1 ) lines:
-
For the first T lines, each line should contain four integers Px, Py, Gx, Gy, representing Pacman's position (Px, Py) and the ghost's position (Gx, Gy) at each round from 1 to T.
-
The ( T+1 )th line should output your chosen random seed.
If multiple valid solutions exist, outputting any one of them will be accepted.