當前位置:首頁 » 操作系統 » 貪心演算法著色

貪心演算法著色

發布時間: 2022-04-17 19:10:33

Ⅰ 關於C語言的問題,高手進

1. 迷宮問題
/////////////////////////
/////////迷宮求解////////
//////作者:hacker/////
/////時間:11.10.2006/////
/////////////////////////
//Migong.h
//利用棧進行回溯
/*class:
Matrix:矩陣類
offsets:搜索偏移
enum directions:四個方向
struct item:搜索節點
Migong:迷宮類
1.創建一個Migong對象
2.使用用Create方法輸入數據
3.使用Solve方法進行求解
4.ShowSolve方法顯示解
5.可以重復使用Create方法
6.入口只能在左上角
7.默認出口在右下角
ShowAllPath:窮舉所有的路徑
備注:
由於演算法原因,這里的所有路徑應該是指
介於:
a.如果兩條路存在某個點不同那麼就是不同的路
b.如果在一條路中去掉一個或者一個以上的圈,那麼他們是同一條路
之間意義上的路
*/
#include <iostream>
#include <stack>
#include <vector>

using namespace std;

#ifndef MIGONG_H
#define MIGONG_H
///////////////////
///////矩陣類//////
///////////////////
class Matrix{
int* m;
int row, col;
bool iscreate;
public:
Matrix(){m=0;iscreate=false;};
~Matrix() {Release();};
bool Create(int, int);
int& operator () (int, int);
int GetRow(){return row;};
int GetCol(){return col;};
void Release();
void Show(char, char );
};

bool Matrix::Create(int r, int c)
{
if( r<=0 || c<=0) return false;

Release();

row = r;
col = c;
m = new int[row*col];

for (int i=0;i<row*col;i++)
{
*(m+i) = 0;
}
iscreate = true;
return true;
}

int& Matrix::operator ()(int r, int c)
{
return *(m+r*col+c);
}

void Matrix::Release()
{
if (iscreate)
{
row = col = 0;
if (m) delete[] m;
m = 0;
}
iscreate = false;
}

void Matrix::Show(char blk='#', char nblk=' ')
{
int i, j;
for (i=0;i<row;i++)
{
for (j=0;j<col;j++)
{
if (*(m+i*col+j) == 0)
cout<<nblk;
else
cout<<blk;
}
cout<<endl;
}
}
/////////////////////////////
////迷宮相關數據結構的定義///
/////////////////////////////
struct offsets{
int a, b;
};

enum directions{
_S = 0,
_E,
_N,
_W
};

struct item{
int row, col, dir;
};

class Migong{
static offsets move[4];
Matrix maze;
Matrix mark;
int row;
int col;
int desr;
int desc;
stack<item> stk;
bool iscreate;
int pathlength;
bool GetPath();
bool IsInPath(int, int);
public:
Migong(){issolved=false;result=0;pathlength=row=col=0;iscreate=false;};
~Migong(){Release();};
bool Create(int* , int , int , int , int );
void Solve();
void Release();
void OutputMaze();
void ShowSolve(char, char );
public:
bool issolved;
item* result;
};

offsets Migong::move[4]={ {1, 0}, {0, 1},
{-1, 0}, {0, -1}};

////////////////////////////
//迷宮數據應該是不含邊框的//
////////////////////////////
bool Migong::Create(int* m, int r, int c, int desrow=-1, int descol=-1)
{
if (r<=0 || c<=0) return false;

Release();

if (desrow==-1 || descol==-1)
{
desr = r;
desc = c;
}
else
{
desr = desrow;
desc = descol;
}

row = r;
col = c;
maze.Create(r+2, c+2);
mark.Create(r+2, c+2);

int i, j;

for (i=0;i<r+2;i++)
{
for (j=0;j<c+2;j++)
{
if (j==0 || j==c+1 || i==0 || i==r+1)
{
mark(i, j) = maze(i, j) = 1;

}else
{
mark(i, j) = 0;
maze(i, j) = m[((i-1)*col+j-1)];
}
}
}
return iscreate = true;
}

