forked from ZoranPandovski/al-go-rithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from ZoranPandovski/master
Update fork from original
- Loading branch information
Showing
445 changed files
with
21,186 additions
and
161 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
#include <fstream> | ||
#include <iostream> | ||
#include <vector> | ||
#define BOARD_SIZE 9 | ||
|
||
// rows[i] shows either -1 if empty or colum number of placed queen | ||
int rows[BOARD_SIZE]; | ||
// cols[i] shows either -1 if ith colum has no queens or 1 if there is a queen | ||
int cols[BOARD_SIZE]; | ||
|
||
// check positions by order of increasing colum | ||
bool check_forwards(int row, int col){ | ||
int tmp_row = row; int tmp_col = col; | ||
while((row < BOARD_SIZE) && (col < BOARD_SIZE)){ | ||
if(rows[row] == col) | ||
return true; | ||
row++; | ||
col++; | ||
} | ||
while((tmp_row >= 0) && (tmp_col < BOARD_SIZE)){ | ||
if(rows[tmp_row] == tmp_col) | ||
return true; | ||
tmp_col++; | ||
tmp_row--; | ||
} | ||
return false; | ||
} | ||
// check positions by order of decreasing colum | ||
bool check_backwards(int row, int col){ | ||
int tmp_row = row; int tmp_col = col; | ||
while((row >= 0) && (col >= 0)){ | ||
if(rows[row] == col) | ||
return true; | ||
row--; | ||
col--; | ||
} | ||
while((tmp_row < BOARD_SIZE) && (tmp_col >= 0)){ | ||
if(rows[tmp_row] == tmp_col) | ||
return true; | ||
tmp_col--; | ||
tmp_row++; | ||
} | ||
return false; | ||
} | ||
|
||
bool attacked(int row, int col){ | ||
// check backwards | ||
if(cols[col] != -1) return true; | ||
if(rows[row] != -1) return true; | ||
//std::cout<<row<<col<<std::endl; | ||
return (check_backwards(row,col) || check_forwards(row,col)); | ||
} | ||
|
||
void place(int row, int col){ | ||
rows[row] = col; | ||
cols[col] = 1; | ||
} | ||
|
||
void remove(int row, int col){ | ||
rows[row] = -1; | ||
cols[col] = -1; | ||
} | ||
|
||
void search(int col,int placed){ | ||
if(placed == BOARD_SIZE){ | ||
for(int i =0; i<BOARD_SIZE;i++){ | ||
std::cout<<"row:"<<i<<" col:"<<rows[i]<<std::endl; | ||
} | ||
} | ||
for(int i =0; i<BOARD_SIZE; i++){ | ||
for(int j = col; j<BOARD_SIZE; j++){ | ||
if(!attacked(i,j)){ | ||
place(i,j); | ||
search(col+1,placed+1); | ||
remove(i,j); | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
||
int main(){ | ||
for(int i =0; i<BOARD_SIZE; i++){ | ||
rows[i] = -1; | ||
cols[i] = -1; | ||
} | ||
search(0,0); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#include <iostream> | ||
#include <string> | ||
#include <algorithm> | ||
using namespace std; | ||
|
||
void swap(string &s, int i, int j) { | ||
char temp = s[i]; | ||
s[i] = s[j]; | ||
s[j] = temp; | ||
} | ||
|
||
void permute(string &s, int level, int &num) { | ||
if(level == s.size()) { | ||
cout<<num<<". "<<s<<endl; | ||
num++; | ||
} | ||
else { | ||
for(int j=level;j<s.size();j++) { | ||
swap(s,level,j); | ||
permute(s,level+1,num); | ||
swap(s,level,j); | ||
} | ||
} | ||
} | ||
|
||
int main() { | ||
string s; int c = 1; | ||
cin>>s; | ||
sort(s.begin(),s.end()); | ||
permute(s,0,c); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
## Generate all permutations of a string ## | ||
|
||
Given a string with all distinct characters, the task is to | ||
generate all permutations of the string. For a n-letter string, there should | ||
be n! strings as output. For repeating characters, there are corresponding duplicates. | ||
|
||
An example of the program: | ||
Input: | ||
abc | ||
|
||
Output: | ||
1. abc | ||
2. acb | ||
3. bac | ||
4. bca | ||
5. cba | ||
6. cab |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
|
||
#include <iostream> | ||
#define N 9 | ||
using namespace std; | ||
|
||
int maze[N][N]; | ||
int path[N][N] = {0}; | ||
int direction[4][2] = { {0,1}, {1,0}, {0,-1}, {-1,0} }; | ||
int src_x, src_y, dest_x, dest_y; | ||
|
||
bool is_valid_move (int x, int y) { | ||
return (x >= 0 && x < N && y >= 0 && y < N && maze[x][y] == 0); | ||
} | ||
|
||
// this will not give the shortest path but only give one of the possible paths | ||
|
||
bool traverse (int curr_x, int curr_y) { | ||
if (curr_x == dest_x && curr_y == dest_y) | ||
return true; | ||
for (int k = 0; k < 4; k++) { | ||
int next_x = curr_x + direction[k][0]; | ||
int next_y = curr_y + direction[k][1]; | ||
if (is_valid_move (next_x, next_y)) { | ||
maze[curr_x][curr_y] = 1; // as the rat has already traversed this path marking it as a block to prevent rat from moving backwards | ||
path[next_x][next_y] = 1; | ||
if (traverse (next_x, next_y) == true) | ||
return true; | ||
else { | ||
maze[curr_x][curr_y] = 0; | ||
path[next_y][next_y] = 0; | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
int main() { | ||
cout << "Enter the maze (0 for empty, 1 for blocked square, 5 for source and 6 for destination) : \n"; | ||
for (int i = 0; i < N; i++) { | ||
for (int j = 0; j < N; j++) { | ||
cin >> maze[i][j]; | ||
if (maze[i][j] == 5) { | ||
src_x = i; | ||
src_y = j; | ||
} | ||
if (maze[i][j] == 6) { | ||
dest_x = i; | ||
dest_y = j; | ||
} | ||
} | ||
} | ||
path[src_x][src_y] = 1; | ||
maze[src_x][src_y] = 0; | ||
maze[dest_x][dest_y] = 0; | ||
if (! traverse (src_x, src_y)) { | ||
cout << "Sorry! The rat cannot reach the destination..."; | ||
return 0; | ||
} | ||
cout << "The path followed by the rat to reach the destination is : \n"; | ||
for (int i = 0; i < N; i++) { | ||
for (int j = 0; j < N; j++) | ||
cout << path[i][j] << " "; | ||
cout << endl; | ||
} | ||
return 0; | ||
} |
Oops, something went wrong.