码迷,mamicode.com
首页 > 其他好文 > 详细

17.11.3 五子棋判断输赢

时间:2017-11-03 19:12:57      阅读:221      评论:0      收藏:0      [点我收藏+]

标签:red   约束   ace   log   under   pad   height   value   none   

描述
在一个N×N的棋盘上下五子棋,给定一个五子棋黑白棋的落子序列 (x0,y0),(x1,y1), ..., (xn,yn),判断走到多少步时,哪方获胜。

关于输入
第一行有两个整数,棋盘的大小N和落子序列的长度n。  其余各行每行两个数字,分别表示黑棋或白棋棋子在棋盘上的位置。  定输入一定合理的,而且一定能分出输赢,但因为对弈双方没有判断出输赢而多行了数步。

关于输出
输出为一行,包含两个整数,第一个整数是判断出输赢时行棋的步数,第二个数字表示获胜方。如果黑方获胜,输出0;如果白方获胜,输出1。

例子输入
10 12
 0 0
 1 0
 0 1
 2 0
 0 2
 3 0
 0 3
 4 0
 0 4
 5 0
 0 5
 6 0
 
 
例子输出
9 0
提示
五子棋规则比较复杂,这里我们只要求黑棋先行,谁先连出至少5连子就算赢,不管是否出现规则中约束的禁手。每局棋都有胜负结果。

 

