convert c 2b 2b code to python online

Solutions on MaxInterview for convert c 2b 2b code to python online by the best coders in the world

showing results for - "convert c 2b 2b code to python online"
Mattia
08 Feb 2018
1// Program to print path from root node to destination node
2// for N*N -1 puzzle algorithm using Branch and Bound
3// The solution assumes that instance of puzzle is solvable
4#include <bits/stdc++.h>
5using namespace std;
6#define N 3
7
8// state space tree nodes
9struct Node
10{
11	// stores the parent node of the current node
12	// helps in tracing path when the answer is found
13	Node* parent;
14
15	// stores matrix
16	int mat[N][N];
17
18	// stores blank tile coordinates
19	int x, y;
20
21	// stores the number of misplaced tiles
22	int cost;
23
24	// stores the number of moves so far
25	int level;
26};
27
28// Function to print N x N matrix
29int printMatrix(int mat[N][N])
30{
31	for (int i = 0; i < N; i++)
32	{
33		for (int j = 0; j < N; j++)
34			printf("%d ", mat[i][j]);
35		printf("\n");
36	}
37}
38
39// Function to allocate a new node
40Node* newNode(int mat[N][N], int x, int y, int newX,
41			int newY, int level, Node* parent)
42{
43	Node* node = new Node;
44
45	// set pointer for path to root
46	node->parent = parent;
47
48	// copy data from parent node to current node
49	memcpy(node->mat, mat, sizeof node->mat);
50
51	// move tile by 1 position
52	swap(node->mat[x][y], node->mat[newX][newY]);
53
54	// set number of misplaced tiles
55	node->cost = INT_MAX;
56
57	// set number of moves so far
58	node->level = level;
59
60	// update new blank tile cordinates
61	node->x = newX;
62	node->y = newY;
63
64	return node;
65}
66
67// bottom, left, top, right
68int row[] = { 1, 0, -1, 0 };
69int col[] = { 0, -1, 0, 1 };
70
71// Function to calculate the number of misplaced tiles
72// ie. number of non-blank tiles not in their goal position
73int calculateCost(int initial[N][N], int final[N][N])
74{
75	int count = 0;
76	for (int i = 0; i < N; i++)
77	for (int j = 0; j < N; j++)
78		if (initial[i][j] && initial[i][j] != final[i][j])
79		count++;
80	return count;
81}
82
83// Function to check if (x, y) is a valid matrix cordinate
84int isSafe(int x, int y)
85{
86	return (x >= 0 && x < N && y >= 0 && y < N);
87}
88
89// print path from root node to destination node
90void printPath(Node* root)
91{
92	if (root == NULL)
93		return;
94	printPath(root->parent);
95	printMatrix(root->mat);
96
97	printf("\n");
98}
99
100// Comparison object to be used to order the heap
101struct comp
102{
103	bool operator()(const Node* lhs, const Node* rhs) const
104	{
105		return (lhs->cost + lhs->level) > (rhs->cost + rhs->level);
106	}
107};
108
109// Function to solve N*N - 1 puzzle algorithm using
110// Branch and Bound. x and y are blank tile coordinates
111// in initial state
112void solve(int initial[N][N], int x, int y,
113		int final[N][N])
114{
115	// Create a priority queue to store live nodes of
116	// search tree;
117	priority_queue<Node*, std::vector<Node*>, comp> pq;
118
119	// create a root node and calculate its cost
120	Node* root = newNode(initial, x, y, x, y, 0, NULL);
121	root->cost = calculateCost(initial, final);
122
123	// Add root to list of live nodes;
124	pq.push(root);
125
126	// Finds a live node with least cost,
127	// add its childrens to list of live nodes and
128	// finally deletes it from the list.
129	while (!pq.empty())
130	{
131		// Find a live node with least estimated cost
132		Node* min = pq.top();
133
134		// The found node is deleted from the list of
135		// live nodes
136		pq.pop();
137
138		// if min is an answer node
139		if (min->cost == 0)
140		{
141			// print the path from root to destination;
142			printPath(min);
143			return;
144		}
145
146		// do for each child of min
147		// max 4 children for a node
148		for (int i = 0; i < 4; i++)
149		{
150			if (isSafe(min->x + row[i], min->y + col[i]))
151			{
152				// create a child node and calculate
153				// its cost
154				Node* child = newNode(min->mat, min->x,
155							min->y, min->x + row[i],
156							min->y + col[i],
157							min->level + 1, min);
158				child->cost = calculateCost(child->mat, final);
159
160				// Add child to list of live nodes
161				pq.push(child);
162			}
163		}
164	}
165}
166
167// Driver code
168int main()
169{
170	// Initial configuration
171	// Value 0 is used for empty space
172	int initial[N][N] =
173	{
174		{1, 2, 3},
175		{5, 6, 0},
176		{7, 8, 4}
177	};
178
179	// Solvable Final configuration
180	// Value 0 is used for empty space
181	int final[N][N] =
182	{
183		{1, 2, 3},
184		{5, 8, 6},
185		{0, 7, 4}
186	};
187
188	// Blank tile coordinates in initial
189	// configuration
190	int x = 1, y = 2;
191
192	solve(initial, x, y, final);
193
194	return 0;
195}
196
Lennard
07 Nov 2016
1#include <stdio.h>
2
3int main()
4{
5    printf("Hello World");
6
7    return 0;
8}