小紫书 4-3 UVA 220 Othello

代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
using namespace std;
char player;
char board[12][12];
int drct[2][8] = { 1, 0, 1, 1, -1, 0, -1, -1
, 0, 1, -1, 1, 0, -1, 1, -1 };
char readchar()
{
	char c = '\0';
	while (c = getchar())
	{
		if (c != ' '&&c != '\t'&&c != '\r'&&c != '\n')
			break;
	}
	return c;
}
bool inside(int x, int y)
{
	if (x >= 1 && x <= 8 && y >= 1 && y <= 8)
		return true;
	else
		return false;
}
bool can_move(char p, int x, int y)
{
	char bktd = (p == 'W') ? 'B' : 'W';
	if (board[x][y] != '-')
		return false;
	for (int i = 0; i < 8; ++i)
	{
		int x1 = x + drct[0][i], y1 = y + drct[1][i];
		if (inside(x1, y1) && board[x1][y1] == bktd)
		{
			for (int j = 1;; ++j)
			{
				int x2 = x1 + j*drct[0][i], y2 = y1 + j*drct[1][i];
				if (inside(x2, y2))
				{
					if (board[x2][y2] == bktd) continue;
					else if (board[x2][y2] == p)
						return true;
					else
						break;
				}
				else
					break;
			}
		}
	}
	return false;
}
void list(char p)
{
	int f = 0;
	for (int i = 1; i <= 8; ++i)
		for (int j = 1; j <= 8; ++j)
			if (can_move(p, i, j))
			{
				if (f++) printf(" (%d,%d)", i, j);
				else printf("(%d,%d)", i, j);
			}
	if (f == 0)
		printf("No legal move.");
	printf("\n");
}
void p_move(char p, int x, int y)
{
	char bktd = (p == 'W') ? 'B' : 'W';
	for (int i = 0; i < 8; ++i)
	{
		int x1 = x + drct[0][i], y1 = y + drct[1][i];
		if (inside(x1, y1) && board[x1][y1] == bktd)
		{
			for (int j = 1;; ++j)
			{
				int x2 = x1 + j*drct[0][i], y2 = y1 + j*drct[1][i];
				if (inside(x2, y2))
				{
					if (board[x2][y2] == bktd) continue;
					else if (board[x2][y2] == p)
					{
						for (int k = 0; k < j; ++k)
							board[x1 + k*drct[0][i]][y1 + k*drct[1][i]] = p;
						break;
					}
					else
						break;
				}
				else
					break;
			}
		}
	}
	board[x][y] = p;
}
void print()
{
	for (int i = 1; i <= 8; ++i)
	{
		for (int j = 1; j <= 8; ++j)
			printf("%c", board[i][j]);
		printf("\n");
	}
}
void printnum()
{
	int b = 0, w = 0;
	for (int i = 1; i <= 8; ++i)
		for (int j = 1; j <= 8; ++j)
		{
			if (board[i][j] == 'B')
				++b;
			else if (board[i][j] == 'W')
				++w;
		}
	printf("Black - %2d White - %2d\n", b, w);
}
int main()
{
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	int n;
	char cmd;
	scanf("%d", &n);
	for (int s = 1; s <= n; ++s)
	{
		memset(board, 0, sizeof(board));
		for (int i = 1; i <= 8; ++i)
			scanf("%s", board[i] + 1);
		player = readchar();
		while (cmd = readchar())
		{
			if (cmd == 'Q')
			{
				print();
				break;
			}
			else if (cmd == 'L')
				list(player);
			else if (cmd == 'M')
			{
				int i, j;
				i = readchar() - '0';
				j = readchar() - '0';
				if (can_move(player, i, j))
					p_move(player, i, j);
				else
				{
					player = (player == 'W') ? 'B' : 'W';
					p_move(player, i, j);
				}
				player = (player == 'W') ? 'B' : 'W';
				printnum();
			}
		}
		if (s != n)
			printf("\n");
	}
	//while (1);
	//system("pause");
	return 0;
}
/*
2
--------
--------
--------
---WB---
---BW---
--------
--------
--------
W
L
M35
L
Q
WWWWB---
WWWB----
WWB-----
WB------
--------
--------
--------
--------
B
L
M25
L
Q
*/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值