Introduction

This is my blog of programming, I take notes and leave codes of computer science problems I solved here. Be my guest to comment :)

Friday, November 2, 2012

uva 260 - Il Gioco dell'X


Problem link
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
using namespace std;
const int maxn = 210;
const int dx[6] = {0,-1,-1,0,1,1};
const int dy[6] = {-1,-1,0,1,1,0};
//---------------------------------
char a[maxn][maxn];
bool fre[maxn][maxn];
int qx[maxn*maxn*maxn];
int qy[maxn*maxn*maxn];
int n;
//---------------------------------
bool readfile() {
	cin >> n;
	if (n==0) return(false);
	for (int i=1; i<=n; i++)
		for (int j=1; j<=n; j++) cin >> a[i][j];
	return(true);
}
//---------------------------------
bool IsIn(int x, int y) {
	if (x>=1 && y>=1 && x<=n && y<=n) return(true);
	return(false);
}
//---------------------------------
bool _bfs(char c, int u, int v) {
	int head=1, tail=1, x,y,xx,yy;
	qx[head]=u;
	qy[head]=v;
	fre[u][v]=false;
	do
	{
		x=qx[head]; y=qy[head]; head++;
		for (int k=0; k<6; k++)
		{
			xx=x+dx[k];
			yy=y+dy[k];
			if (IsIn(xx,yy) && fre[xx][yy] && a[xx][yy]==c)
			{
				fre[xx][yy]=false;
				if (c=='w' && yy==n) return(true);
				if (c=='b' && xx==n) return(true);
				tail++;
				qx[tail]=xx;
				qy[tail]=yy;
			}
		}
	} while (head<=tail);
	return(false);
}
//---------------------------------
int main() {
	int test=0;
	while (readfile())
	{
		test++;
		bool win=false;
		for (int i=0; i<=n; i++)
			for (int j=0; j<=n; j++) fre[i][j]=true;
		for (int i=1; i<=n; i++)
		{
			if (fre[i][1] && a[i][1]=='w') win=_bfs('w',i,1);
			if (win) break;
		}
		if (win) cout << test << " W" << endl;
		else cout << test << " B" << endl;
	}
	return 0;
}
//---------------------------------

No comments:

Post a Comment