首页 > 试题广场 >

黑白棋

[编程题]黑白棋
  • 热度指数:346 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
黑白棋,又叫翻转棋(Reversi)、苹果棋或奥赛罗棋(Othello)。棋盘共有8行8列共64格。
开局时,棋盘正中央的4格先置放黑白相隔的4枚棋子。双方轮流落子,只要落子和棋盘上任一枚己方的棋子在一条线上(横、直、斜线皆可)夹着对方棋子,就能将对方的这些棋子转变为我己方(翻面即可)。如果在任一位置落子都不能夹住对手的任一颗棋子,就要让对手下子。当双方皆不能下子时,游戏就结束,子多的一方胜。
现在给你一个8x8的棋局,以及下一步玩家的落子位置。请输出翻转好的新棋局。

输入描述:
输入有多组数据,每组数据有两部分。

第一部分有8行,为8x8的棋局,其中“*”为黑子、“+”为白子、“.”为空位置。

第二个部分有一行,包含要落子的行号r(1≤r≤8)、列号c(1≤c≤8)、棋子e(“*”或“+”)。


输出描述:
对应每组输入,根据黑白棋的规则,请输出落子后翻转的结果。

每组数据之后输出一个空行作为间隔。
示例1

输入

........
........
........
...+*...
...*+...
........
........
........
4 3 *
........
........
........
..***...
...*+...
........
........
........
3 3 +

输出

........
........
........
..***...
...*+...
........
........
........

........
........
..+.....
..*+*...
...*+...
........
........
........
题目都没说清楚怎样算【夹住】对方的棋子?!
发表于 2017-06-08 08:18:22 回复(0)
#include <cstdio>
#include <limits.h>
#include <vector>
using namespace std;
void updateBoard(vector<vector<char>>& board, int x, int y, char type,char enemy)
{
 int i, j;
 for (i = x - 1; i >= 0 && board[i][y] == enemy; --i)
  ;
 if (i >= 0 && board[i][y] == type)
  while (i < x) board[i++][y] = type;
 for (i = x + 1; i < 8 && board[i][y] == enemy; ++i)
  ;
 if (i < 8 && board[i][y] == type)
  while (i > x) board[i--][y] = type;
 for (j = y - 1; j >= 0 && board[x][j] == enemy; --j)
  ;
 if (j >= 0 && board[x][j] == type)
  while (j < y) board[x][j++] = type;
 for (j = y + 1; j < 8 && board[x][j] == enemy; ++j)
  ;
 if (j < 8 && board[x][j] == type)
  while (j > y) board[x][j--] = type;
 for (i = x - 1, j = y - 1; i >= 0 && j >= 0 && board[i][j] == enemy; --i, --j)
  ;
 if (i >= 0 && j >= 0 && board[i][j] == type)
  while (i < x && j < y) board[i++][j++] = type;
 for (i = x + 1, j = y - 1; i < 8 && j >= 0 && board[i][j] == enemy; ++i, --j)
  ;
 if (i < 8 && j >= 0 && board[i][j] == type)
  while (i > x && j < y) board[i--][j++] = type;
 for (i = x - 1, j = y + 1; i >= 0 && j < 8 && board[i][j] == enemy; --i, ++j)
  ;
 if (i >= 0 && j < 8 && board[i][j] == type)
  while (i < x && j > y) board[i++][j--] = type;
 for (i = x + 1, j = y + 1; i < 8 && j < 8 && board[i][j] == enemy; ++i, ++j)
  ;
 if (i < 8 && j < 8 && board[i][j] == type)
  while (i > x && j > y) board[i--][j--] = type;
}
int main()
{
 //freopen("in.txt", "r", stdin);
 char c;
 vector<vector<char>> board(8, vector<char>(8));
 int x, y;
 while ((c = getchar()) != EOF)
 {
  ungetc(c,stdin);
  for (int i = 0; i < 8; ++i)
  {
   for (int j = 0; j < 8; ++j)
    board[i][j] = getchar();
   getchar();
  }
  scanf("%d %d %c\n", &x, &y, &c);
  --x; --y;
  board[x][y] = c;
  if(c == '+') updateBoard(board, x, y, '+','*');
  else updateBoard(board, x, y, '*', '+');
  for (int i = 0; i < 8; ++i)
  {
   for (int j = 0; j < 8; ++j)
    putchar(board[i][j]);
   putchar('\n');
  }
  putchar('\n');
 }
 return 0;
}

