1.一个n*m的矩阵,求一个x是否在这个矩阵中的算法?
矩形如下:
1 3 5 8 10
2 4 6 11 12
3 5 9 12 15
6 8 10 13 18
解析:
1):规律: 每行递增,每列递增,
2):利用二分法的扩展 a[n/2]每次去掉1/2, => a [n/2] [m/2]每次去掉 n/2* (m/2)划分为四个矩形,利用递归法进行分解。
3):如果 a [n/2] [m/2] < x;排除一个矩形,还剩下三个,递归带三个矩形。同样的,如果a [n/2] [m/2] > x还剩下三个,递归带三个矩形。
并在最外层设置一个bool变量,如果a [n/2] [m/2] == x,则找到,bool = true。程序最后返回bool,而且只有返回至为true时,才将返回值付给bool变量。关于代码我会继续更新,写完后会贴出来。
4):要特别注意临界点,像二分法(arrau[n/2]有能剩下两个数,也有可能剩下一个数。同样的,我们可以扩展,array[n/2, m/2],相当于二分法*二分法,所以就会有剩下四个数和剩下一个的情况。我个人认为,还可以在此基础上继续扩展,array[n/2, m/2,k/2] =>就会有剩下8个数,和剩下一个数的情况。算法也可以在此基础上继续加深,其实也只不过是多了个z坐标,当然写其实会更加复杂,有时间的话,我会在尝试着写一写,有朋友写的话,也可以拿出来分享一下,呵呵。。。
代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication11
{
class Program
{
static void Main(string[] args)
{
int[,] array = new int[,] { { 1,3, 5, 8, 10}, { 2, 4, 6, 11, 12},{3, 5, 9, 12, 15},{6, 8, 10, 13, 18} };
bool test = CheckExist(array, 0, 0, 3, 4, 13);
}
static bool CheckExist(int[,] array, int x1, int y1, int x2, int y2, int x)
{
//The last one when the count is odd number.
if(((x1+x2)/2 == x1 && (y1+y2)/2 == y1) && ((x1+x2)/2 == x2 && (y1+y2)/2== y2))
{
// Check whether the last one equl the input paremeter x.
if (array[(x1 + x2)/2, (y1 + y2)/2] == x)
{
return true;
}
else
{
return false;
}
}
// The last four ones when the count is even number.
if (x2 -x1 == 1 && y2 -y1 == 1)
{
// Check the last four ones if one of them equal x.If find, return true.
if (array[x1,y1]== x ||array[x1,y2] ==x||array[x2,y1]== x ||array[x2,y2] == x)
{
return true;
}
else
{
return false;
}
}
if (array[(x1 +x2)/2,(y1+y2)/2] < x)
{
// We should check whether it exist in other there areas.
if (array[x2,(y1 + y2) / 2] > x || array[(x1 + x2) / 2,y2] > x || array[x2,y2] > x)
{
bool result1 = false;
bool result2 = false;
bool result3 = false;
if (array[(x1+x2)/2, y2] > x)
{
result1 = CheckExist(array, x1, (y1+y2)/2 +1, (x1+x2)/2, y2, x);
}
if (array[x2, (y1+y2)/2] > x)
{
result2 = CheckExist(array, (x1 +x2)/2 +1, y1, x2, (y1+y2)/2, x);
}
if (array[x2,y2] > x)
{
result3 = CheckExist(array, (x1 + x2)/2 + 1, (y1 + y2)/2 +1, x2, y2, x);
}
// If it exits in one area, return true.
return result1 || result2 ||result3;
}
else
{
// If we can find in other three areas, so it does not exist.
return false;
}
}
else if (array[(x1 + x2) / 2, (y1 + y2) / 2] > x)
{
// We should check whether it exist in other there areas.
if (array[(x1 + x2) / 2, y1] < x || array[x1, (y1 + y2) / 2] < x || array[x1, y1] < x)
{
bool result1 = false;
bool result2 = false;
bool result3 = false;
if (array[x1, (y1 + y2) / 2 + 1] < x)
{
result1 = CheckExist(array, x1, (y1 + y2) / 2 + 1, (x1 + x2) / 2, y2, x);
}
if (array[(x1 + x2) / 2 + 1, y1] < x)
{
result2 = CheckExist(array, (x1 + x2) / 2 + 1, y1, x2, (y1 + y2) / 2, x);
}
if (array[x1, y1] < x)
{
result3 = CheckExist(array, x1, y1, (x1 + x2) / 2, (y1 + y2) / 2, x);
}
// If it exits in one area, return true.
return result1 || result2 || result3;
}
else
{
// If we can find in other three areas, so it does not exist.
return false;
}
}
else
{
return true;
}
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication11
{
class Program
{
static void Main(string[] args)
{
int[,] array = new int[,] { { 1,3, 5, 8, 10}, { 2, 4, 6, 11, 12},{3, 5, 9, 12, 15},{6, 8, 10, 13, 18} };
bool test = CheckExist(array, 0, 0, 3, 4, 13);
}
static bool CheckExist(int[,] array, int x1, int y1, int x2, int y2, int x)
{
//The last one when the count is odd number.
if(((x1+x2)/2 == x1 && (y1+y2)/2 == y1) && ((x1+x2)/2 == x2 && (y1+y2)/2== y2))
{
// Check whether the last one equl the input paremeter x.
if (array[(x1 + x2)/2, (y1 + y2)/2] == x)
{
return true;
}
else
{
return false;
}
}
// The last four ones when the count is even number.
if (x2 -x1 == 1 && y2 -y1 == 1)
{
// Check the last four ones if one of them equal x.If find, return true.
if (array[x1,y1]== x ||array[x1,y2] ==x||array[x2,y1]== x ||array[x2,y2] == x)
{
return true;
}
else
{
return false;
}
}
if (array[(x1 +x2)/2,(y1+y2)/2] < x)
{
// We should check whether it exist in other there areas.
if (array[x2,(y1 + y2) / 2] > x || array[(x1 + x2) / 2,y2] > x || array[x2,y2] > x)
{
bool result1 = false;
bool result2 = false;
bool result3 = false;
if (array[(x1+x2)/2, y2] > x)
{
result1 = CheckExist(array, x1, (y1+y2)/2 +1, (x1+x2)/2, y2, x);
}
if (array[x2, (y1+y2)/2] > x)
{
result2 = CheckExist(array, (x1 +x2)/2 +1, y1, x2, (y1+y2)/2, x);
}
if (array[x2,y2] > x)
{
result3 = CheckExist(array, (x1 + x2)/2 + 1, (y1 + y2)/2 +1, x2, y2, x);
}
// If it exits in one area, return true.
return result1 || result2 ||result3;
}
else
{
// If we can find in other three areas, so it does not exist.
return false;
}
}
else if (array[(x1 + x2) / 2, (y1 + y2) / 2] > x)
{
// We should check whether it exist in other there areas.
if (array[(x1 + x2) / 2, y1] < x || array[x1, (y1 + y2) / 2] < x || array[x1, y1] < x)
{
bool result1 = false;
bool result2 = false;
bool result3 = false;
if (array[x1, (y1 + y2) / 2 + 1] < x)
{
result1 = CheckExist(array, x1, (y1 + y2) / 2 + 1, (x1 + x2) / 2, y2, x);
}
if (array[(x1 + x2) / 2 + 1, y1] < x)
{
result2 = CheckExist(array, (x1 + x2) / 2 + 1, y1, x2, (y1 + y2) / 2, x);
}
if (array[x1, y1] < x)
{
result3 = CheckExist(array, x1, y1, (x1 + x2) / 2, (y1 + y2) / 2, x);
}
// If it exits in one area, return true.
return result1 || result2 || result3;
}
else
{
// If we can find in other three areas, so it does not exist.
return false;
}
}
else
{
return true;
}
}
}
}
做个快乐的自己。