bool Migong::GetPath()
{
mark(1,1) = 1;
item temp;
temp.col = 1;
temp.row = 1;
temp.dir = _S;
stk.push(temp);

while (!stk.empty())
{
temp = stk.top();
stk.pop();

int i = temp.row;
int j = temp.col;
int d = temp.dir;

while (d<4)
{//根據當前點的狀態確定下一個搜索點
int g = i + move[d].a;
int h = j + move[d].b;

if (g==desr && h==desc)
{
return true;
}
//如果這個點不是障礙點且沒有被搜索過那麼可以對這個點進行搜索
if (maze(g, h)==0 && mark(g, h)==0)
{
mark(g, h) = 1;
temp.row = g;
temp.col = h;
temp.dir = d+1;
stk.push(temp);
i = g;
j = h;
d = _S;//對一下個點進行搜索
}
else d++;
}
}
return false;
}

void Migong::Solve()
{
issolved = GetPath();
if (issolved)
{
pathlength = stk.size();
result = new item[pathlength];
for (int i=0;i<pathlength;i++)
{
*(result+i) = stk.top();
stk.pop();
// cout<<"("<<(*(result+i)).row<<","<<(*(result+i)).col<<")"<<endl;
}
}
while (!stk.empty())
stk.pop();
}

void Migong::Release()
{
if (iscreate)
{
maze.Release();
mark.Release();
row=col=0;
if (result)
delete [] result;
result = 0;
while (!stk.empty())
stk.pop();
}
iscreate = false;
issolved = false;
pathlength = 0;
}

void Migong::OutputMaze()
{
if (!iscreate) return;
maze.Show();
}

bool Migong::IsInPath(int r, int c)
{
if (!iscreate || !issolved)
return false;

item temp;
for (int i=0;i<pathlength;i++)
{
temp = *(result+i);
if ((temp.row==r) && (temp.col==c))
return true;
}

return false;
}

void Migong::ShowSolve(char blk='#',char s='o')
{
if (!iscreate) return;
if (!issolved)
{
cout<<"無解"<<endl;
}
else
{
int i, j;
for (i=0;i<row+2;i++)
{
for (j=0;j<col+2;j++)
{
if ((i==1 && j==1) || (i==desr && j==desc))
{
cout<<s;
}
else if (maze(i, j) == 1)
{
cout<<blk;
}else
{
if (IsInPath(i, j))
cout<<s;
else
cout<<' ';
}
}
cout<<endl;
}
}
}

//////////////////////
//////窮舉所有路徑////
//////////////////////
offsets move[4]={ {1, 0}, {0, 1},
{-1, 0}, {0, -1}};

struct node
{
int row,col;
};

vector<node> path;
int count;
bool IsReachable( Matrix& maze, Matrix& mark, node beg, node des)
{
if (beg.row==des.row&&beg.col==des.col)
{//如果達到的話那麼顯示路徑
count++;
cout<<"第"<<count<<"條路徑:"<<endl;
for (int i=0;i<path.size();i++)
cout<<"("<<path[i].row<<","<<path[i].col<<")";
cout<<"("<<des.row<<","<<des.col<<")";
cout<<endl;
return false;
}
if (maze(beg.row, beg.col)==1 || mark(beg.row, beg.col)==1)
{
return false;
}

path.push_back(beg);
mark(beg.row, beg.col) = 1;

node nextnode;

for (int i=_S;i<_W+1;i++)
{
nextnode.row = beg.row + move[i].a;
nextnode.col = beg.col + move[i].b;

IsReachable(maze, mark, nextnode, des);
}

path.resize(path.size()-1);
mark(beg.row, beg.col) = 0;

return false;//如果不是窮舉的話應該根據for循環的結果重新設置返回值
}

/*
參數maze,mark為迷宮長寬均加二的矩陣
desr,desc為出口點
*/
void FindAllPath( Matrix& maze, Matrix& mark, int desr, int desc)
{
node first, last;
first.row = 1;
first.col = 1;
last.row = desr;
last.col = desc;

IsReachable(maze, mark, first, last);

path.clear();
}

