C++實(shí)現(xiàn)連連看游戲
本文實(shí)例為大家分享了C++實(shí)現(xiàn)連連看游戲的具體代碼,供大家參考,具體內(nèi)容如下
這個(gè)項(xiàng)目還是挺不錯(cuò)的,運(yùn)行后也比較有意思,可以看看。
#include<iostream> #include<conio.h> #include<time.h> #include<stdlib.h> #include<math.h> #include<string.h> using namespace std; ? void Initial(int a[8][8]);/*初始化*/ int Output(int a[8][8]);/*輸出*/ void Sort(int a[8][8]);/*重排*/ void Change(int a[8][8],int x,int y,int x1,int y1);/*交換數(shù)組內(nèi)兩坐標(biāo)對(duì)的符號(hào)值*/ void Judge(int a[8][8],int b[4]);/*判斷是否能夠消除*/ void Zero(int a[8][8],int b[4]);/*設(shè)置為零,即:消除*/ ? int Two(int a[8][8],int b[4]);/*兩條線*/ ? void Check(int a[8][8],int x,int y,char c[5]);/*返回某點(diǎn)上下左右的情況*/ ? int ?UpDown(int a[8][8],int b[4],int g); int ?LeftRight(int a[8][8],int b[4],int g); ? int main() { ?? ?int flag=1,flag1=1; ?? ?//flag用于標(biāo)記是否已經(jīng)初始化,flag=1表示未初始化;flag1標(biāo)記用戶是否選擇了退出,flag1=1表示未選擇。 ?? ?int a[8][8]={0};//0 表示空格。使用數(shù)組a[8][8]存放符號(hào)值,其中僅用了下標(biāo)1-6. ?? ?int b[4];/*接收坐標(biāo)*/ ?? ?char ch; ?? ?do? ?? ?{ ?? ??? ? ? ?int o;//用于標(biāo)記是否已經(jīng)獲勝。 ?? ? ? ? ? ?if(flag) ?? ??? ??? ?{ ?? ??? ??? ??? ?Initial(a); ?? ??? ??? ??? ?flag=0; ?? ??? ??? ?} ?? ??? ??? ?o=Output(a); ?? ??? ??? ?if(o==0) ?? ??? ??? ?{ ?? ??? ??? ??? ?char ch; ?? ??? ??? ??? ?system("cls"); ?? ??? ??? ??? ?cout << "您已經(jīng)獲勝!!按Enter鍵確定。" << endl; ?? ??? ??? ??? ?while(1)//確保用戶輸入 的是Enter鍵 ?? ??? ??? ??? ?{ ?? ??? ??? ??? ??? ?ch=getch(); ?? ??? ??? ??? ??? ?if(ch==13) ?? ??? ??? ??? ??? ??? ?break; ?? ??? ??? ??? ?} ?? ??? ??? ??? ?flag=1;//標(biāo)記為未初始化。 ?? ??? ??? ??? ?system("cls"); ?? ??? ??? ??? ?continue;//跳出本次循環(huán),開始新的一局。?? ??? ??? ??? ? ?? ??? ??? ?} ?? ??? ??? ?while(1)//確保用戶的輸入符合要求 ?? ??? ??? ?{ ?? ??? ??? ??? ?ch=getch(); ?? ??? ??? ??? ?if(ch=='a'||ch=='A' ?? ??? ??? ??? ? ? ||ch=='b'||ch=='B' ?? ??? ??? ??? ? ? ||ch=='c'||ch=='C' ?? ??? ??? ??? ? ? ||ch=='d'||ch=='D') ?? ??? ??? ??? ? ?? ??? ??? ??? ?{ ?? ??? ??? ??? ??? ?putchar(ch); ?? ??? ??? ??? ??? ?break; ?? ??? ??? ??? ?} ?? ??? ??? ?} ?? ??? ??? ?switch(ch)//根據(jù)用戶的選擇進(jìn)行處理。 ?? ??? ??? ?{ ?? ??? ??? ??? ?case 'a': ?? ??? ??? ??? ?case 'A': ?? ??? ??? ??? ??? ?{ ?? ??? ??? ??? ??? ??? ?cout << endl; ?? ??? ??? ??? ??? ??? ?cout << "請(qǐng)輸入坐標(biāo):" << endl; ?? ??? ??? ??? ??? ??? ?fflush(stdin); ?? ??? ??? ??? ??? ??? ?cin >> b[0] >> b[1] >> b[2] >> b[3]; ?? ??? ??? ??? ??? ??? ?break; ?? ??? ??? ??? ??? ?} ?? ??? ??? ??? ?case 'b': ?? ??? ??? ? ? ?case 'B': ?? ??? ??? ??? ??? ?{ ?? ??? ??? ??? ??? ??? ?Sort(a); ?? ??? ??? ??? ??? ??? ?break; ?? ??? ??? ??? ??? ?} ?? ??? ??? ??? ?case 'c': ?? ??? ??? ??? ?case 'C': ?? ??? ??? ??? ??? ?{ ?? ??? ??? ??? ??? ??? ?flag=1; ?? ??? ??? ??? ??? ??? ?break; ?? ??? ??? ??? ??? ?} ?? ??? ??? ??? ?case 'd': ?? ??? ??? ??? ?case 'D': ?? ??? ??? ??? ??? ?{ ?? ??? ??? ??? ??? ??? ?flag1=0; ?? ??? ??? ??? ??? ??? ?break; ?? ??? ??? ??? ??? ?} ?? ??? ??? ?} ?? ??? ??? ?if(ch!='a'&&ch!='A') ?? ??? ??? ??? ?system("cls"); ?? ??? ??? ?else ? ? ? ? ? ? ? ? //若用戶輸入坐標(biāo),則進(jìn)行判斷 ?? ??? ??? ?{ ?? ??? ??? ??? ?Judge(a,b); ?? ??? ??? ??? ?system("cls"); ?? ??? ??? ?} ?? ?}while(flag1); ?? ?cout << "游戲已退出!" << endl; } ? void Initial(int a[8][8])//用以初始化數(shù)組存放的符號(hào)值 { ?? ? ?? ?int i,j,m=0,k=1;//k值表示符號(hào)類別,m表示每個(gè)符號(hào)必須出現(xiàn)以2的倍數(shù)出現(xiàn),出現(xiàn)兩次 ?? ?for(i=1;i<=6;i++) ?? ??? ?for(j=1;j<=6;j++) ?? ??? ?{ ?? ??? ? ? if(k>12) ?? ??? ??? ? ? k=k%12;//k值大于12后,符號(hào)類型循環(huán),最多11種符號(hào),0表示空 ? ? ? ? ? ?a[i][j]=k; ?? ??? ? ? m++; ?? ??? ? ? if(m%2==0)//每種符號(hào)賦兩個(gè), ?? ??? ? ? ? ?k++; ?? ??? ?} ?? ?Sort(a);//進(jìn)行隨機(jī)重排。 } ? int Output(int a[8][8]) ? ? ? ? ? /*輸出,對(duì)應(yīng)符號(hào) ? ?☆ ★ ○ ● ◎ ◇ ■ ∷ ⊙ ﹫ ¤ ﹩ ?? ? ?? ? 同時(shí),判斷是否勝利 */ { ?? ?int i,j,flag1=0;//flag1用于記錄輸出的空格數(shù),當(dāng)flag1=64時(shí),表示所有的都已消完,獲勝。 ?? ?for(i=0;i<8;i++) ?? ?{ ?? ??? ?for(j=0;j<8;j++) ?? ??? ?{ ?? ??? ??? ?switch(a[i][j])//根據(jù)二維數(shù)組a中的值輸出相應(yīng)的符號(hào) ?? ??? ??? ?{ ?? ??? ??? ? ? case 0: ?? ??? ??? ??? ? ? flag1++; ?? ??? ??? ??? ? ? cout << " ?"; ?? ??? ??? ??? ? ? break; ?? ??? ??? ? ? case 1: ?? ??? ??? ??? ? ? cout << "☆"; ?? ??? ??? ??? ? ? break;?? ??? ??? ??? ? ?? ?? ??? ??? ? ? case 2: ?? ??? ??? ??? ? ? cout << "★"; ?? ??? ??? ??? ? ? break; ?? ??? ??? ? ? case 3: ?? ??? ??? ??? ? ? cout << "○"; ?? ??? ??? ??? ? ? break; ?? ??? ??? ? ? case 4: ?? ??? ??? ??? ? ? cout << "●"; ?? ??? ??? ??? ? ? break; ?? ??? ??? ? ? case 5: ?? ??? ??? ??? ? ? cout << "◎"; ?? ??? ??? ??? ? ? break; ?? ??? ??? ? ? case 6: ?? ??? ??? ??? ? ? cout << "◇"; ?? ??? ??? ??? ? ? break; ?? ??? ??? ? ? case 7: ?? ??? ??? ??? ? ? cout << "■"; ?? ??? ??? ??? ? ? break; ?? ??? ??? ? ? case 8: ?? ??? ??? ??? ? ? cout << "∷"; ?? ??? ??? ??? ? ? break; ?? ??? ??? ? ? case 9: ?? ??? ??? ??? ? ? cout << "⊙"; ?? ??? ??? ??? ? ? break; ?? ??? ??? ? ? case 10: ?? ??? ??? ??? ? ? cout << "﹫"; ?? ??? ??? ??? ? ? break; ?? ??? ??? ? ? case 11: ?? ??? ??? ??? ? ? cout << "¤"; ?? ??? ??? ??? ? ? break; ?? ??? ??? ? ? case 12: ?? ??? ??? ??? ? ? cout << "﹩"; ?? ??? ??? ??? ? ? break; ?? ??? ??? ?} ?? ??? ?} ?? ??? ?if(i>0&&i<7) ?? ??? ?{ ?? ??? ??? ?cout << '\t' << '\t';?? ??? ??? ? ?? ??? ??? ?for(j=1;j<=6;j++) ?? ??? ??? ?{ ?? ??? ??? ??? ?cout << "<" << i << "," << j << ">,";?? ??? ??? ??? ? ?? ??? ??? ?} ?? ??? ?} ?? ??? ?cout << endl;?? ??? ? ?? ?} ?? ?if(flag1==64) ?? ??? ??? ?return 0; ?? ?cout << endl; ?? ?cout << "選項(xiàng):" << endl; ?? ?cout << '\t' << '\t' << 'a' << '\t' << "輸入坐標(biāo);" << endl; ?? ?cout << '\t' << '\t' << 'b' << '\t' << "重排;" << endl; ?? ?cout << '\t' << '\t' << 'c' << '\t' << "重新開始;" << endl; ?? ?cout << '\t' << '\t' << 'd' << '\t' << "退出;" << endl; ?? ?cout << endl; ?? ?cout << "坐標(biāo)輸入格式:" << endl << "1 2" << endl << "3 6" << endl <<"然后按Enter。" << endl; ?? ?cout << "選擇:"; ?? ?return 1; } ? void Change(int a[8][8],int x,int y,int x1,int y1)/*交換*/ { ?? ?int temp; ?? ?temp=a[x][y]; ?? ?a[x][y]=a[x1][y1]; ?? ?a[x1][y1]=temp; } ? void Sort(int a[8][8])//對(duì)數(shù)組內(nèi)的符號(hào)隨機(jī)交換,打亂符號(hào)的位置,注意坐標(biāo)必須是1-6之間 { ?? ?int x1,y1,i,j; ?? ?srand((unsigned)time(NULL)); ?? ?for(i=1;i<=6;i++) ?? ??? ?for(j=1;j<=6;j++) ?? ??? ?{ ?? ??? ??? ?x1=rand()%(6-1+1)+1; ?? ??? ??? ?y1=rand()%(6-1+1)+1; ?? ??? ??? ?//隨機(jī)產(chǎn)生一個(gè)坐標(biāo)(x1,y1); ?? ??? ??? ?Change(a,i,j,x1,y1);//將a(i,j)的值與a(x1,y1)的值交換 ?? ??? ?} ? } void Judge(int a[8][8],int b[4])/*判斷是否能夠消除*/ { ?? ? ? ?if(a[b[0]][b[1]]==a[b[2]][b[3]]&&!(b[0]==b[2]&&b[1]==b[3])) ? ?{ ?? ? ?? ?? ? ? if((b[0]==b[2]&&abs(b[3]-b[1])==1) ?? ??? ? ? ||(b[1]==b[3]&&abs(b[0]-b[2])==1))/*判斷兩點(diǎn)緊鄰*/ ?? ? ? {?? ? ? ?? ??? ? ? Zero(a,b); ?? ? ? ? ? return; ?? ? ? } ?? ? ? if((b[0]==b[2]&&(b[0]==1||b[0]==6)) ?? ??? ? ? ||(b[1]==b[3]&&(b[1]==6||b[1]==1)))/*判斷兩點(diǎn)在邊界上*/ ?? ? ? { ?? ??? ? ? Zero(a,b); ?? ??? ? ? return; ?? ? ? } ?? ? ? if(b[0]==b[2]) ? ? ? ? ? ? ? ? ? ? ?/*兩點(diǎn)在同一行上*/ ?? ? ? { ?? ??? ? ? int n,m;//n為兩個(gè)數(shù)中較大的值,m為兩數(shù)中較小的。 ?? ??? ? ? n=b[3]>b[1]?b[3]:b[1]; ?? ??? ? ? m=b[3]>b[1]?b[1]:b[3]; ?? ??? ? ? n=n-1; ?? ??? ? ? while(n>m) ?? ??? ? ? { ?? ??? ??? ? ? //從右往左依次查看,看在此行,兩點(diǎn)之間是否均為空格 ?? ??? ??? ? ? if(a[b[0]][n]==0) ?? ??? ??? ??? ? ? n--; ?? ??? ??? ? ? else ?? ??? ??? ??? ? ?break; ?? ??? ? ? } ?? ??? ? ? if(n==m)//在此行,兩點(diǎn)之間均為空格。 ?? ??? ? ? {?? ? ? ?? ??? ??? ? ? Zero(a,b); ? ? ? ? ? ? ? ?return; ?? ??? ? ? } ?? ? ? } ?? ? ? if(b[1]==b[3]) ? ? ? ?/*兩點(diǎn)在同一列*/ ?? ? ? { ?? ??? ? ? int n,m;//n為兩個(gè)數(shù)中較大的值,m為兩數(shù)中較小的。 ?? ??? ? ? n=b[2]>b[0]?b[2]:b[0]; ?? ??? ? ? m=b[2]>b[0]?b[0]:b[2]; ?? ??? ? ? n=n-1; ?? ??? ? ? while(n>m) ?? ??? ? ? { ? ? ? ? ? ? ? ?//從下往上依次查看,看在此列,兩點(diǎn)之間是否均為空格 ?? ??? ??? ? ? if(a[n][b[1]]==0) ?? ??? ??? ??? ? ? n--; ?? ??? ??? ? ? else ?? ??? ??? ??? ? ? break; ?? ??? ? ? } ?? ??? ? ? if(n==m)//在此列,兩點(diǎn)之間均為空格 ?? ??? ? ? { ?? ??? ??? ? ? Zero(a,b); ?? ??? ??? ? ? return; ?? ??? ? ? } ? ?? ? ? } ?? ? ? if(1)//為了添加局部變量i,所以用if(1),優(yōu)點(diǎn):使變量i存在的時(shí)間盡可能短。 ?? ? ? { ?? ??? ? ? int i;//標(biāo)記是否符合兩條線的情況。i=1表示符合, ?? ??? ? ? i=Two(a,b); ?? ??? ? ? if(i==1) ?? ??? ? ? ? return; ?? ? ? } ?? ? ? if(1)//為了添加局部變量f,w,e,所以用if(1),優(yōu)點(diǎn):使變量f,e,w存在的時(shí)間盡可能短。 ?? ? ? { ?? ??? ? ? //三條線的情況 ?? ??? ? ? int f,w; ?? ??? ? ? int e; ?? ??? ? ? char c1[5],c2[5],c3[5]="1111"; ?? ??? ? ? //檢查(b[0],b[1]),(b[2],b[3])上、下、左、右的情況 ?? ??? ? ? Check(a,b[0],b[1],c1); ?? ??? ? ? Check(a,b[2],b[3],c2); ?? ??? ? ? if(strcmp(c1,c3)==0||strcmp(c2,c3)==0)//兩點(diǎn)中只要有一點(diǎn)四周均不為空,兩點(diǎn)就不能連通,即:不可消除。 ?? ??? ??? ? ? return; ?? ??? ? ? for(e=0;e<=3;e++)//比較兩點(diǎn)四周的情況 ?? ??? ? ? { ?? ??? ??? ? ? int r[4]; ?? ??? ??? ? ? //b數(shù)組中的值不可變化,因此用r數(shù)組接收b數(shù)組中的值 ?? ??? ? ? ? ? r[0]=b[0]; ?? ??? ??? ? ? r[1]=b[1]; ?? ??? ??? ? ? r[2]=b[2]; ?? ??? ??? ? ? r[3]=b[3]; ?? ??? ??? ? ? if(c1[e]==c2[e]&&c1[e]=='0') ?? ??? ??? ? ? { ?? ??? ??? ??? ? ? switch(e) ?? ??? ??? ??? ? ? { ?? ??? ??? ??? ? ? ? case 0://兩點(diǎn)上方均有空格 ?? ??? ??? ??? ??? ??? ? ? //依次檢查(b[0],b[1])上方的點(diǎn)是否為空格(直到邊界),若為空格則檢查該點(diǎn)與(b[2],b[3]) ?? ??? ??? ??? ??? ??? ? ? //是否能通過兩條線連通 ?? ??? ??? ??? ??? ??? ? ? while(r[0]>0) ?? ??? ??? ??? ??? ??? ? ? { ?? ??? ??? ??? ??? ??? ??? ? ? r[0]=r[0]-1; ?? ??? ??? ??? ??? ??? ??? ? ? if(a[r[0]][r[1]]!=0)//只要不為空格就已說明不可連通 ?? ??? ??? ??? ??? ??? ??? ??? ? ? break; ?? ??? ??? ??? ??? ??? ??? ? ? if(Two(a,r)==1) ?? ??? ??? ??? ??? ??? ??? ? ? { ?? ?? ??? ??? ??? ??? ??? ??? ??? ? ? Zero(a,b); ?? ??? ??? ??? ??? ??? ??? ??? ? ? return; ?? ??? ??? ??? ??? ??? ??? ? ? } ?? ??? ??? ??? ??? ??? ? ? } ?? ??? ??? ??? ??? ??? ? ? break; ?? ??? ??? ??? ??? ? case 1://兩點(diǎn)下方均有空格 ?? ??? ??? ??? ??? ??? ? ? //依次檢查(b[0],b[1])下方的點(diǎn)是否為空格(直到邊界),若為空格則檢查該點(diǎn)與(b[2],b[3]) ?? ??? ??? ??? ??? ??? ? ? //是否能通過兩條線連通 ?? ??? ??? ??? ??? ??? ? ? while(r[0]<8) ?? ??? ??? ??? ??? ??? ? ? { ?? ??? ??? ??? ??? ??? ??? ? ? r[0]=r[0]+1; ?? ??? ??? ??? ??? ??? ??? ? ? if(a[r[0]][r[1]]!=0)//只要不為空格就已說明不可連通 ?? ??? ??? ??? ??? ??? ??? ??? ? ? break; ?? ??? ??? ??? ??? ??? ??? ? ? if(Two(a,r)==1) ?? ??? ??? ??? ??? ??? ??? ? ? { ?? ??? ??? ??? ??? ??? ??? ??? ? ? Zero(a,b); ?? ??? ??? ??? ??? ??? ??? ??? ? ? return; ?? ??? ??? ??? ??? ??? ??? ? ? } ?? ??? ??? ??? ??? ??? ? ? } ?? ??? ??? ??? ? ? ? ? ? ? break; ?? ??? ??? ??? ??? ? case 2: ?? ??? ??? ??? ??? ??? ? //兩點(diǎn)左邊均有空格 ?? ??? ??? ??? ??? ??? ? ? //依次檢查(b[0],b[1])左邊的點(diǎn)是否為空格(直到邊界),若為空格則檢查該點(diǎn)與(b[2],b[3]) ?? ??? ??? ??? ??? ??? ? ? //是否能通過兩條線連通 ?? ??? ??? ??? ??? ??? ? ? while(r[1]>0) ?? ??? ??? ??? ??? ??? ? ? { ?? ??? ??? ??? ??? ??? ??? ? ? r[1]=r[1]-1; ?? ??? ??? ??? ??? ??? ??? ? ? if(a[r[0]][r[1]]!=0)//只要不為空格就已說明不可連通 ?? ??? ??? ??? ??? ??? ??? ??? ? ? break; ?? ??? ??? ??? ??? ??? ??? ? ? if(Two(a,r)==1) ?? ??? ??? ??? ??? ??? ??? ? ? { ?? ??? ??? ??? ??? ??? ??? ??? ? ? Zero(a,b); ?? ??? ??? ??? ??? ??? ??? ??? ? ? return; ?? ??? ??? ??? ??? ??? ??? ? ? } ? ?? ??? ??? ??? ??? ??? ? ? } ?? ??? ??? ??? ??? ??? ? ? break; ?? ??? ??? ??? ??? ? case 3: ?? ??? ??? ??? ??? ??? ? //兩點(diǎn)右邊均有空格 ?? ??? ??? ??? ??? ??? ? ? //依次檢查(b[0],b[1])右邊的點(diǎn)是否為空格(直到邊界),若為空格則檢查該點(diǎn)與(b[2],b[3]) ?? ??? ??? ??? ??? ??? ? ? //是否能通過兩條線連通 ?? ??? ??? ??? ??? ??? ? ? while(r[1]<8) ?? ??? ??? ??? ??? ??? ? ? { ?? ??? ??? ??? ??? ??? ??? ? ? r[1]+=1; ?? ??? ??? ??? ??? ??? ??? ? ? if(a[r[0]][r[1]]!=0)//只要不為空格就已說明不可連通 ?? ??? ??? ??? ??? ??? ??? ??? ? ? break; ?? ??? ??? ??? ??? ??? ??? ? ? if(Two(a,r)==1) ?? ??? ??? ??? ??? ??? ??? ? ? { ?? ??? ??? ??? ??? ??? ??? ??? ? ? Zero(a,b); ?? ??? ??? ??? ??? ??? ??? ??? ? ? return; ?? ??? ??? ??? ??? ??? ??? ? ? } ?? ??? ??? ??? ??? ??? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? ? ?break; ?? ??? ??? ??? ? ? } ?? ??? ??? ? ? } ?? ??? ? ? } ? ? ? ? ? ? ?f=b[2]-b[0];//f>0表示(b[2],b[3])在(b[0],b[1])下方,f<0表示(b[2],b[3])在(b[0],b[1])上方 ? ?? ??? ? ? w=b[3]-b[1];//w>0表示(b[2],b[3])在(b[0],b[1])右方,w<0表示(b[2],b[3])在(b[0],b[1])左方 ? ? ?? ??? ? ? if(f>0&&w>0) ? ? ? ? ? ? ? ? ? ? ? ?/*(b[2],b[3])在(b[0],b[1])右下*/ ?? ??? ? ? { ?? ??? ??? ? ? if(c2[0]=='0'&&c1[1]=='0') //(b[2],b[3])的上方為空格,(b[0],b[1])的下方為空格 ?? ??? ??? ? ? {? ?? ? ? ? ? ? ? ? ?if(UpDown(a,b,1)) ?? ??? ??? ??? ??? ? ? return; ?? ??? ??? ? ? } ? ? ? ? ? ? ? ?if(c2[2]=='0'&&c1[3]=='0')//(b[2],b[3])的左邊為空格,(b[0],b[1])的右邊為空格 ?? ??? ??? ? ? { ?? ??? ??? ??? ? ? if(LeftRight(a,b,1)) ?? ??? ??? ??? ??? ? ? return; ?? ??? ??? ? ? } ?? ??? ? ? } ?? ??? ? ? if(f<0&&w>0) ? ? ? ? ? ? ? ? ? ? ? /*(b[2],b[3])在(b[0],b[1])右上*/ ?? ??? ? ? { ?? ??? ??? ? ? if(c2[1]=='0'&&c1[0]=='0') ? //(b[2],b[3])的下方為空格,(b[0],b[1])的上方為空格 ?? ??? ??? ? ? { ?? ??? ??? ??? ? ? if(UpDown(a,b,0)) ?? ??? ??? ??? ??? ? ? return; ?? ??? ??? ? ? } ?? ??? ??? ? ? if(c2[2]=='0'&&c1[3]=='0')//(b[2],b[3])的左邊為空格,(b[0],b[1])的右邊為空格 ?? ??? ??? ? ? { ?? ??? ??? ??? ? ? if(LeftRight(a,b,1)) ?? ??? ??? ??? ??? ? ? return; ?? ??? ??? ? ? } ?? ??? ? ? } ?? ??? ? ? if(f>0&&w<0) ? ? ? ? ? ? ? ? ? ? /*(b[2],b[3])在(b[0],b[1])左下*/ ?? ??? ? ? { ?? ??? ??? ? ? if(c2[0]=='0'&&c1[1]=='0')//(b[2],b[3])的上方為空格,(b[0],b[1])的下方為空格 ?? ??? ??? ? ? { ?? ??? ??? ??? ? ? ?if(UpDown(a,b,1)) ?? ??? ??? ??? ??? ? ? return; ?? ??? ??? ? ? } ? ? ? ? ? ? ? ?if(c2[3]=='0'&&c1[2]=='0')//(b[2],b[3])的右邊為空格,(b[0],b[1])的左邊為空格 ?? ??? ??? ? ? { ?? ??? ??? ??? ? ? if(LeftRight(a,b,0)) ?? ??? ??? ??? ??? ? ? return; ?? ??? ??? ? ? } ?? ??? ? ? } ?? ??? ? ? if(f<0&&w<0) ? ? ? ? ? ? ? ? ? ?/*(b[2],b[3])在(b[0],b[1])左上*/ ?? ??? ? ? { ?? ??? ??? ? ? if(c2[1]=='0'&&c1[0]=='0') //(b[2],b[3])的下方為空格,(b[0],b[1])的上方為空格 ?? ??? ??? ? ? { ?? ??? ??? ??? ? ? if(UpDown(a,b,0)) ?? ??? ??? ??? ??? ? ? return; ?? ??? ??? ? ? } ? ? ? ? ? ? ? ?if(c2[3]=='0'&&c1[2]=='0')//(b[2],b[3])的右邊為空格,(b[0],b[1])的左邊為空格 ?? ??? ??? ? ? { ?? ??? ??? ??? ? ? if(LeftRight(a,b,0)) ?? ??? ??? ??? ??? ? ? return; ?? ??? ??? ? ? } ? ?? ??? ? ? } ? ?? ??? ? ?? ?? ??? ?} ? ?? ?} ?? ? ?? ? } void Zero(int a[8][8],int b[4])/*設(shè)置為零*/ { ?? ?a[b[0]][b[1]]=0; ?? ?a[b[2]][b[3]]=0; } void Check(int a[8][8],int x,int y,char c[5])/*返回某點(diǎn)上下左右的情況*/ { ?? ?//1 表示不為空格,0表示為空格, ?? ?//c[0],c[1],c[2],c[3]分別表示某點(diǎn)上下左右的情況 ? ? ?if(a[x-1][y]==0)//(x,y)上一格為空 ?? ??? ? c[0]='0'; ?? ? else ?? ??? ? c[0]='1'; ? ? ? ?if(a[x+1][y]==0)//(x,y)下一格為空 ?? ??? ? c[1]='0'; ?? ? else ?? ??? ? c[1]='1'; ? ?? ? if(a[x][y-1]==0)//(x,y)左一格為空 ?? ??? ? c[2]='0'; ?? ? else ?? ??? ? c[2]='1'; ? ?? ? if(a[x][y+1]==0)//(x,y)右一格為空 ?? ??? ? c[3]='0'; ?? ? else ?? ??? ? c[3]='1'; ?? ? c[4]='\0'; } int Two(int a[8][8],int b[4]) ? ?/*兩條線*/ { ?? ?int m,n; ?? ?int c,d; //(c,d)為可動(dòng)檢驗(yàn)點(diǎn)坐標(biāo) ? ?? ?n=b[2]-b[0]; ? ? ? ? ?/*行間距*/ ?? ?m=b[3]-b[1]; ? ? ? ? /*列間距*/ ?? ?d=b[2]; ?? ?c=b[3]-m;//b[1] ?? ?while(c!=b[3])// 驗(yàn)證是否存在水平通道(b[2],b[3])到(b[2],b[1])。 ?? ?{ ?? ??? ?if(a[d][c]==0) ?? ??? ??? ?c+=(m/abs(m)); ?? ??? ?else ?? ??? ??? ?break; ?? ?} ?? ?if(c==b[3])//存在水平通道 ?? ?{ ?? ??? ?c=b[3]-m; ?? ??? ?while(d!=b[0])//驗(yàn)證是否存在豎直通道(b[2],b[1])到(b[0],b[1])。 ?? ??? ?{ ?? ??? ??? ?if(a[d][c]==0) ?? ??? ??? ??? ?d-=(n/abs(n)); ?? ??? ??? ?else ?? ??? ??? ??? ?break; ?? ??? ?} ?? ??? ?if(d==b[0])//存在豎直通道。 ?? ??? ?{ ?? ??? ??? ?Zero(a,b); ?? ??? ??? ?return 1; ?? ??? ?} ?? ??? ? ?? ?} ?? ?c=b[3]; ?? ?d=b[2]-n; ?? ?while(d!=b[2])//驗(yàn)證是否存在豎直通道(b[2],b[3])到(b[0],b[3])。 ?? ?{ ?? ??? ?if(a[d][c]==0) ?? ??? ??? ?d+=(n/abs(n)); ?? ??? ?else ?? ??? ??? ?break; ?? ?} ?? ?if(d==b[2])//存在豎直通道 ?? ?{ ?? ??? ? ?? ??? ?d=b[2]-n; ?? ? ? ?while(c!=b[1])// 驗(yàn)證是否存在水平通道(b[0],b[3])到(b[0],b[1])。 ?? ??? ?{ ?? ??? ??? ?if(a[d][c]==0) ?? ??? ??? ??? ?c-=(m/abs(m)); ?? ??? ??? ?else ?? ??? ??? ??? ?break; ?? ??? ?} ?? ??? ?if(c==b[1]) ?? ??? ?{ ?? ??? ??? ?Zero(a,b); ?? ??? ??? ?return 1; ?? ??? ?} ?? ??? ?else ?? ??? ??? ?return 0; ?? ?}?? ? } int ?UpDown(int a[8][8],int b[4],int g) { ?? ?int r[4],i; ?? ?for(i=0;i<4;i++) ?? ??? ?r[i]=b[i]; ?? ?//g=1表示(b[2],b[3])在(b[0],b[1])的下方,g=0表示(b[2],b[3])在(b[0],b[1])的上方 ?? ?if(g) ?? ?{ ? ?? ??? ? //依次檢查(b[2],b[3])上邊的點(diǎn)是否為空格(直到(b[0],b[3])下方緊鄰點(diǎn)),若為空格則檢查該點(diǎn)與(b[0],b[1]) ?? ??? ?//是否能通過兩條線連通 ?? ??? ?r[0]=r[0]+1; ?? ??? ?while(r[2]>=r[0]) ?? ??? ?{ ? ? ? ? ? ?r[2]-=1; ?? ??? ? ? if(Two(a,r)==1) ?? ??? ? ? { ?? ??? ??? ?Zero(a,b); ? ? ? ? ? ? return 1; ?? ??? ? ? } ?? ??? ?} ?? ?} ?? ?else ?? ?{ ?? ??? ? //依次檢查(b[2],b[3])下邊的點(diǎn)是否為空格(直到(b[0],b[3])上方緊鄰點(diǎn)),若為空格則檢查該點(diǎn)與(b[0],b[1]) ?? ??? ?//是否能通過兩條線連通 ?? ??? ?r[0]=r[0]-1; ?? ??? ?while(r[2]<=r[0]) ?? ??? ?{ ? ? ? ? ? ?r[2]+=1; ?? ??? ? ? if(Two(a,r)==1) ?? ??? ? ? { ?? ??? ??? ?Zero(a,b); ? ? ? ? ? ? return 1; ?? ??? ? ? } ?? ??? ?} ?? ?} ? ? return 0; } ? int LeftRight(int a[8][8],int b[4],int g) { ?? ?int r[4],i; ?? ?for(i=0;i<4;i++) ?? ??? ?r[i]=b[i]; ? ? //g=1表示(b[2],b[3])在(b[0],b[1])的右方,g=0表示(b[2],b[3])在(b[0],b[1])的左方 ?? ?if(g) ?? ?{ ? ? ? ? //依次檢查(b[2],b[3])左邊的點(diǎn)是否為空格(直到(b[2],b[1]右邊緊鄰的點(diǎn)),若為空格則檢查該點(diǎn)與(b[0],b[1]) ?? ??? ?//是否能通過兩條線連通 ?? ??? ?r[1]=r[1]+1; ?? ??? ?while(r[3]>=r[1]) ?? ??? ?{ ?? ??? ? ? r[3]-=1; ?? ??? ? ? if(Two(a,r)==1) ?? ??? ? ? { ?? ??? ??? ? ? Zero(a,b); ?? ??? ??? ? ? return 1; ?? ??? ? ? } ?? ??? ?} ?? ?} ?? ?else ?? ?{ ?? ??? ?//依次檢查(b[2],b[3])右邊的點(diǎn)是否為空格(直到(b[2],b[1]左邊緊鄰的點(diǎn)),若為空格則檢查該點(diǎn)與(b[0],b[1]) ?? ??? ?//是否能通過兩條線連通 ?? ??? ?r[1]-=1; ? ? ? ? while(r[3]<=r[1]) ?? ??? ?{ ?? ??? ? ? r[3]+=1; ?? ??? ? ? if(Two(a,r)==1) ?? ??? ? ? { ?? ??? ??? ? ? Zero(a,b); ?? ??? ??? ? ? return 1; ?? ??? ? ? } ?? ??? ?} ? ?? ?} ?? ?return 0; }
小編再為大家分享一段代碼:連連看消除算法的C++代碼實(shí)現(xiàn),過程比較復(fù)雜。
#include<iostream> #include<vector> using namespace std; bool CheckRemove(int x1,int y1,int x2,int y2,int rows,int cols,int ***pArray); void Swap(int &a,int &b); typedef struct { ?? ?int x; ?? ?int y; }point; int main() { ?? ?int Num=0; ?? ?while(Num<=15) ?? ?{ ?? ??? ?int m,n,t; ?? ??? ?cin>>m>>n>>t; ?? ??? ?int **store_num=new int*[m]; ?? ??? ?int **t_pos=new int*[t]; ?? ??? ?for(int i=0;i<m;i++) ?? ??? ?{ ?? ??? ??? ?store_num[i]=new int[n]; ?? ??? ?} ?? ??? ?for(int i=0;i<m;i++) ?? ??? ?{ ?? ??? ??? ?for(int j=0;j<n;j++) ?? ??? ??? ?{ ?? ??? ??? ??? ?store_num[i][j]=0; ?? ??? ??? ?} ?? ??? ?} ?? ??? ?for(int i=0;i<t;i++) ?? ??? ?{ ?? ??? ??? ?t_pos[i]=new int[4]; ?? ??? ?} ?? ??? ?for(int i=0;i<t;i++) ?? ??? ?{ ?? ??? ??? ?for(int j=0;j<4;j++) ?? ??? ??? ?{ ?? ??? ??? ??? ?t_pos[i][j]=0; ?? ??? ??? ?} ?? ??? ?} ?? ??? ?for(int i=0;i<m;i++) ?? ??? ?{ ?? ??? ??? ?for(int j=0;j<n;j++) ?? ??? ??? ?{ ?? ??? ??? ??? ?cin>>store_num[i][j]; ?? ??? ??? ?} ?? ??? ?} ?? ??? ?for(int i=0;i<t;i++) ?? ??? ?{ ?? ??? ??? ?for(int j=0;j<4;j++) ?? ??? ??? ?{ ?? ??? ??? ??? ?cin>>t_pos[i][j]; ?? ??? ??? ?} ?? ??? ?} ?? ??? ?int score=0; ?? ??? ?for(int i=0;i<t;i++) ?? ??? ?{ ?? ??? ??? ?if(CheckRemove(t_pos[i][0],t_pos[i][1],t_pos[i][2],t_pos[i][3],m,n,&store_num)) ?? ??? ??? ?{ ?? ??? ??? ??? ?score++; ?? ??? ??? ?} ?? ??? ?} ?? ??? ?cout<<score<<endl; ?? ??? ?Num++; ?? ?} ?? ?return 0; } bool CheckRemove(int x1,int y1,int x2,int y2,int rows,int cols,int ***pArray) { ? ?? ?int m1=x1-1;int n1=y1-1; ?? ?int m2=x2-1;int n2=y2-1; ?? ?if((*pArray)[m1][n1]!=(*pArray)[m2][n2]) ?? ?{ ?? ??? ?return false; ?? ?} ?? ?if(m1==m2&&(m1==0||m1==rows-1)) ?? ?{ ?? ??? ?(*pArray)[m1][n1]=0; ?? ??? ?(*pArray)[m2][n2]=0; ?? ??? ?return true; ?? ?} ?? ?if(n1==n2&&(n1==0||n1==cols-1)) ?? ?{ ?? ??? ?(*pArray)[m1][n1]=0; ?? ??? ?(*pArray)[m2][n2]=0; ?? ??? ?return true; ?? ?} ?? ?vector<point>v_point; ?? ?point p_in; ?? ?int **pArray_incre=new int*[rows+2]; ?? ?for(int i=0;i<rows+2;i++) ?? ?{ ?? ??? ?pArray_incre[i]=new int [cols+2]; ?? ?} ?? ?for(int i=0;i<rows+2;i++) ?? ?{ ?? ??? ?for(int j=0;j<cols+2;j++) ?? ??? ?{ ?? ??? ??? ?pArray_incre[i][j]=0; ?? ??? ?} ?? ?} ?? ?for(int i=1;i<rows+1;i++) ?? ?{ ?? ??? ?for(int j=1;j<cols+1;j++) ?? ??? ?{ ?? ??? ??? ?pArray_incre[i][j]=(*pArray)[i-1][j-1]; ?? ??? ?} ?? ?} ?? ?for(int y=y1+1;y<cols+2;y++) ?? ?{ ?? ??? ?if(pArray_incre[x1][y]!=0) ?? ??? ?{ ?? ??? ??? ?break; ?? ??? ?} ?? ??? ?p_in.x=x1; ?? ??? ?p_in.y=y; ?? ??? ?v_point.push_back(p_in); ?? ?} ?? ?for(int y=0;y<y1;y++) ?? ?{ ?? ??? ?if(pArray_incre[x1][y]!=0) ?? ??? ?{ ?? ??? ??? ?break; ?? ??? ?} ?? ??? ?p_in.x=x1; ?? ??? ?p_in.y=y; ?? ??? ?v_point.push_back(p_in); ?? ?} ?? ?for(int y=y2+1;y<cols+2;y++) ?? ?{ ?? ??? ?if(pArray_incre[x2][y]!=0) ?? ??? ?{ ?? ??? ??? ?break; ?? ??? ?} ?? ??? ?p_in.x=x2; ?? ??? ?p_in.y=y; ?? ??? ?v_point.push_back(p_in); ?? ?} ?? ?for(int y=0;y<y2;y++) ?? ?{ ?? ??? ?if(pArray_incre[x2][y]!=0) ?? ??? ?{ ?? ??? ??? ?break; ?? ??? ?} ?? ??? ?p_in.x=x2; ?? ??? ?p_in.y=y; ?? ??? ?v_point.push_back(p_in); ?? ?} ?? ?for(int i=0;i<(int)v_point.size();i++) ?? ?{ ?? ??? ?for(int j=0;j<(int)v_point.size();j++) ?? ??? ?{ ?? ??? ??? ?if(j!=i) ?? ??? ??? ?{ ?? ??? ??? ??? ?if(v_point[i].y==v_point[j].y) ?? ??? ??? ??? ?{ ?? ??? ??? ??? ??? ?int yy=v_point[i].y; ?? ??? ??? ??? ??? ?int xx1=v_point[i].x; ?? ??? ??? ??? ??? ?int xx2=v_point[j].x; ?? ??? ??? ??? ??? ?if(xx1>xx2) ?? ??? ??? ??? ??? ??? ?Swap(xx1,xx2); ?? ??? ??? ??? ??? ?int num=xx2-xx1+1; ?? ??? ??? ??? ??? ?int count=0; ?? ??? ??? ??? ??? ?for(int h=xx1;h<=xx2;h++) ?? ??? ??? ??? ??? ?{ ?? ??? ??? ??? ??? ??? ?if(pArray_incre[h][yy]==0) ?? ??? ??? ??? ??? ??? ?{ ?? ??? ??? ??? ??? ??? ??? ?count++; ?? ??? ??? ??? ??? ??? ?} ?? ??? ??? ??? ??? ?} ?? ??? ??? ??? ??? ?if(count==num) ?? ??? ??? ??? ??? ?{ ?? ??? ??? ??? ??? ??? ?(*pArray)[x1-1][y1-1]=0; ?? ??? ??? ??? ??? ??? ?(*pArray)[x2-1][y2-1]=0; ?? ??? ??? ??? ??? ??? ?v_point.empty(); ?? ??? ??? ??? ??? ??? ?return true; ?? ??? ??? ??? ??? ?} ?? ??? ??? ??? ?} ?? ??? ??? ?} ?? ??? ?} ?? ?} ?? ?v_point.empty(); ?? ?for(int x=x1+1;x<rows+2;x++) ?? ?{ ?? ??? ?if(pArray_incre[x][y1]!=0) ?? ??? ?{ ?? ??? ??? ?break; ?? ??? ?} ?? ??? ?p_in.x=x; ?? ??? ?p_in.y=y1; ?? ??? ?v_point.push_back(p_in); ?? ?} ?? ?for(int x=0;x<x1;x++) ?? ?{ ?? ??? ?if(pArray_incre[x][y1]!=0) ?? ??? ?{ ?? ??? ??? ?break; ?? ??? ?} ?? ??? ?p_in.x=x; ?? ??? ?p_in.y=y1; ?? ??? ?v_point.push_back(p_in); ?? ?} ?? ?for(int x=x2+1;x<rows+2;x++) ?? ?{ ?? ??? ?if(pArray_incre[x][y2]!=0) ?? ??? ?{ ?? ??? ??? ?break; ?? ??? ?} ?? ??? ?p_in.x=x; ?? ??? ?p_in.y=y2; ?? ??? ?v_point.push_back(p_in); ?? ?} ?? ?for(int x=0;x<x2;x++) ?? ?{ ?? ??? ?if(pArray_incre[x][y2]!=0) ?? ??? ?{ ?? ??? ??? ?break; ?? ??? ?} ?? ??? ?p_in.x=x; ?? ??? ?p_in.y=y2; ?? ??? ?v_point.push_back(p_in); ?? ?} ?? ?for(int i=0;i<(int)v_point.size();i++) ?? ?{ ?? ??? ?for(int j=0;j<(int)v_point.size();j++) ?? ??? ?{ ?? ??? ??? ?if(j!=i) ?? ??? ??? ?{ ?? ??? ??? ??? ?if(v_point[i].x==v_point[j].x) ?? ??? ??? ??? ?{ ?? ??? ??? ??? ??? ?int xx=v_point[i].x; ?? ??? ??? ??? ??? ?int yy1=v_point[i].y; ?? ??? ??? ??? ??? ?int yy2=v_point[j].y; ?? ??? ??? ??? ??? ?if(yy1>yy2) ?? ??? ??? ??? ??? ??? ?Swap(yy1,yy2); ?? ??? ??? ??? ??? ?int num=yy2-yy1+1; ?? ??? ??? ??? ??? ?int count=0; ?? ??? ??? ??? ??? ?for(int h=yy1;h<=yy2;h++) ?? ??? ??? ??? ??? ?{ ?? ??? ??? ??? ??? ??? ?if(pArray_incre[xx][h]==0) ?? ??? ??? ??? ??? ??? ?{ ?? ??? ??? ??? ??? ??? ??? ?count++; ?? ??? ??? ??? ??? ??? ?} ?? ??? ??? ??? ??? ?} ?? ??? ??? ??? ??? ?if(count==num) ?? ??? ??? ??? ??? ?{ ?? ??? ??? ??? ??? ??? ?(*pArray)[x1-1][y1-1]=0; ?? ??? ??? ??? ??? ??? ?(*pArray)[x2-1][y2-1]=0; ?? ??? ??? ??? ??? ??? ?v_point.empty(); ?? ??? ??? ??? ??? ??? ?return true; ?? ??? ??? ??? ??? ?} ?? ??? ??? ??? ?} ?? ??? ??? ?} ?? ??? ?} ?? ?} ? ? ?? ?return false; } void Swap(int &a,int &b) { ?? ?int tmp; ?? ?tmp=a; ?? ?a=b; ?? ?b=tmp; }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C/C++浮點(diǎn)數(shù)使用的兩個(gè)注意事項(xiàng)詳解
浮點(diǎn)數(shù)都是有符號(hào)的,沒有 unsigned 浮點(diǎn)數(shù),下面這篇文章主要給大家介紹了關(guān)于C/C++浮點(diǎn)數(shù)使用的兩個(gè)注意事項(xiàng),文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下2023-02-02C++11實(shí)現(xiàn)簡(jiǎn)易定時(shí)器的示例代碼
這篇文章主要介紹了C++11實(shí)現(xiàn)簡(jiǎn)易定時(shí)器的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04關(guān)于VS2019 C++項(xiàng)目同時(shí)出現(xiàn)LNK2005 和LNK1169 error 的解決辦法
這篇文章主要介紹了關(guān)于VS2019 C++項(xiàng)目同時(shí)出現(xiàn)LNK2005 和LNK1169 error 的解決辦法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-04-04C語言數(shù)據(jù)結(jié)構(gòu)時(shí)間復(fù)雜度及空間復(fù)雜度簡(jiǎn)要分析
我們?cè)谶M(jìn)行編程時(shí),往往會(huì)開發(fā)諸多的算法,那么我們?cè)趺丛谀敲炊嗨惴ㄖ姓业阶詈玫哪莻€(gè)呢?本文主要介紹時(shí)間和空間復(fù)雜度概念及時(shí)間復(fù)雜度的求解,預(yù)祝讀者學(xué)習(xí)愉快2021-10-10C++實(shí)現(xiàn)LeetCode(135.分糖果問題)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(135.分糖果問題),本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07