Last active
September 17, 2019 13:59
-
-
Save ChlorUpload/6746da25b38de35919063c0a9f7ee366 to your computer and use it in GitHub Desktop.
find the number of fastest path in n by m matrix ( 0 is way, 1 is obstacle )
This file contains hidden or 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
8 8 | |
0 1 0 0 0 0 0 0 | |
0 1 1 1 0 1 1 0 | |
0 0 0 0 0 0 0 0 | |
1 1 1 0 1 1 1 0 | |
0 0 0 0 1 0 0 0 | |
1 1 0 1 1 1 0 1 | |
0 0 0 1 1 1 0 1 | |
0 0 0 0 0 0 0 0 |
This file contains hidden or 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
//use StackQueue.hpp in my gist | |
#include "StackQueue.hpp" | |
class Coord | |
{ | |
public: | |
int x; | |
int y; | |
}; | |
const int maxsize = 100; | |
int data[maxsize][maxsize]; | |
bool CheckCoordInOnBox(Coord coord, int x1, int y1, int x2, int y2) | |
{ | |
return (x1 <= coord.x && coord.x <= x2 && | |
y1 <= coord.y && coord.y <= y2); | |
} | |
int BFS(int startx, int starty, int width, int height) | |
{ | |
Coord curpos = { startx, starty }; | |
Coord temp; | |
Queue<Coord> q; | |
q.Push(curpos); | |
int count = 0; | |
while (!q.IsEmpty()) | |
{ | |
curpos = q.Pop(); | |
if (curpos.x == width - 1 && curpos.y == height - 1) | |
{ | |
count++; | |
} | |
data[curpos.y][curpos.x] = -1; | |
//right | |
temp.x = curpos.x + 1; | |
temp.y = curpos.y; | |
if (CheckCoordInOnBox(temp, 0, 0, width-1, height-1)) | |
{ | |
if (data[temp.y][temp.x] == 0) | |
{ | |
q.Push(temp); | |
} | |
} | |
//top | |
temp.x = curpos.x; | |
temp.y = curpos.y-1; | |
if (CheckCoordInOnBox(temp, 0, 0, width - 1, height - 1)) | |
{ | |
if (data[temp.y][temp.x] == 0) | |
{ | |
q.Push(temp); | |
} | |
} | |
//left | |
temp.x = curpos.x - 1; | |
temp.y = curpos.y; | |
if (CheckCoordInOnBox(temp, 0, 0, width - 1, height - 1)) | |
{ | |
if (data[temp.y][temp.x] == 0) | |
{ | |
q.Push(temp); | |
} | |
} | |
//bottom | |
temp.x = curpos.x; | |
temp.y = curpos.y+1; | |
if (CheckCoordInOnBox(temp, 0, 0, width - 1, height - 1)) | |
{ | |
if (data[temp.y][temp.x] == 0) | |
{ | |
q.Push(temp); | |
} | |
} | |
} | |
return count; | |
} | |
int main(void) | |
{ | |
FILE* fp; | |
freopen_s(&fp, "input.txt", "rt", stdin); | |
int width, height; | |
scanf_s("%d %d", &width, &height); | |
if (width > maxsize || height > maxsize) | |
{ | |
return -1; | |
} | |
for (int i = 0; i < height; i++) | |
{ | |
for (int j = 0; j < width; j++) | |
{ | |
scanf_s("%d", &data[i][j]); | |
} | |
} | |
printf("%d", BFS(0, 0, width, height)); | |
return 0; | |
} |
This file contains hidden or 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
//Copyright 2019. Song, Dae-geon | |
#ifndef __H_STACKQUEUE__ | |
#define __H_STACKQUEUE__ | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <type_traits> | |
#include <string> | |
#define MAX 128 | |
template <typename T> | |
class Stack { | |
private: | |
int _max; | |
int _iter; | |
T* _data; | |
//initial allocation for data | |
void _Alloc() | |
{ | |
_data = (T*)malloc((size_t)(sizeof(T) * _max)); | |
} | |
void _DeAlloc() | |
{ | |
free(_data); | |
} | |
public: | |
//getters and setters | |
void max(int max) | |
{ | |
_max = max; | |
} | |
int max() | |
{ | |
return _max; | |
} | |
//constructor | |
Stack() : _max(MAX), _iter(0) | |
{ | |
_Alloc(); | |
} | |
Stack(int max) : _max(max), _iter(0) | |
{ | |
_Alloc(); | |
} | |
~Stack() | |
{ | |
_DeAlloc(); | |
} | |
//methods | |
bool Push(T datum) | |
{ | |
if (_iter >= _max) | |
return false; | |
if constexpr (std::is_scalar_v<T>) | |
{ | |
new(_data + _iter++) T(datum); | |
} | |
else | |
{ | |
_data[_iter++] = datum; | |
} | |
return true; | |
} | |
T Pop() | |
{ | |
if (IsEmpty()) | |
{ | |
throw std::_Xruntime_error; | |
} | |
if constexpr (std::is_scalar_v<T>) | |
{ | |
T temp = std::move(_data[_iter]); | |
_data[_iter--].~T(); | |
return temp; | |
} | |
else | |
{ | |
return _data[_iter--]; | |
} | |
} | |
bool IsEmpty() | |
{ | |
return (_iter <= 0); | |
} | |
}; | |
template <typename T> | |
class Queue { | |
private: | |
int _max; | |
int _head; | |
int _tail; | |
T* _data; | |
//initial allocation for data | |
void _Alloc() | |
{ | |
_data = (T*)malloc((size_t)(sizeof(T) * _max)); | |
} | |
void _DeAlloc() | |
{ | |
free(_data); | |
} | |
public: | |
//getters and setters | |
void max(int max) | |
{ | |
_max = max; | |
} | |
int max() | |
{ | |
return _max; | |
} | |
//constructor | |
Queue() : _max(MAX), _head(0), _tail(0) | |
{ | |
_Alloc(); | |
} | |
Queue(int max) : _max(max), _head(0), _tail(0) | |
{ | |
_Alloc(); | |
} | |
~Queue() | |
{ | |
_DeAlloc(); | |
} | |
//methods | |
bool Push(T datum) | |
{ | |
if (_tail >= _max) | |
return false; | |
if constexpr (std::is_scalar_v<T>) | |
{ | |
new(_data + _tail++) T(datum); | |
} | |
else | |
{ | |
_data[_tail++] = datum; | |
} | |
return true; | |
} | |
T Pop() | |
{ | |
if (IsEmpty()) | |
{ | |
throw std::_Xruntime_error; | |
} | |
if constexpr (std::is_scalar_v<T>) | |
{ | |
T temp = std::move(_data[_head]); | |
_data[_head++].~T(); | |
return temp; | |
} | |
else | |
{ | |
return _data[_head++]; | |
} | |
} | |
bool IsEmpty() | |
{ | |
return !(_head < _tail); | |
} | |
}; | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment