#define N 40
int a[N][2]
int num
void display()
{
for(int j=0j <numj++)
{
printf("%d ", a[j][1])
}
printf("\n")
}
void test(int i)
{
if(i == num)
{
int j
int flag = 1
if(a[0][1]+a[1][1]!=a[0][0]&a[num-1][1]+a[num-2][1]!=a[num-1][0])
{
}
for(j = 1j <num - 1j++)
{
if(a[j-1][1] + a[j][1] + a[j+1][1] != a[j][0])
flag = 0
}
if(flag)
display()
}
for(i <numi++)
{
if(a[i][1] == 0)
{
if(i == 0)
{
if(a[i][1]+a[i+1][1] != a[i][0])
{
a[i][1] = 1
test(i+1)
a[i][1] = 0
}
}
if(i >0)
{
if(a[i-1][1] + a[i][1] + a[i+1][1] != a[i][0])
{
a[i][1] = 1
test(i+1)
a[i][1] = 0
}
}
}
}
}
int main()
{
int i
printf("输入个数:\n")
scanf("%d",&num)
printf("输入数据(0~3):\n")
for(i = 0i <numi++)
{
scanf("%d",&a[i][0])
a[i][1]=0
}
for(i = 1i <num - 1i++)
{
if(a[i][0] == 3)
{
a[i-1][1] = 1
a[i][1] = 1
a[i+1][1] = 1
}
}
test(0)
}
算法思想:
1、唤庆如果有输入数字是3则输出数字中对应和祥握上中下都必为1
2、输出数组中只有为0的才能宴好为1;
3、用回溯法判断成立条件,成功则输出。
俄罗斯方快http://topic.csdn.net/t/20051201/01/4429905.html扫雷
#include<stdio.h>
#include<graphics.h>
#include<stdlib.h>
struct list
{
int x
int y
int num
int bomb
int wa
}
struct list di[10][10]
int currentx=210
int currenty=130
void initxy(void)
{
int i,j
for(i=0i<=9i++)
for(j=0j<=9j++)
{
di[j].x=i*20+200
di[j].y=j*20+120
di[j].wa=0
di[j].bomb=0
}
}
void initmu(void)
{
int i,j
setcolor(2)
rectangle(200,120,400,320)
rectangle(190,110,410,330)
setfillstyle(8,14)
floodfill(191,111,2)
for(i=0i<=9i++)
for(j=0j<=9j++)
rectangle(di[j].x,di[j].y,di[j].x+19,di[j].y+19)
outtextxy(450,200,"press 'enter' to kick"芹做启嫌如)
outtextxy(450,250,"press '\' to mark")
}
void randbomb(void)
{
int k
int i,j
randomize()
for(i=0i<=9i++)
for(j=0j<=9j++)
{
k=random(5)
if(k==2)
di[j].bomb=1
}
}
void jisuan(void)
{
int k=0
int i,j
for(i=0i<=9i++)
for(j=0j<=9j++)
{
if(i&&j&&di[i-1][j-1].bomb)
k=k+1
if(i&&di[i-1][j].bomb)
k=k+1
if(j&&di[j-1].bomb)
k=k+1
if(i<=8&&di[i+1][j].bomb)
k=k+1
if(j<=8&&di[j+1].bomb)
k=k+1
if(i<胡亏=8&&j<=8&&di[i+1][j+1].bomb)
k=k+1
if(i&&j<=8&&di[i-1][j+1].bomb)
k=k+1
if(i<=8&&j&&di[i+1][j-1].bomb)
k=k+1
di[j].num=k
k=0
}
}
void xianbomb(void)
{
int i,j
char biaoji[2]
char znum[2]
biaoji[0]=1
biaoji[1]=NULL
for(i=0i<=9i++)
for(j=0j<=9j++)
{
if(di[j].bomb==1)
outtextxy(di[j].x+2,di[j].y+2,biaoji)
else
{
itoa(di[j].num,znum,10)
setfillstyle(1,0)
bar(i*20+202,j*20+122,i*20+218,j*20+138)
outtextxy(i*20+202,j*20+122,znum)
}
}
}
void move(void)
{
int key
key=bioskey(1)
if(key)
key=bioskey(0)
if(key==0x4800)
{
if(currenty>130)
{
setcolor(0)
circle(currentx,currenty,5)
currenty-=20
setcolor(4)
circle(currentx,currenty,5)
}
else
{
setcolor(0)
circle(currentx,currenty,5)
currenty=310
setcolor(4)
circle(currentx,currenty,5)
}
}
if(key==0x4b00)
{
if(currentx>210)
{
setcolor(0)
circle(currentx,currenty,5)
currentx-=20
setcolor(4)
circle(currentx,currenty,5)
}
else
{
setcolor(0)
circle(currentx,currenty,5)
currentx=390
setcolor(4)
circle(currentx,currenty,5)
}
}
if(key==0x4d00)
{
if(currentx<390)
{
setcolor(0)
circle(currentx,currenty,5)
currentx+=20
setcolor(4)
circle(currentx,currenty,5)
}
else
{
setcolor(0)
circle(currentx,currenty,5)
currentx=210
setcolor(4)
circle(currentx,currenty,5)
}
}
if(key==0x5000)
{
if(currenty<310)
{
setcolor(0)
circle(currentx,currenty,5)
currenty+=20
setcolor(4)
circle(currentx,currenty,5)
}
else
{
setcolor(0)
circle(currentx,currenty,5)
currenty=130
setcolor(4)
circle(currentx,currenty,5)
}
}
if(key==0x1c0d)
{
int i,j
char snum[2]
snum[0]=NULL
snum[1]=NULL
i=(currentx-210)/20
j=(currenty-130)/20
if(di[j].bomb==1)
{
outtextxy(100,100,"game over")
xianbomb()
sleep(2)
exit(0)
}
if(di[j].bomb==0)
{
di[j].wa=1
setfillstyle(1,0)
bar(currentx-8,currenty-8,currentx+8,currenty+8)
setcolor(15)
itoa(di[j].num,snum,10)
outtextxy(currentx-8,currenty-8,snum)
setcolor(4)
circle(currentx,currenty,5)
}
}
if(key==0x2b5c)
{
char biaoji[2]
biaoji[0]=1
biaoji[1]=NULL
setcolor(0)
bar(currentx-8,currenty-8,currentx+8,currenty+8)
setcolor(4)
outtextxy(currentx-8,currenty-8,biaoji)
circle(currentx,currenty,5)
}
}
void success(void)
{
int k=1
int i,j
for(i=0i<=9i++)
for(j=0j<=9j++)
if(di[j].bomb==0&&di[j].wa==0)
k=0
if(k==1)
{
outtextxy(100,100,"success good")
xianbomb()
sleep(2)
exit(0)
}
}
void main(void)
{
int gd=DETECT,gm
initgraph(&gd,&gm,"")
initxy()
initmu()
randbomb()
jisuan()
setcolor(4)
circle(210,130,5)
while(1)
{
move()
success()
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)