首页 > 搜索 > BFS搜索 > hdu 2699 Five in a Row-BFS-[解题报告]C++
2014
02-13

hdu 2699 Five in a Row-BFS-[解题报告]C++

Five in a Row

问题描述 :

The ACM Team of WHU is planning an online AI arena for Go-bang (Five in a Row). Now, we need some help.

We all know the famous game "Five in a Row". It is traditionally played with go pieces (black and white stones) on a go
board (15×15 intersections). The winner is the first player to get an unbroken row of five stones horizontally, vertically, or
diagonally.

We now have an uncompleted game, that is to say, yet no one won. You are required to tell us that whether the next player
could win within one step or not.

输入:

The input consists of multiple test cases. The first line of input contains an integer T, which is the number of test cases.

Each test case is a 15 * 15 go board, ‘B’ represent for a black stone while ‘W’ for white ones. ‘.’ represents cross with no
stone represented by 0.

[Technical Specification]
T is an integer, and T <= 20.
The number of white stones is either equal or 1 less than the number of black stones.
There is at least ONE ‘.’ in each test case.
Test cases are separated by ONE empty line.
You can assume that black player goes first.

输出:

The input consists of multiple test cases. The first line of input contains an integer T, which is the number of test cases.

Each test case is a 15 * 15 go board, ‘B’ represent for a black stone while ‘W’ for white ones. ‘.’ represents cross with no
stone represented by 0.

[Technical Specification]
T is an integer, and T <= 20.
The number of white stones is either equal or 1 less than the number of black stones.
There is at least ONE ‘.’ in each test case.
Test cases are separated by ONE empty line.
You can assume that black player goes first.

样例输入:

2 
........W...... 
..W......W...B. 
.W....B........ 
.W.......W..... 
..BB....W...... 
...B........... 
....B........WW 
...........B... 
....W.......... 
.W..B.B....WBB. 
BW........B.W.B 
............... 
W.....BB.W....W 
B..B.W.BB....B. 
........WW..... 

...B...W..BBW.. 
W..WBW..W...B.. 
...W..WBW...... 
.....BWW..B.B.. 
..BW.B.B..W.... 
B..W..WBW...... 
B......B......W 
B.B...B....W... 
....B.WB...B... 
B.W............ 
.....WW...B.... 
..B.....B...W.W 
.W.........WBW. 
B.............. 
..W.B...W..W..B 

样例输出:

NO
YES

简单题。

/*

*/
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include<iostream>
#include<queue>
#include<map>
#include<stack>
#include<set>
#include<math.h>
using namespace std;
typedef long long int64;
//typedef __int64 int64;
typedef pair<int64,int64> PII;
#define MP(a,b) make_pair((a),(b)) 
const int maxn = 18;
const int inf = 0x7fffffff;
const double pi=acos(-1.0);
const double eps = 1e-8;
char mat[ maxn ][ maxn ];
bool flag;
const int dx[]={1,-1,0,0,-1,-1,1,1};
const int dy[]={0,0,1,-1,-1,1,-1,1};

bool in( int x,int y ){
	if( x>=0&&x<15&&y>=0&&y<15 )
		return true;
	else
		return false;
}

bool Judge( int x,int y,char xx ){
	for( int i=0;i<8;i++ ){
		int tx = x+dx[i];
		int ty = y+dy[i];
		if( in(tx,ty)==true&&mat[tx][ty]==xx )
			return true;
	}
	return false;
}

int bfs( int x,int y,char xx ){
	
	int tx = x+1;
	int ty = y;
	int cnt = 0;
	while( tx<15 ){
		if( mat[tx][ty]==xx ){
			cnt++;
			tx++;
			if( cnt>=4 ) break;
		}
		else 
			break;
	}
	tx = x-1;
	while( tx>=0 ){
		if( mat[tx][ty]==xx ){
			cnt++;
			tx--;
			if( cnt>=4 ) break;
		}
		else 
			break;
	}
	if( cnt>=4 ) return 5;
	//row to "5"
	tx = x;
	ty = y+1;
	cnt = 0;
	while( ty<15 ){
		if( mat[tx][ty]==xx ){
			cnt++;
			ty++;
			if( cnt>=4 ) break;
		}
		else 
			break;
	}
	ty = y-1;
	while( ty>=0 ){
		if( mat[tx][ty]==xx ){
			cnt++;
			ty--;
			if( cnt>=4 ) break;
		}
		else 
			break;
	}
	if( cnt>=4 ) return 5;
	// col to "5"
	tx = x+1;
	ty = y+1;
	cnt = 0;
	while( tx<15&&ty<15 ){
		if( mat[tx][ty]==xx ){
			cnt++;
			tx++;
			ty++;
			if( cnt>=4 ) break;
		}
		else 
			break;
	}
	tx = x-1;
	ty = y-1;
	while( tx>=0&&ty>=0 ){
		if( mat[tx][ty]==xx ){
			cnt++;
			tx--;
			ty--;
			if( cnt>=4 ) break;
		}
		else 
			break;
	}
	if( cnt>=4 ) return 5;
	//right up to "5"
	tx = x-1;
	ty = y+1;
	cnt = 0;
	while( tx>=0&&ty<15 ){
		if( mat[tx][ty]==xx ){
			cnt++;
			tx--;
			ty++;
			if( cnt>=4 ) break;
		}
		else 
			break;
	}
	tx = x+1;
	ty = y-1;
	while( tx<15&&ty>=0 ){
		if( mat[tx][ty]==xx ){
			cnt++;
			tx++;
			ty--;
			if( cnt>=4 ) break;
		}
		else 
			break;
	}
	if( cnt>=4 ) return 5;
	//left up to "5"
	return -1;
}

int main(){
	int T;
	scanf("%d",&T);
	while( T-- ){
		int cnt1 = 0;
		int cnt2 = 0;
		for( int i=0;i<15;i++ ){
			scanf("%s",mat[i]);
			for( int j=0;j<15;j++ ){
				if( mat[i][j]=='W' ) cnt1++;
				if( mat[i][j]=='B' ) cnt2++;
			}
		}
		char xx ;
		if( cnt1==cnt2 ) xx = 'B';
		else xx = 'W';
		flag = false;
		for( int i=0;i<15;i++ ){
			for( int j=0;j<15;j++ ){
				if( mat[i][j]=='.'&&Judge( i,j,xx )==true ){//i,j周围有 xx
					if( bfs( i,j,xx )>=5 ){
						flag = true;
						break;
					}
				}
			}
			if( flag==true ) break;
		}
		if( flag ) puts("YES");
		else puts("NO");
	}
	return 0;
}

解题转自:http://blog.csdn.net/xxx0624_/article/details/9956157


,
  1. [email protected]

  2. 网站做得很好看,内容也多,全。前段时间在博客园里看到有人说:网页的好坏看字体。觉得微软雅黑的字体很好看,然后现在这个网站也用的这个字体!nice!

  3. Excellent Web-site! I required to ask if I might webpages and use a component of the net web website and use a number of factors for just about any faculty process. Please notify me through email regardless of whether that would be excellent. Many thanks