/*
m迷宮矩陣數據
r,c行和列的大小
desr,desc目標位置
*/
void ShowAllPath(int* m, int r, int c, int desr=-1, int desc=-1)
{
Matrix maze, mark;
maze.Create(r+2, c+2);
mark.Create(r+2, c+2);

if (desr==-1 || desc==-1)
{
desr = r;
desc = c;
}

int i, j;

for (i=0;i<r+2;i++)
{
for (j=0;j<c+2;j++)
{
if (j==0 || j==c+1 || i==0 || i==r+1)
{
mark(i, j) = maze(i, j) = 1;

}else{
mark(i, j) = 0;
maze(i, j) = m[((i-1)*c+j-1)];
}
}
}

count = 0;
FindAllPath(maze, mark, desr, desc);

maze.Release();
mark.Release();
}
#endif
//main.cpp
#include <iostream>
#include "Migong.h"
using namespace std;

int mg[]={
0,0,1,0,0,0,1,0,//1
0,0,1,0,0,0,1,0,//2
0,0,0,0,1,1,0,1,//3
0,1,1,1,0,0,1,0,//4
0,0,0,1,0,0,0,0,//5
0,1,0,0,0,0,0,1,//6
0,1,1,1,1,0,0,1,//7
1,1,0,0,0,1,0,1,//8
1,1,0,0,0,0,0,0,//9
};

void main()
{
Migong m;
m.Create(mg, 9, 8);
m.OutputMaze();
m.Solve();
m.ShowSolve();

ShowAllPath(mg,9,8,9,8);
}
2.任意兩點的最短路
class Matrix
{
bool IsCreated;
int* data;
int row;
int col;
public:
Matrix(){IsCreated = false;row = 0; col = 0; data = 0;};
~Matrix(){if (data!=0) delete [] data;};
bool Create(int r, int c, int n);
int& operator () (int r, int c);
};

bool Matrix::Create(int r, int c, int n = 0)
{
if ( IsCreated)
return false;

if ( (row = r) <=0 || (col = c) <=0)
return false;

data = new int[row*col];
int i,j;
for (i=0;i<row;i++)
for (j=0;j<col;j++)
data[i*col+j] = n;

IsCreated = true;
return true;
}

int& Matrix::operator()(int r, int c)
{
return data[r*col+c];
}
jingdian//Matrix類,存儲
dist//Matrix類,存儲任意兩點間的距離
MAX_DIST//一個大數,大於任意兩點間的距離,當兩點間沒有路的時候設為它
bestdist//Matrix類,存儲任意兩點的最近距離
bestpath//Matrix類,i到j的最短路為i.......bestpath(i,bestpath(i,pbestpath(i,j))), bestpath(i,pbestpath(i,j)), j bestpath(i,j)表示i到j的最短路中j的前一個結點號
void GetBestPath()
{

int n = jingdian.size();
int i, j, k;

bestdist.Create(n, n);
bestpath.Create(n, n);

for (i=0;i<n;i++)
for (j=0;j<n;j++)
{
if ( (bestdist(i,j) = dist(i,j)) == -1)
bestdist(i,j) = MAX_DIST;
if ( i!=j && bestdist(i,j)<MAX_DIST )
bestpath(i,j) = i;
else
bestpath(i,j) = -1;
}

for (k=0;k<n;k++)
for (i=0;i<n;i++)
for (j=0;j<n;j++)
if (bestdist(i,k)+bestdist(k,j) < bestdist(i,j))
{
bestdist(i,j) = bestdist(i,k)+bestdist(k,j);
bestpath(i,j) = bestpath(k,j);
}
}

Ⅱ 貪心演算法之會場安排問題

三星演算法之間最好還是不要安排互相的問題,這樣不利於你們倆的關系的便有好。

Ⅲ 求高手幫忙做一套演算法分析的題目。做好之後再加100。

