这题好费劲,用了好几个小时才搞定,用的方法又笨的要死,如果你有更好的方法,一定要告诉我!
题目描述:
Hj经常和同学下五子棋,在同学面前那可是盘盘皆赢,所向披靡,战无不胜....(自夸地),有一天同学就说:你下棋这么厉害,那你编程厉害?我现在给你一道题目,你能编写出正确的程序?Hj坦然接受了。
题目是这样的:给你一局未下完的五子棋棋局,现在轮到白棋下,试问白棋能不能至多下两颗棋子就赢了?
五子棋规则:
(1)对局双方各执一色棋子。
(2)空棋盘开局。
(3)黑先、白后,交替下子,每次只能下一子。
(4)棋子下在棋盘的空白点上,棋子下定后,不得向其它点移动,不得从棋盘上拿掉或拿起另落别处。
(5)先手的第一枚棋子可下在棋盘任意方格上。
(6)轮流下子是双方的权利,但允许任何一方放弃下子权(即:PASS权)。
(7)先达到五子相连(横竖斜)的状态的一方是赢家,如果最后棋牌放满还没分胜负则是平局。
输入描述:
测试数据包含多组,每组首先是两个正整数n,m(0< n,m<=10),分别表示棋盘的行数和列数。接下来是n行m列的字符,其中‘1’表示白棋子,‘0’表示黑棋子,‘ ’表示没有棋子。保证输入棋局合法。
输出描述:
如果白棋方能至多下两颗棋子就赢了就输出“YES”,否则输出“NO”。
解题思路:
注意事项:
1.输入样例格式有问题,正确的格式是这样
6 6
_1100__
_____11
_______
000___0
0000000
0000000
下划线代表空格!
具体代码如下
#include
#include
#include
#include
#include
using namespace std; char ma[12][12]; char matmp[12][12]; int n,m; //判断从x,y开始有没有匹配字串c int Fit(int x, int y, string c) { bool flag=true; int len = c.length(); //右侧 if(y+len-1 <= m){ for(int i=0;i
0 && y+len-1<=m){ for(int i=0;i
0 && y+len-1<=m){ for(int i=0;i
white side='0'=>black { string cmpBlackStr[] = {" 0000", "0 000", "00 00", "000 0", "0000 "}; string cmpWhiteStr[] = {" 1111", "1 111", "11 11", "111 1", "1111 "}; for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) { if(side == '1'){ for(int k=0;k<5;k++){ if(Fit(i,j,cmpWhiteStr[k]))return true; } }else{ for(int k=0;k<5;k++){ if(Fit(i,j,cmpBlackStr[k]))return true; } } } } return false; } //若成功封住,x,y为封住的地点 bool Feng(char side) { string notPossibleBlack = " 0000 "; string notPossibleWhite = " 1111 "; string PossibleBlack[] = {"0 000", "00 00", "000 0", " 0000", "0000 "}; string SetBlack[] = {"01000", "00100", "00010", "10000", "00001"}; string PossibleWhite[] = {"1 111", "11 11", "111 1", " 1111", "1111 "}; string SetWhite[] = {"10111", "11011", "11101", "01111", "11110"}; int pNum=0; for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) { //如果封不住 返回false if(side == '1'){ if(Fit(i, j, notPossibleWhite)){ return false; } }else{ if(Fit(i, j, notPossibleBlack))return false; } } } for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) { //Possible if(side == '1'){ for(int k=0;k<5;k++) { if(SetFit(i,j,PossibleWhite[k], SetWhite[k])){ pNum++; if(pNum > 1)return false; } } }else { for(int k=0;k<5;k++) { if(SetFit(i,j,PossibleBlack[k], SetBlack[k])){ pNum++; if(pNum > 1)return false; } } } } } return true; } void printM() { for(int i=0;i<=n+1;i++) { for(int j=0;j<=m+1;j++) if(ma[i][j] == ' ')cout<<'_'; else cout<
>n>>m) { int i=1,j=1; getchar(); for(int x=0;x<12;x++) for(int y=0;y<12;y++) ma[x][y] = '*'; while(true) { ma[i][j] = getchar(); j++; if(j>m){i++;j=1;getchar();} if(i>n)break; } if(OneStepWin('1')){ cout<<"YES"<