有趣 的 ponds and islands 递归

yorkzjy 2010-02-04 02:14:55
今天在教室的黑板上看到了这个题目,觉得挺有趣。

给出一个矩阵,即通 ponds 或者 islands 的个数, 相连的 0 为一个pond , 相连的 1 为 一个island。
(其实跟找迷宫差不多)
这是我写的递归程序,可是得不到正确答案。 请高手指教我错在哪里了??

数据: 每个矩阵开头一行是说明列行大小的

5 5
0 1 0 1 0
0 1 0 1 0
0 0 0 1 0
1 1 0 0 1
0 0 0 1 0
6 10
0 0 0 1 0 0 1 0 1 0
1 1 0 1 1 0 1 0 1 0
0 0 0 0 0 0 0 0 0 0
1 1 1 0 1 1 1 0 1 1
0 0 0 0 1 1 1 0 1 0
1 1 1 0 0 0 0 0 0 0
10 10
0 0 0 0 1 0 0 0 0 0
1 1 0 1 1 1 1 0 1 1
0 0 0 0 1 0 0 0 0 0
1 0 0 0 1 1 1 1 0 1
0 0 0 0 0 1 0 0 0 0
1 0 0 0 1 1 1 0 1 1
0 0 0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1 1 1
0 0 0 0 0 0 0 1 1 0
0 1 0 1 0 1 0 0 0 0
6 6
0 1 1 1 1 1
0 0 0 0 0 0
1 1 1 0 1 1
0 0 0 0 1 1
1 0 1 1 1 1
0 0 0 0 1 0
4 4
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
4 4
1 0 1 0
0 1 0 1
1 0 1 0
0 1 0 1
20 20
0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0
1 1 1 1 1 0 1 1 1 1 1 1 1 0 1 1 1 1 1 1
0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 1 1 1 0
1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 0 1 1 1 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
0 1 0 0 0 0 0 1 0 1 0 1 0 1 0 1 0 1 0 1
0 1 1 0 1 1 0 1 1 1 0 1 0 0 1 0 0 1 1 0
0 0 0 0 1 0 0 0 0 0 0 1 1 1 1 1 1 1 1 0
1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 1 1 1 0 1 1 1 1 1 0 1 1 1 0 1 1 1 1 1
0 0 0 0 0 0 1 1 1 1 1 1 1 1 0 1 1 1 1 1
0 1 1 1 1 1 0 0 0 0 1 1 1 1 0 1 1 1 1 1
0 0 0 0 0 0 0 1 0 1 1 0 0 0 0 0 0 0 0 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0




#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
using namespace std;

typedef struct node
{
int data;// 0 for island 1 for pond
bool visited;

}node;

void recMark(node **Matrix, int y, int x, int flag)//rec find and mark
{
Matrix[y][x].visited = true;
if(Matrix[y-1][x].data = flag)
recMark(Matrix, y-1, x, flag);
if(Matrix[y][x+1].data = flag)
recMark(Matrix, y, x+1, flag);
if(Matrix[y+1][x].data = flag)
recMark(Matrix, y+1, x, flag);
if(Matrix[y][x-1].data = flag)
recMark(Matrix, y, x-1, flag);



}

void loopFind(node **Matrix, int y , int x)//y and x represent the size of the matrix
{
int pondCounts = 0;
int islandCounts = 0;
for(int _y = 1; _y <= y-2; _y++ )
{
for(int _x = 1; _x <= x-2 ; _x++)
{
if(Matrix[_y][_x].visited == false)//if not visited, then visit all related
{
if(Matrix[_y][_x].data == 0)
{
recMark(Matrix, _y, _x, 0);
pondCounts++;
}
else if(Matrix[_y][_x].data == 1)
{
recMark(Matrix, _y, _x, 1);
islandCounts++;

}
}
}
}
cout<<"the number of pond: "<<pondCounts<<", and the number of island(s): "<<islandCounts<<endl;

//return tolCounts;
}


int main()
{
ifstream fin;
fin.open("PondsIslands.txt");
if(!fin)
{
cout<<"opening file failed"<<endl;
exit(-1);
}
int x =0;
int y =0;
while(!fin.eof())
{
fin>>y>>x;
node theMatrix[y+2][x+2];
for(int _x = 0;_x<x+2; _x++ )
{
for(int _y = 0; _y< y+2; _y++ )
{
theMatrix[y][x].visited = false; // mark un-visited
}
}
//setting the surrounding to 9 to signify edging
for(int temp = 0; temp < x+2; temp++)
{
theMatrix[0][temp].data = 9;
theMatrix[y+1][temp].data = 9;



}
for(int temp = 0; temp < y+2; temp++)
{
theMatrix[temp][0].data = 9;
theMatrix[temp][x+1].data = 9;
}
//putting all the number 0 or 1 into the inner of array
for(int tempY = 1; tempY <= y; tempY++)
{
for(int tempX = 1; tempX<= x; tempX++)
{
string stemp;
getline(fin, stemp);
stringstream sstemp(stemp);
sstemp>>theMatrix[tempX][tempY].data;

}
}

//cout<<"reach here "<<endl;

}
fin.close();

return 0;


}
...全文
87 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
cphj 2010-02-05
  • 打赏
  • 举报
回复
可能代码太长,分数太少吧
有空的话,再来看看
yorkzjy 2010-02-05
  • 打赏
  • 举报
回复
能啊,不知道为啥我这个帖子就是没什么人看。。
logiciel 2010-02-05
  • 打赏
  • 举报
回复
这个程序能通过编译吗?

int x =0;
int y =0;
node theMatrix[y+2][x+2];
yorkzjy 2010-02-05
  • 打赏
  • 举报
回复
谢谢楼上了,自顶
durant 2010-02-04
  • 打赏
  • 举报
回复
快要沉了,帮你顶下
yorkzjy 2010-02-04
  • 打赏
  • 举报
回复
另 :pondsIslands.txt 里面只是存储一个矩阵的。

64,692

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