如何選擇排序、矩陣相乘、樹和圖演算法的時間復雜性計量單位?
排序:排序的循環次數(或遞歸次數)。
矩陣相乘:做實數乘法的次數。
樹:搜索的次數。
圖:同樹。
演算法有幾種基本結構?各種結構的時間復雜度的計算規則?
3種
順序結構:T(n)=O(c)
選擇結構:T(n)=O(c)
循環結構:T(n)=O(n)
最壞情況下的時間復雜性和平均情況下的時間復雜性的定義?
在規模n的全部輸入中,可以找尋執行一個演算法所需的最大時間資源的量,這個量稱為對規模n的輸入,演算法的最壞情況時間復雜性。
對規模都為n的一些有限輸入集,執行演算法所需的平均時間資源的量稱為平均情況下的時間復雜性。
為什麼選擇時間復雜度的漸進性態評價演算法?
因為在規模較小的時候無法客觀體現一個演算法的效率。
解釋f(n)=O(g(n))的意義。
若f(n)和g(n)是定義在正整數集合上的 兩個函數,則f(n)=O(g(n))表示存在正的常數C和n0 ,使得當n≥n0時滿足0≤f(n)≤C*g(n)。
簡述之就是這兩個函數當整型自變數n趨向於無窮大時,兩者的比值是一個不等於0的常數。
有效演算法和無效演算法的劃分原則?
區分在於問題是否能夠精確求解。
用分治法設計演算法有什麼好處?為什麼描述分治演算法需要使用遞歸技術?
分治法可以將問題分為許規模更小的子問題,這些子問題相互獨立且與原問題相同。使用遞歸技術,雖然一些簡單的循環結構替代之,但是復雜的問題,比如二階遞歸是無法替代的。
歸並排序演算法和快速排序演算法劃分子問題和合並子問題的解的方法各是是怎樣的?
歸並排序演算法:
劃分子問題:每次分成2個大小大致相同的子集和
合並子問題:將2個排好序的子數組合並為一個數組
快速排序演算法:對輸入的子數組a[p:r]
劃分子問題:劃分為a[p:q-1],a[q]和a[q+1:r]使a[p:q-1]任意元素小於a[q],a[q+1:r] 任意元素大於a[q]
合並子問題:不需要(因為劃分過程就已經排序完成了)
簡述二分檢索(折半查找)演算法為什麼比順序查找的效率高?
對於二分搜索 最壞情況為O(logn)時間完成
而順序查找 需要O(n)次比較
顯然二分搜索效率高
貪心法的核心是什麼?
貪心演算法是通過一系列選擇得到問題的解,它所作出的選擇都是當前狀態下的最佳選擇。
背包問題的目標函數是什麼?背包問題貪心演算法的最優量度是什麼?演算法是否獲得最優解? 用貪心演算法解0/1背包問題是否可獲得最優解?
Max=∑Vi*Xi (V是價值X取1,0表示裝入或不裝)
每次選取單位重量價值最高的
不一定是最優解

情況不妙啊 LZ還要繼續否。。。
早知發郵件了。。。

熱點內容
android自動化測試腳本 發布:2024-09-30 07:32:51 瀏覽:508
伺服器如何查看線程池未關閉 發布:2024-09-30 07:13:51 瀏覽:412
如何配置資源管理 發布:2024-09-30 07:08:10 瀏覽:992
坦克世界亞服伺服器怎麼連接 發布:2024-09-30 07:07:18 瀏覽:493
手機nba2k17的文件夾 發布:2024-09-30 06:50:30 瀏覽:898
廣州市java培訓 發布:2024-09-30 06:48:52 瀏覽:143
python爬蟲簡歷模板 發布:2024-09-30 06:40:23 瀏覽:590
項目源碼丟失反編譯 發布:2024-09-30 06:27:07 瀏覽:776
fpga編譯後生成什麼文件 發布:2024-09-30 06:18:34 瀏覽:111
端編程語言 發布:2024-09-30 06:13:19 瀏覽:423