連通區域內求1的個數
来源:互联网 发布:activiti 源码 编辑:程序博客网 时间:2024/06/05 20:56
#include <iostream>
#include <fstream>
using namespace std;
struct Number
{
int i;
int j;
int num;
struct Number *next;
};
int SearchNumber(int i,int j, int l, struct Number *phead);
void ClearFunction(struct Number *phead);
int main()
{
int m;
int n;
int x;
int y;
int a;
int i;
int j;
int s = 0;
int l = 1;
int total = 0;
struct Number *phead;
struct Number *ptail;
struct Number *pnew;
struct Number *p;
pnew = phead = ptail = NULL;
ifstream infile("Number.txt",ios::in);
if (!infile)
{
cerr << "打開文件失敗!" << endl;
exit(1);
}
cout << "請輸入矩陣的行數和列數:" << endl;
cin >> m >> n;
for (x=0; x<m; x++)
{
for (y=0; y<n; y++)
{
pnew = (struct Number *)malloc(sizeof (struct Number));
pnew->i = x;
pnew->j = y;
infile >> pnew->num;
cout << pnew->num << " ";
pnew->next = NULL;
if (phead == NULL)
{
phead = pnew;
ptail = pnew;
}
else
{
ptail->next = pnew;
ptail = pnew;
}
}
cout << endl;
}
int *count = new int[sizeof(int)*m*n];
for (i=0; i<m*n; i++)
{
count [i] = 0;
}
p = phead;
while (p != NULL)
{
if (p->num == 1)
{
total ++;
}
p = p->next;
}
while (phead != NULL)
{
if (phead->num == 1)
{
s++;
i = phead->i;
j = phead->j;
count[s] = SearchNumber(i,j,l,phead);
}
phead = phead->next;
}
cout << endl;
cout << "數字1的連通區域的個數為:" << s << endl;
cout << "各個區域中含有1的個數為:";
for (a=1; a<=s; a++)
{
cout << count[a] << " ";
}
cout << endl;
cout << "個連通區域中1的總個數為:" << total << endl;
cout << endl;
system("pause");
ClearFunction(phead);
delete[] count;
return 0;
}
int SearchNumber(int i, int j, int l, struct Number *phead)
{
while (phead!=NULL)
{
if ((phead->i == i+1)&&(phead->j == j))
{
if (phead->num == 1)
{
phead->num = 2;
l++;
l = SearchNumber(i+1,j,l,phead);
}
}
if ((phead->i == i-1)&&(phead->j == j))
{
if (phead->num == 1)
{
phead->num = 2;
l++;
l = SearchNumber(i-1,j,l,phead);
}
}
if ((phead->i == i)&&(phead->j == j+1))
{
if (phead->num == 1)
{
phead->num = 2;
l++;
l = SearchNumber(i,j+1,l,phead);
}
}
if ((phead->i == i+1)&&(phead->j == j))
{
if (phead->num == 1)
{
phead->num = 2;
l++;
l = SearchNumber(i,j-1,l,phead);
}
}
phead = phead->next;
}
return l;
}
void ClearFunction(struct Number *phead)
{
struct Number *p;
while (phead!=NULL)
{
p = phead;
phead = p->next;
free(p);
}
}
- 連通區域內求1的個數
- 求質數的個數
- SQL 獲取一年的每一日.每週,某個星期的數據
- vs2005兩個非常有用的函數
- PHP-九個非常有用的函數
- 時間處理的函數
- oracle的函數
- 建置一個 WINCE 下的 XML BASE 的人機介面引擎 ---- 繪圖引擎篇 (1)
- 1的平方-2的平方+3的平方-4的平方...-100的平方,结果
- 常用的幾個sql
- 失戀萬歲的20個理由
- 一個平淡的下午
- 委托的一個示例
- 我的個人特徵
- 四個出人意表的 Library
- 打不過就說的那個你
- 打不過就說的那個你
- 丟失的數據
- 安静地待着
- GridView LINQ与DataSet绑定时空值判断
- 敏捷无敌之末日帝国(4)
- 文件输入/输出
- 为什么要用要用带自适应的均衡器?
- 連通區域內求1的個數
- 总结: 名字空间
- 今天遇到个杀不死的会话
- Esterel语言的一项应用实验
- java.sql.SQLException: Cannot load JDBC driver class 'oracle.jdbc.driver.OracleDriver'
- 灵魂很轻的日子
- WEB开发新势力——Openparty
- AIX LVM底层数据结构剖析(根据qintl讲稿整理)
- operator=, swap, copy constructor