标签:
Spent time to write down C# program to implement an algorithm. Feel great! Start to practice, a question a time.
Question is from careercup:
http://www.careercup.com/question?id=4716965625069568
—
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FindShortestGuard
{
class Program
{
/*
* http://www.careercup.com/question?id=4716965625069568
* http://www.cnblogs.com/zhuli19901106/p/3710623.html
*
* Given a 2-D matrix represents the room, obstacle and guard like the following (0 is room, B->obstacle, G-> Guard):
0 0 0
B G G
B 0 0
calculate the steps from a room to nearest Guard and set the matrix, like this
2 1 1
B G G
B 1 1
Write the algorithm, with optimal solution.
*
* using BFS, breadth first search; in detail, starting from every guard, using the BFS search.
*/
static void Main(string[] args)
{
//int[,] matrix = new int[3, 3] { { 0, 0, 0 }, { -2, -1, -1 }, { -2, 0, 0 } };
/*
int[][] matrix = new int[][]{
new int[]{ 0, 0, 0},
new int[]{ -2, -1, -1},
new int[]{-2, 0, 0}
};
m = n=3;
*/
int[][] matrix = new int[][]{
new int[]{0, 0, 0, 0},
new int[]{-2, -2, -1, -1},
new int[]{-2, -2, 0, 0},
new int[]{0, 0, 0, 0}
};
m = 4;
n = 4;
solve(matrix);
Console.WriteLine(“The result of the coding: “);
}
public static void solve(int[][] matrix) {
// -1 for guard
// -2 for blockade
// 0 for room
// > 0 for distance
int nRows = (int)matrix.Length;
if (nRows == 0) {
return;
}
int mColumns = (int)matrix[0].Length;
if (mColumns == 0) {
return;
}
int i, j;
for (i = 0; i < nRows; ++i) {
for (j = 0; j < mColumns; ++j) {
if (matrix[i][j] == -1) { // Guard: -1
doBFS(matrix, i, j);
}
}
}
}
private static int n, m;
private static bool inBound(int x, int y) {
return x >= 0 && x <= n – 1 && y >= 0 && y <= m – 1;
}
private bool inBoundJulia(int x, int y){
return x>=0 && (x<=(n-1)) && (y>=0) && (y<=m-1);
}
// https://msdn.microsoft.com/en-us/library/2yd9wwz4.aspx
//int[,] array2Da = new int[4, 2] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };
static int[,] dd = new int[4,2]{{-1, 0}, {+1, 0}, {0, -1}, {0, +1}};
private static void doBFS(int[][] matrix, int x, int y) {
Queue<int> q = new Queue<int>();
int tmp;
int xx, yy;
int i;
int dist;
q.Enqueue(x * m + y);
while (q.Count>0) {
tmp = q.Peek();
q.Dequeue();
x = tmp / m;
y = tmp % m;
dist = matrix[x][y] > 0 ? matrix[x][y] : 0;
for (i = 0; i < 4; ++i) {
// one of 4 neighbors –
xx = x + dd[i,0];
yy = y + dd[i,1];
// boundary check – avoid duplication calculations as well
// the order is also important; in the array; and then, check node is not room;
// and then, node is already with less steps
//
if (
// node is out of boundary of matrix
!inBound(xx, yy) ||
// node is not room – secondary check
matrix[xx][yy] < 0 ||
// node visited has less steps to a guard
(matrix[xx][yy] > 0 && matrix[xx][yy] <= dist + 1))
{
// out of boundary
// a guard or a blockade
// the distance is no shorter
continue;
}
// if the node on the queue is the Guard node, (value: -1)
bool queuedNodeIsGuard = (dist == 0); //
bool queueNodeIsRoom = (dist >= 1);
if (queuedNodeIsGuard)
matrix[xx][yy] = 1;
// else if the node on the queue is a room, need to compare the distance
else if (queueNodeIsRoom )
{
// keep minimum value
bool neverCalculated = matrix[xx][yy] == 0;
bool hasHighSteps = matrix[xx][yy] > dist + 1;
bool needUpdate = (neverCalculated || hasHighSteps);
if(needUpdate)
matrix[xx][yy] = dist+1;
}
q.Enqueue(xx * m + yy);
}
}
}
}
}
标签:
原文地址:http://www.cnblogs.com/juliachenOnSoftware/p/4382662.html