Answer to Question #284269 in C++ for Zaidi

Question #284269

Suppose you are playing a killing game and have to kill few of your enemies. You are at a cliff



and from there you can see few of them. You see the enemies at your height only. Therefore,



you can only kill at the same height. The enemies are forming a balanced binary tree where



their General is at the top of the other cliff, his two commanders are on the first level below



him. The soldiers are at the lowest level. Total four soldiers are appointed. Under both the



commanders exact two soldiers serve.



You have to make a strategy to kill the left commander first, than whoever takes his position



kill him. Now start killing the right side with same strategy until only one enemy is left at left



side. Now increase your height and kill the General. You can only win if only three of your



enemies are left.



Display the enemies who are left and who have been killed. Don’t forget to print your victory



message.

1
Expert's answer
2022-01-03T04:27:44-0500
#include<iostream>
#include<string>

using namespace std;

struct Node
{
	string enemy;
	Node* left;
	Node*right;
	Node(string _enemy):enemy(_enemy),left(NULL),right(NULL){}
};

class EnemyTree {
public:
	Node* Head=NULL;
	int KillEnemy(string name, int& level)
	{
if (Head->enemy == name&&level==3)
	{
if (Head->left == NULL&&Head->right == NULL)
	{
	cout << Head->enemy<<" is killed!"
	<< "You are winner in a game";
	delete Head;
	Head = NULL;
return -1;
	}
else
	{
if (level == 3)
	{
	cout << "Incorrect enemy!";
	}
else
	cout << "You must killed another enemies before general!";
	}
	}
else if(level==2)
	{
if (Head->left&&Head->left->enemy == name)
	{
if (Head->left->left == NULL&&Head->left->right == NULL)
	{
	delete Head->left;
	Head->left = NULL;
	}
	}
else if(Head->right&&Head->right->enemy == name)
	{
if (Head->right->left == NULL&&Head->right->right == NULL)
	{
	delete Head->right;
	Head->right = NULL;
	}
	}
else
	{
	cout << "Incorrect enemy!\n";
	}
if (Head->left == NULL&&Head->right == NULL)
	level++;
	}
else if(level==1)
	{
if (Head->left->left&&Head->left->left->enemy == name)
	{
	delete Head->left->left;
	Head->left->left = NULL;
	}
else if (Head->left->right&&Head->left->right->enemy == name)
	{
	delete Head->left->right;
	Head->left->right = NULL;
	}
else if (Head->right->left&&Head->right->left->enemy == name)
	{
	delete Head->right->left;
	Head->right->left = NULL;
	}
else if (Head->right->right&&Head->right->right->enemy == name)
	{
	delete Head->right->right;
	Head->right->right = NULL;
	}
else
	cout << "Incorrect enemy!\n";
if (Head->left->left == NULL&&Head->left->right == NULL
	&&Head->right->left == NULL&&Head->right->right == NULL)
	{
	level++;
	}
	}
return 0;
	}
	void DisplayTree()
	{
	Node* p = Head;
if (p != NULL)
	cout << "\t\t\t" << p->enemy << endl;
else
	{
return;
	}
if (p->left != NULL || p->right != NULL)
	{
	cout << "\t" << (p->left ? p->left->enemy : "killed") << "\t\t"
	<< (p->right ? p->right->enemy : "killed") << endl;
	}
else
	{
	cout << "\tkilled\t\tkilled\n";
	}
if (p->left && (p->left->left != NULL || p->left->right != NULL))
	{
	cout << (p->left->left ? p->left->left->enemy : "killed") << "\t"
	<< (p->left->right ? p->left->right->enemy : "killed") << "\t\t";
	}
else
	{
	cout << "killed\tkilled\t\t";
	}
if (p->right && (p->right->left != NULL || p->right->right != NULL))
	{
	cout << (p->right->left ? p->right->left->enemy : "killed") << "\t"
	<< (p->right->right ? p->right->right->enemy : "killed") << endl;
	}
else
	{
	cout << "killed\tkilled";
	}
	}
};


int main()
{
//Make enemy tree
	EnemyTree et;
	et.Head = new Node("General");
	et.Head->left= new Node("Lcommander");
	et.Head->right = new Node("Rcommander");
	et.Head->left->left = new Node("LLsoldier");
	et.Head->left->right = new Node("LRsoldier");
	et.Head->right->left = new Node("RLsoldier");
	et.Head->right->right = new Node("RRsoldier");
	int level = 1;
	int res = 0;
	string enm;
do
	{
	et.DisplayTree();
	cout << "\nPlease, enter an enemy to kill (0-exit program): ";
	cin >> enm;
if (enm == "0")break;
	res=et.KillEnemy(enm,level);
	} while (res!=-1);
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog