到底怎么了?调一天了

yanjiashang 2011-05-11 11:50:26
本程序设定一个12*12的二维数组,通过运行该程序想标出所有同特定位置上值相同且相联的元素。

#include <stdio.h>
#include <time.h>
#include <stdlib.h>

#define MAX_BUBBLE 12

class BuubleGame
{
public:
BuubleGame()
{
srand(time(0));

for (int i = 0; i < MAX_BUBBLE; ++i)
{
for (int j = 0; j < MAX_BUBBLE; ++j)
{
bubble[i][j] = rand() % 4 + 1;
}
}

}

void setCellVal(int i, int j , int val)
{
bubble[i][j] = val;
}

int getCellVal(int i, int j)
{
return bubble[i][j];
}

int setSelected(int (*selectedBubble)[MAX_BUBBLE],
int row, int col, int val)
{
int sum = 0;

if (row < 0 || row >= MAX_BUBBLE ||
col < 0 || col >= MAX_BUBBLE ||
val != bubble[row][col] || selectedBubble[row][col] == 1)
{
return sum;
}

selectedBubble[row][col] = 1;
++sum;

sum += setSelected(selectedBubble, row, col - 1, val);
sum += setSelected(selectedBubble, row - 1, col, val);
sum += setSelected(selectedBubble, row, col + 1, val);
sum += setSelected(selectedBubble, row + 1, col, val);

return sum;
}

private:
int bubble[MAX_BUBBLE][MAX_BUBBLE];
};

int main(int argc, char *argv[]) {
int selecteBubble[MAX_BUBBLE][MAX_BUBBLE];
int sum = 0;
BuubleGame game;

for (int i = 0; i < MAX_BUBBLE; ++i)
{
for (int j = 0; j < MAX_BUBBLE; ++j)
{
selecteBubble[i][j] = 0;
}
}

for (int i = 0; i < MAX_BUBBLE; ++i)
{
for (int j = 0; j < MAX_BUBBLE; ++j)
{
printf("%d ", game.getCellVal(i, j));
}

printf("\n");
}

sum = game.setSelected(selecteBubble, 0, 0,
game.getCellVal(0, 0));


printf("\n");
for (int i = 0; i < MAX_BUBBLE; ++i)
{
for (int j = 0; j < MAX_BUBBLE; ++j)
{
printf("%d ", selecteBubble[i][j]);
}

printf("\n");
}

printf("sum = %d\n", sum);
}
...全文
175 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
yanjiashang 2011-05-12
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 zhao4zhong1 的回复:]

标记计算过的单元不能用1,可以用-1
[/Quote]
谢谢提醒
赵4老师 2011-05-12
  • 打赏
  • 举报
回复
标记计算过的单元不能用1,可以用-1
yanjiashang 2011-05-12
  • 打赏
  • 举报
回复
我又测了测,总算发现是哪错了,我都不意思说什么地方犯错了,只能BS自己了。
yanjiashang 2011-05-12
  • 打赏
  • 举报
回复
我也很奇怪,在我这的vs2005下结果就是不对,哪位兄弟有2005的可以测一下试。
qq120848369 2011-05-11
  • 打赏
  • 举报
回复
并查集,一行一行的从左往右走,对于每一个格子,合并它下边与右边的格子.
yanjiashang 2011-05-11
  • 打赏
  • 举报
回复
看我的例子
小程程程 2011-05-11
  • 打赏
  • 举报
回复
这段代码是干嘛用的??
yanjiashang 2011-05-11
  • 打赏
  • 举报
回复
举个例子
2 4 4 1 1 3 3 2 3 2 4 1
2 2 3 3 2 4 3 1 1 1 4 4
3 4 2 2 3 4 3 2 3 1 4 3
2 2 3 3 4 1 3 2 2 3 4 4
3 3 1 3 1 4 3 3 4 1 4 4
3 1 4 2 1 4 2 4 4 4 3 3
4 3 2 3 1 4 4 1 4 4 2 3
3 3 3 2 4 2 3 2 4 4 4 3
4 4 1 2 4 1 4 1 2 1 4 4
1 1 2 4 2 1 3 1 3 4 3 3
2 3 4 4 1 2 2 1 4 1 2 2
2 4 1 3 3 2 2 1 1 3 2 4

1 0 0 0 0 0 0 0 0 0 0 0
1 1 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
sum = 3
标出所有同0,0相联值又相等,这里是2.
bdmh 2011-05-11
  • 打赏
  • 举报
回复
晕,先把你的规则解释清楚再说
Sky-Yang 2011-05-11
  • 打赏
  • 举报
回复
4 3 2 3 2 4 1 1 4 2 2 4
4 1 4 2 2 3 1 3 3 4 3 2
3 4 1 1 2 4 3 1 3 1 2 2
1 4 4 4 1 1 2 2 3 3 2 1
4 3 4 1 4 4 4 3 1 2 2 2
4 2 4 4 2 2 3 4 2 2 4 1
4 1 1 4 1 4 4 2 2 3 4 4
2 1 2 4 4 3 1 4 4 4 4 4
1 3 4 3 3 4 2 2 2 4 2 3
2 3 2 4 4 3 4 2 1 3 1 3
2 1 2 2 4 4 1 2 4 4 1 1
1 3 1 3 1 4 2 1 2 2 3 3

0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0 0 0 0
0 1 1 1 0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0 0 0
0 0 1 1 0 0 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0 0 0 0
0 0 0 1 1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
sum = 10

这个是 4,4的情况,也没错哦
你说你程序什么地方出错啊。。。
Sky-Yang 2011-05-11
  • 打赏
  • 举报
回复
2 3 3 4 2 1 1 2 4 1 4 3
2 1 3 3 2 2 1 3 3 2 2 4
2 3 3 4 2 3 3 4 1 3 2 3
2 4 2 2 3 3 1 1 4 3 3 4
4 4 4 1 4 2 4 1 4 4 1 2
4 3 1 4 4 2 4 2 4 3 4 3
3 1 3 4 1 4 4 4 2 2 3 3
1 3 2 3 1 3 4 4 2 2 4 2
3 4 1 1 3 2 1 4 2 1 2 4
2 2 4 3 1 2 4 4 4 3 4 2
4 2 4 1 4 2 1 3 3 1 4 3
4 1 2 2 3 2 1 2 2 2 1 4

1 0 0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
sum = 4

程序没错误啊
yanjiashang 2011-05-11
  • 打赏
  • 举报
回复
看看我在Linux下的运行结果。只是将

sum = game.setSelected(selecteBubble, 0, 0,
game.getCellVal(0, 0));
改为

sum = game.setSelected(selecteBubble, 5, 5,
game.getCellVal(5, 5));

当然在Linux下0,0的情况也测了,在这里只放5,5的情况。
4 2 2 2 2 1 4 3 2 1 3 3
1 2 1 1 4 3 4 2 3 2 3 3
3 1 1 2 1 1 4 4 2 1 2 3
1 1 1 3 1 4 1 1 1 2 1 4
4 1 1 3 2 4 1 4 4 1 2 4
1 1 4 2 2 1 1 2 1 1 4 1
4 1 1 4 2 1 4 1 1 4 3 3
3 3 2 2 4 3 2 4 4 1 2 1
1 2 2 1 2 2 1 2 2 1 1 3
1 4 3 2 4 2 4 2 4 1 4 3
4 1 3 3 1 4 3 1 1 4 1 2
1 1 3 2 1 4 4 1 3 3 2 2

0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 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 0 0 0
0 0 0 0 0 0 1 0 0 0 0 0
0 0 0 0 0 1 1 0 0 0 0 0
0 0 0 0 0 1 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
sum = 7
赵4老师 2011-05-11
  • 打赏
  • 举报
回复
种子填充算法。仅供参考
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <graphics.h>
int xs[10000];
int ys[10000];
int i=0,xx,yy;
int fc,bc;
void push(int x,int y) {
xs[i]=x;
ys[i]=y;
if (i<10000-1) {
i=i+1;
} else {
printf("stack overflow!\n");
exit(1);
}
}
void pop(void) {
i=i-1;
xx=xs[i];
yy=ys[i];
}
int check(int x,int y) {
int c;

c=getpixel(x,y); /* 获取当前点的颜色 */
return ((c!=bc)&&(c!=fc));/* 如果颜色为边界色或填充色则不填充 */
}
void seedfilling(int x,int y,int fill_color,int boundary_color) {
fc=fill_color;
bc=boundary_color;
push(x,y);
while (1) {
if (i<=0) return;
pop();
if (check(xx,yy)) {
putpixel(xx, yy, 14);getch(); /* 加上这行显示当前填充状态 */

putpixel(xx, yy, fill_color); /* 画点 */

if (check(xx-1,yy )) push(xx-1,yy );
if (check(xx ,yy+1)) push(xx ,yy+1);
if (check(xx ,yy-1)) push(xx ,yy-1);
if (check(xx+1,yy )) push(xx+1,yy );
/* 去掉下面四句就是四连通 */
if (check(xx-1,yy-1)) push(xx-1,yy-1);
if (check(xx-1,yy+1)) push(xx-1,yy+1);
if (check(xx+1,yy-1)) push(xx+1,yy-1);
if (check(xx+1,yy+1)) push(xx+1,yy+1);
}
}
}
void main() {
int a,b,color;
int gdriver = DETECT, gmode, errorcode;
int poly[10];

initgraph(&gdriver, &gmode, "d:\\bc\\bgi");
a=150;
b=140;
color=4;
poly[0]=110;/* 第一个点的x坐标以及y坐标 */
poly[1]=110;
poly[2]=200;/* 第二点 */
poly[3]=105;
poly[4]=170;/* 第三点 */
poly[5]=120;
poly[6]=150;/* 第四点 */
poly[7]=170;
poly[8]=110;/* 多边形的起点与终点一样 */
poly[9]=110;
drawpoly(5,poly);/* 显示各点连接起来的多边形 */

/* 保证边界对八连通是封闭的 */
setviewport(0,1,600,300,0);
drawpoly(5,poly);/* 显示各点连接起来的多边形 */
setviewport(1,0,600,300,0);
drawpoly(5,poly);/* 显示各点连接起来的多边形 */
setviewport(1,1,600,300,0);
drawpoly(5,poly);/* 显示各点连接起来的多边形 */

/* 恢复默认viewport */
setviewport(0,0,600,300,0);

seedfilling(a,b,color,15); /* 种子填充多边形 */

getch();
closegraph();
}
yanjiashang 2011-05-11
  • 打赏
  • 举报
回复
我将以上代码拖到fedora下,可以正确运行,但在vs2005下不行,真不知道为什么?
wtbike 2011-05-11
  • 打赏
  • 举报
回复
话说例子都看不懂。。。
yanjiashang 2011-05-11
  • 打赏
  • 举报
回复
谁能帮我看看?

64,682

社区成员

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

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