技术分享
  1 #include<iostream>
  2 using namespace std;
  3 int main()
  4 {
  5     int N, n,flat=0;
  6     cin >> N >> n;
  7     int chess[50][50];
  8     int col1, line1,col2,line2;
  9     for (int i = 0; i < 50; i++)
 10         for (int j = 0; j < 50; j++)
 11             chess[i][j] = 0;
 12     for (int i = 1; i <= n; i++)
 13     {
 14         if (i % 2 != 0)
 15         {
 16             cin >> line1 >> col1;
 17             chess[line1][col1] = -1;
 18             for (int j = 0; j <= 4; j++)
 19             {
 20                 if (line1 + j > N || col1 + j > N)
 21                     continue;
 22                 if (line1 - 4 + j < 0 || col1 - 4 + j < 0)
 23                     continue;
 24                 flat = 0;
 25                 for (int p = 0; p <= j; p++)
 26                 {
 27                     if (chess[line1 + p][col1 + p] == -1)
 28                         flat++;
 29                 }
 30                 for (int p = 0; p <= 4 - j; p++)
 31                 {
 32                     if (chess[line1 - p][col1 - p] == -1)
 33                         flat++;
 34                 }
 35                 if (flat == 6)
 36                 {
 37                     cout << i << " " << "0";
 38                     return 0;
 39                 }
 40             }
 41             flat = 0;
 42             for (int j = 0; j <= 4; j++)
 43             {
 44                 if (line1 + j > N || col1 - j < 0)
 45                     continue;
 46                 if (line1 - 4 + j < 0 || col1 + 4 - j >N)
 47                     continue;
 48                 flat = 0;
 49                 for (int p = 0; p <= j; p++)
 50                 {
 51                     if (chess[line1 + p][col1 - p] == -1)
 52                         flat++;
 53                 }
 54                 for (int p = 0; p <= 4 - j; p++)
 55                 {
 56                     if (chess[line1 - p][col1 + p] == -1)
 57                         flat++;
 58                 }
 59                 if (flat == 6)
 60                 {
 61                     cout << i << " " << "0";
 62                     return 0;
 63                 }
 64             }
 65             flat = 0;
 66             for (int j = 0; j <= 4; j++)
 67             {
 68                 if (line1 + j > N)
 69                     continue;
 70                 if (line1 - 4 + j < 0)
 71                     continue;
 72                 flat = 0;
 73                 for (int p = 0; p <= j; p++)
 74                 {
 75                     if (chess[line1 + p][col1] == -1)
 76                         flat++;
 77                 }
 78                 for (int p = 0; p <= 4 - j; p++)
 79                 {
 80                     if (chess[line1 - p][col1] == -1)
 81                         flat++;
 82                 }
 83                 if (flat == 6)
 84                 {
 85                     cout << i << " " << "0";
 86                     return 0;
 87                 }
 88             }
 89             flat = 0;
 90             for (int j = 0; j <= 4; j++)
 91             {
 92                 if (col1 + j > N)
 93                     continue;
 94                 if (col1 - 4 + j < 0)
 95                     continue;
 96                 flat = 0;
 97                 for (int p = 0; p <= j; p++)
 98                 {
 99                     if (chess[line1][col1 + p] == -1)
100                         flat++;
101                 }
102                 for (int p = 0; p <= 4 - j; p++)
103                 {
104                     if (chess[line1][col1 - p] == -1)
105                         flat++;
106                 }
107                 if (flat == 6)
108                 {
109                     cout << i << " " << "0";
110                     return 0;
111                 }
112             }
113         }
114         else
115         {
116                 cin >> line2 >> col2;
117                 chess[line2][col2] =1;
118                 flat = 0;
119                 for (int j = 0; j <= 4; j++)
120                 {
121                     if (line2 + j > N || col2 + j > N)
122                         continue;
123                     if (line2 - 4 + j < 0 || col2 - 4 + j < 0)
124                         continue;
125                     flat = 0;
126                     for (int p = 0; p <= j; p++)
127                     {
128                         if (chess[line2 + p][col2 + p] == 1)
129                             flat++;
130                     }
131                     for (int p = 0; p <= 4 - j; p++)
132                     {
133                         if (chess[line2 - p][col2 - p] == 1)
134                             flat++;
135                     }
136                     if (flat ==6)
137                     {
138                         cout << i << " " << "0";
139                         return 0;
140                     }
141                 }
142                 flat = 0;
143                 for (int j = 0; j <= 4; j++)
144                 {
145                     if (line2 + j > N || col2 - j < 0)
146                         continue;
147                     if (line2 - 4 + j < 0 || col2 + 4 - j >N)
148                         continue;
149                     flat = 0;
150                     for (int p = 0; p <= j; p++)
151                     {
152                         if (chess[line2 + p][col2 - p] == 1)
153                             flat++;
154                     }
155                     for (int p = 0; p <= 4 - j; p++)
156                     {
157                         if (chess[line2 - p][col2 + p] == 1)
158                             flat++;
159                     }
160                     if (flat == 6)
161                     {
162                         cout << i << " " << "0";
163                         return 0;
164                     }
165                 }
166                 flat = 0;
167                 for (int j = 0; j <= 4; j++)
168                 {
169                     if (line2 + j > N)
170                         continue;
171                     if (line2 - 4 + j < 0)
172                         continue;
173                     flat = 0;
174                     for (int p = 0; p <= j; p++)
175                     {
176                         if (chess[line2 + p][col2] == 1)
177                             flat++;
178                     }
179                     for (int p = 0; p <= 4 - j; p++)
180                     {
181                         if (chess[line2 - p][col2] == 1)
182                             flat++;
183                     }
184                     if (flat == 6)
185                     {
186                         cout << i << " " << "0";
187                         return 0;
188                     }
189                 }
190                 flat = 0;
191                 for (int j = 0; j <= 4; j++)
192                 {
193                     if (col2 + j > N)
194                         continue;
195                     if (col2 - 4 + j < 0)
196                         continue;
197                     flat = 0;
198                     for (int p = 0; p <= j; p++)
199                     {
200                         if (chess[line2][col2 + p] == 1)
201                             flat++;
202                     }
203                     for (int p = 0; p <= 4 - j; p++)
204                     {
205                         if (chess[line2][col2 - p] == 1)
206                             flat++;
207                     }
208                     if (flat ==6)
209                     {
210                         cout << i << " " << "0";
211                         return 0;
212                     }
213                 }
214         }
215     }
216     return 0;
217 }
View Code

在我看来这道题非常考验耐心和细心……以及如果是考试的话一定要冷静……

不知道有没有更便捷些的解法

(神烦

17.11.3 五子棋判断输赢

标签:red   约束   ace   log   under   pad   height   value   none   

原文地址:http://www.cnblogs.com/yalphait/p/7779222.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!