Created
April 12, 2016 17:43
-
-
Save jianminchen/ce7ccfa5db5b57c36d6742b622e9153e to your computer and use it in GitHub Desktop.
K index - algorithm - fix 2 bugs - one is to continue to do DFS, conditional checking; second is DFS calls return handling
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
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace kIndex | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
int k = Convert.ToInt16(Console.ReadLine()); | |
int[][] arr = new int[k][]; | |
for (int i = 0; i < k; i++) | |
arr[i] = new int[k]; | |
for (int i = 0; i < k; i++) | |
{ | |
string s = Console.ReadLine(); | |
string[] aA = s.Split(' '); | |
if (aA.Length != k) | |
return; | |
for (int j = 0; j < k; j++) | |
arr[i][j] = Convert.ToInt16(aA[j].Trim()); | |
} | |
int kIndex = Convert.ToInt16(Console.ReadLine()); | |
if (kIndexFunc(arr, kIndex)) | |
{ | |
Console.WriteLine("YES"); | |
} | |
else | |
Console.WriteLine("NO"); | |
} | |
public static bool kIndexFunc(int[][] arr, int kIndex) | |
{ | |
if (arr == null) return false; | |
int m = arr.Length; | |
int n = arr[0].Length; | |
if (m != n) | |
return false; | |
for (int i = 0; i < m; i++) | |
for (int j = 0; j < m; j++) | |
{ | |
int search = arr[i][j]; | |
if (searchInKIndex(arr, i, j, kIndex, search, m)) | |
return true; | |
} | |
return false; | |
} | |
private static bool searchInKIndex(int[][] arr, int row, int col, int kIndex, int search, int MaxRow) | |
{ | |
bool[][] searchedA = new bool[MaxRow][]; | |
for (int i = 0; i < MaxRow; i++) | |
searchedA[i] = new bool[MaxRow]; | |
for (int i = 0; i < MaxRow; i++) | |
for (int j = 0; j < MaxRow; j++) | |
searchedA[i][j] = false; | |
// up, down, left, right | |
return DFS(arr, row, col, row, col, kIndex, search, MaxRow, searchedA); | |
} | |
private static bool DFS(int[][] arr, int oriX, int oriY, int row, int col, int kIndex, int search, int MaxRow, bool[][] searchedA) | |
{ | |
if (!isValid(row, MaxRow) || !isValid(col, MaxRow) || kIndex < 0) | |
return false; | |
if (Math.Abs(oriX - row) + Math.Abs(oriY - col) > 0 && !searchedA[row][col] ) | |
{ | |
if (arr[oriX][oriY] == arr[row][col]) | |
return true; | |
} | |
searchedA[row][col] = true; // bug001 - check condition is wrong | |
if (DFS(arr, oriX, oriY, row - 1, col, kIndex - 1, search, MaxRow, searchedA) || | |
DFS(arr, oriX, oriY, row + 1, col, kIndex - 1, search, MaxRow, searchedA) || | |
DFS(arr, oriX, oriY, row, col + 1, kIndex - 1, search, MaxRow, searchedA) || | |
DFS(arr, oriX, oriY, row, col - 1, kIndex - 1, search, MaxRow, searchedA)) | |
return true; // any of conditions are ok, then, return true | |
return false; | |
} | |
private static bool isValid(int row, int MaxRow) | |
{ | |
if (row >= 0 && row < MaxRow) | |
return true; | |
else | |
return false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment