#include <bits/stdc++.h>
class Coordinates
{
public:
int rowNumber, columnNumber;
// Constructor will take size of board and cellNumber
Coordinates(int cellNumber, int n)
{
// Formula for conversion of cellNumber to Coordinates
rowNumber = (cellNumber - 1) / n;
columnNumber = (cellNumber - 1) % n;
if (rowNumber % 2 == 1)
{
columnNumber = (n - 1) - columnNumber;
}
rowNumber = (n - 1) - rowNumber;
}
};
int minDiceThrowToLastCell(int **board, int n)
{
int *minDiceThrow, i;
minDiceThrow = new int[(n * n) + 1];
// Initializing the minDiceThrow for all the cells to INT_MAX;
for (i = 1; i <= n * n; i++)
{
minDiceThrow[i] = INT_MAX;
}
/*
We will store cellNumber in the queue where the front of the queue will
always contain a cell which can be reached by minimum dice throw from start (cellNumber = 1).
We will BFS Technique to maintain the queue.
*/
std::queue<int> Q;
// As we are starting from cell 1
minDiceThrow[1] = 0;
Q.push(1);
while (!Q.empty())
{
int curCellNumber = Q.front();
Q.pop();
for (i = 1; i <= 6 && curCellNumber + i <= n * n; i++)
{
int nextCellNumber = i + curCellNumber;
Coordinates nextCell(nextCellNumber, n);
// Check for Snake or Ladder
if (board[nextCell.rowNumber][nextCell.columnNumber] != -1)
{
nextCellNumber = board[nextCell.rowNumber][nextCell.columnNumber];
}
// If we have a better minimum
if (minDiceThrow[nextCellNumber] > minDiceThrow[curCellNumber] + 1)
{
minDiceThrow[nextCellNumber] = minDiceThrow[curCellNumber] + 1;
Q.push(nextCellNumber);
}
}
}
int finalMinDiceThrowToLastCell = minDiceThrow[n * n];
// If it's impossible to reach the last cell
if (finalMinDiceThrowToLastCell == INT_MAX)
{
finalMinDiceThrowToLastCell = -1;
}
// Deleting Dynamic MinDiceThrow Array
delete[] minDiceThrow;
return finalMinDiceThrowToLastCell;
}
int main()
{
int **board = new int*[3];
for (int i = 0; i<3; i++)
{
board[i] = new int[3];
}
board[0][0] = -1;
board[0][1] = 1;
board[0][2] = -1;
board[1][0] = -1;
board[1][1] = -1;
board[1][2] = 9;
board[2][0] = -1;
board[2][1] = 4;
board[2][2] = -1;
minDiceThrowToLastCell(board, 3);
}
Coordinates nextCell(nextCellNumber, n);가 뭐임?
coordinates는 대충 int, bool 같은 데이터 타입을 사용자가 임의로 만든 듯 하고 nextCell은 어디서 나오는 새끼임?
생성자, 그냥 변수 이름
Coordinates nextCell = Coordinates(nextCellNumber, n); 으로 코드를 고쳐도 돌아가던데 이거랑 같은거지?
Coordinates(int cellNumber, int n) { // Formula for conversion of cellNumber to Coordinates rowNumber = (cellNumber - 1) / n; columnNumber = (cellNumber - 1) % n;if (rowNumber % 2 == 1) { columnNumber = (n - 1) - columnNumber; }rowNumber = (n - 1) - rowNumber; }