经典指数          
原因
2452
浏览数
0
收藏数
 

NowCoder最近爱上了五子棋,现在给你一个棋局,请你帮忙判断其中有没有五子连珠(超过五颗也算)。 输入描述: 输入有多组数据,每组数据为一张20x20的棋盘。其中黑子用“*”表示,白子用“+”表示,空白位置用“.”表示。 输出描述: 如果棋盘上存在五子连珠(无论哪种颜色的棋子),输入“Yes”,否则输出“No”。 输入例子: ......................................................................................*....................*....................*...............++++.*....................*........................................................................................................................................................................................................................................................................................................*..................+*+++................*...................*................................................................................................................................................................................................................................................................................ 输出例子: YesNo

     举报   纠错  
 
切换
1 个答案
#include #include #define N 20 using namespace std; string table[N]; //本题由于棋子是用字符表示的,所以非常适合用string数组表示 //对角线十字交叉判断是否有可能存在结果 bool crossJudge(string *table,int x,int y){ string s,str1(5,'.'); s.push_back(table[x-2][y-2]); s.push_back(table[x-1][y-1]); s.push_back(table[x][y]); s.push_back(table[x+1][y+1]); s.push_back(table[x+2][y+2]); if(s==str1){ s.clear(); s.push_back(table[x][y]); s.push_back(table[x-2][y+2]); s.push_back(table[x-1][y+1]); s.push_back(table[x+1][y-1]); s.push_back(table[x+2][y-2]); if(s==str1){ return false; //不可能存在结果,pass }else{ return true; } }else{ return true; //可能存在结果 } } bool judgeResult(string *table,int x,int y){ //在一个5*5的领域内判断 int row1=x-2,row2=x+2; string str1(5,'*'); string str2(5,'+'); for(int i=row1;i<=row2;i++){ //注意边界,取等号很关键 string s=table[i].substr(y-2,5); if(s==str1||s==str2){ return true; } } int col1=y-2,col2=y+2; string s; for(int i=col1;i<=col2;i++){ s.clear(); //每轮循环记得清空 s.push_back(table[y-2][i]); s.push_back(table[y-1][i]); s.push_back(table[y][i]); s.push_back(table[y+1][i]); s.push_back(table[y+2][i]); if(s==str1||s==str2){ return true; } } //交叉判断 s.clear(); s.push_back(table[x-2][y-2]); s.push_back(table[x-1][y-1]); s.push_back(table[x][y]); s.push_back(table[x+1][y+1]); s.push_back(table[x+2][y+2]); if(s==str1||s==str2)return true; s.clear(); s.push_back(table[x-2][y+2]); s.push_back(table[x-1][y+1]); s.push_back(table[x][y]); s.push_back(table[x+1][y-1]); s.push_back(table[x+2][y-2]); if(s==str1||s==str2)return true; return false; } int main(){ //(0ms 8552k) while(cin>>table[0]){ for(int i=1;i>table[i]; } //开始判断 int edge=N-2; bool res=false; for(int i=2;i
 
切换
撰写答案