发表于 2017-07-26 22:37:00 回复(0)
题目说的夹住对方棋子有点模糊,需要注意几点
1.斜线方向只限于45度
2.夹住对方棋子时中间必须是对方棋子不能有空格和我方棋子
3.针对第二点强调一下,每个方向上只有一个最近的我方棋子能夹住对方棋子,该方向上后续棋子不算
发表于 2018-10-13 16:34:53 回复(0)
import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		while (sc.hasNext()) {
			Character[][] map = new Character[8][8];
			for (int i = 0; i < 8; i ++ ) {
				String s = sc.next();
				for (int j = 0; j < 8; j ++ )
					map[i][j] = s.charAt(j);
			}
			int row = sc.nextInt() - 1;
			int col = sc.nextInt() - 1;
			Character c = sc.next().charAt(0);
			map[row][col] = c;
			fun(map, row, col, c);
			for (int i = 0; i < map.length; i ++ ) {
				for (int j = 0; j < map[0].length; j ++ ) {
					if(j == map[0].length - 1) System.out.println(map[i][j]);
					else System.out.print(map[i][j]);
				}
			}
			System.out.println();
		}
	}

	public static void fun(Character[][] map, int row, int col, Character c) {
		int[][] direction = {{0, 1}, {0, - 1}, {1, 0}, { - 1, 0}, {1, 1}, {1, - 1}, { - 1, 1}, { - 1, - 1}};
		for (int i = 0; i < direction.length; i ++ ) {
			int x = row + direction[i][0];
			int y = col + direction[i][1];
			while (x >= 0 && x < map.length && y >= 0 && y < map[0].length && map[x][y] != c && map[x][y] != '.') {
				x += direction[i][0];
				y += direction[i][1];
			}
			if(x >= 0 && x < map.length && y >= 0 && y < map[0].length && map[x][y] == c) {
				while (map[x -= direction[i][0]][y -= direction[i][1]] != c)
					map[x][y] = c;
			}
		}
	}
}

编辑于 2016-10-11 18:21:21 回复(0)
按照八个方向查找即可
#include <iostream>
#include <vector>
using namespace std;

#define N 8

void solve(vector<vector<char> > & map, int x, int y, char c)
{
	int dir[8][2] = { {-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1} };
	map[x][y] = c;

	for (int i = 0; i < 8; ++i)
	{
		int count = 1, flag = 0;
		int nx = x + dir[i][0];
		int ny = y + dir[i][1];
		if (c == '*')
		{
			while (nx >= 0 && nx <= N && ny >= 0 && ny <= N && map[nx][ny] == '+')
				nx += dir[i][0], ny += dir[i][1];
			if (nx >= 0 && nx <= N && ny >= 0 && ny <= N && map[nx][ny] == '*')
			{
				nx -= dir[i][0], ny -= dir[i][1];
				while (map[nx][ny] == '+')
				{
					map[nx][ny] = '*';
					nx -= dir[i][0], ny -= dir[i][1];
				}
			}
		}
		else if (c == '+')
		{
			while (nx >= 0 && nx <= N && ny >= 0 && ny <= N && map[nx][ny] == '*')
				nx += dir[i][0], ny += dir[i][1];
			if (nx >= 0 && nx <= N && ny >= 0 && ny <= N && map[nx][ny] == '+')
			{
				nx -= dir[i][0], ny -= dir[i][1];
				while (map[nx][ny] == '*')
				{
					map[nx][ny] = '+';
					nx -= dir[i][0], ny -= dir[i][1];
				}
			}
		}
	}
}

int main() 
{
	char ch;
	while (cin >> ch)
	{
		vector<vector<char> > map(10, vector<char>(10, '.'));
		map[1][1] = ch;
		for (int i = 1; i <= N; ++i)
			for (int j = 1; j <= N; ++j)
			{
				if (i == 1 && j == 1) continue;
				cin >> ch;
				map[i][j] = ch;
			}

		int x, y;
		cin >> x >> y >> ch;
		solve(map, x, y, ch);

		for (int i = 1; i <= N; ++i)
		{
			for (int j = 1; j <= N; ++j)
				cout << map[i][j];
			cout << endl;
		}
		cout << endl;
	}

	return 0;
} 


发表于 2015-12-22 22:47:18 回复(0)

问题信息

难度:
5条回答 9765浏览

热门推荐

通过挑战的用户

查看代码