其实这个题,和我上次讲的那个连连看一样!只不过是字符变成了整型而已!
还是贴一下关键代码吧(⊙o⊙)~~
1 bool search(int x1,int y1,int x2,int y2,int n,int m)
2 {
3 memset(gk,0,sizeof(gk));
4 int t=game[x2][y2];
5 game[x2][y2]=0;
6 gk[x1][y1]=1;
7
8 //对game[x1][y1]四个方向是空格的标为1
9 for (int i=x1-1;i>=1;i--)
10 {
11 if(game[i][y1]==0)gk[i][y1]=1;
12 else break;
13 }
14 for (int j=x1+1;j<=n;j++){
15 if(game[j][y1]==0)gk[j][y1]=1;
16 else break;
17 }
18
19 for (int i=y1-1;i>=1;i--){
20 if(game[x1][i]==0)gk[x1][i]=1;
21 else break;
22 }
23 for (int i=y1+1;i<=m;i++){
24 if(game[x1][i]==0)gk[x1][i]=1;
25 else break;
26 }
27
28 if (gk[x2][y2]>0&&gk[x2][y2]<4)
29 {
30 game[x2][y2]=t;
31 return true;
32 }
33 //对gk[i][j]为1的四个方向是空格的标为2
34 for (int i=1;i<=n;i++)
35 for (int j=1;j<=m;j++)
36 if (gk[i][j]==1)
37 {
38 for (int k=i-1;k>=1;k--)
39 {
40 if (game[k][j]==0){
41 if(gk[k][j]==0)gk[k][j]=2;
42 }
43 else break;
44 }
45 for (int k=i+1;k<=n;k++){
46 if (game[k][j]==0){
47 if(gk[k][j]==0)gk[k][j]=2;
48 }
49 else break;
50 }
51
52 for (int k=j-1;k>=1;k--){
53 if (game[i][k]==0){
54 if(gk[i][k]==0)gk[i][k]=2;
55 }
56 else break;
57 }
58 for (int k=j+1;k<=m;k++){
59 if (game[i][k]==0){
60 if(gk[i][k]==0)gk[i][k]=2;
61 }
62 else break;
63 }
64 }
65
66 if (gk[x2][y2]>0&&gk[x2][y2]<4)
67 {
68 game[x2][y2]=t;
69 return true;
70 }
71 //对gk[i][j]为2的四个方向是空格的标为3
72 for (int i=1;i<=n;i++)
73 for (int j=1;j<=m;j++)
74 if (gk[i][j]==2){
75 for (int k=i-1;k>=1;k--)
76 {
77 if (game[k][j]==0)
78 {
79 if(gk[k][j]==0)gk[k][j]=3;
80 }
81 else break;
82 }
83 for (int k=i+1;k<=n;k++)
84 {
85 if (game[k][j]==0)
86 {
87 if(gk[k][j]==0)gk[k][j]=3;
88 }
89 else break;
90 }
91
92 for (int k=j-1;k>=1;k--)
93 {
94 if (game[i][k]==0){
95 if(gk[i][k]==0)gk[i][k]=3;
96 }
97 else break;
98 }
99 for (int k=j+1;k<=m;k++)
100 {
101 if (game[i][k]==0)
102 {
103 if(gk[i][k]==0)gk[i][k]=3;
104 }
105 else break;
106 }
107 }
108
109 game[x2][y2]=t;
110 if(gk[x2][y2]>0&&gk[x2][y2]<4)return true;
111 if(gk[x2][y2]==0) return false;
112 }
posted on 2010-11-24 16:07
路修远 阅读(1806)
评论(0) 编辑 收藏 引用 所属分类:
路修远