uva 141 The Spot Game(STL set)

942 篇文章 2 订阅
223 篇文章 1 订阅

 The Spot Game 

The game of Spot is played on an NxN board as shown below for N = 4. During the game, alternate players may either place a black counter (spot) in an empty square or remove one from the board, thus producing a variety of patterns. If a board pattern (or its rotation by 90 degrees or 180 degrees) is repeated during a game, the player producing that pattern loses and the other player wins. The game terminates in a draw after 2N moves if no duplicate pattern is produced before then.

Consider the following patterns:

picture23

If the first pattern had been produced earlier, then any of the following three patterns (plus one other not shown) would terminate the game, whereas the last one would not.

Input and Output

Input will consist of a series of games, each consisting of the size of the board, N (2 tex2html_wrap_inline180 N tex2html_wrap_inline180 50) followed, on separate lines, by 2N moves, whether they are all necessary or not. Each move will consist of the coordinates of a square (integers in the range 1..N) followed by a blank and a character `+' or `-' indicating the addition or removal of a spot respectively. You may assume that all moves are legal, that is there will never be an attempt to place a spot on an occupied square, nor to remove a non-existent spot. Input will be terminated by a zero (0).

Output will consist of one line for each game indicating which player won and on which move, or that the game ended in a draw.

Sample input

2
1 1 +
2 2 +
2 2 -
1 2 +
2
1 1 +
2 2 +
1 2 +
2 2 -
0

Sample output

Player 2 wins on move 3
Draw

题目大意:在一个给定大小的n * n 的棋盘上,由玩家两方各自在棋盘上添加或删除棋子,如果当前玩家的操作使得棋盘上的棋子摆法出现了重复(前面出现过的, 包括旋转),则记该玩家失败,若未分出胜负,输出draw.

解题思路:借助STL中的set,记录所有出现过的棋子摆法,包括旋转。

#include <stdio.h>
#include <string.h>
#include <set>
#include <iostream>
using namespace std;
const int N = 55;

set<string> vec;
int g[N][N], n, flag;
char str[4][N * N];

void handle() {
    memset(str, 0, sizeof(str));
    int t = 0, k = 0;
    for (int i = 0; i < n; i++)
	for (int j = 0; j < n; j++)
	    str[k][t++] = g[i][j] + '0';
    str[k++][t] = '\0';
    t = 0;

    for (int j = n - 1; j >= 0; j--)
	for (int i = 0; i < n; i++)
	    str[k][t++] = g[i][j] + '0';
    str[k++][t] = '\0';
    t = 0;

    for (int j = 0; j < n; j++)
	for (int i = n - 1; i >= 0; i--)
	    str[k][t++] = g[i][j] + '0';
    str[k++][t] = '\0';
    t = 0;

    for (int i = n - 1; i >= 0; i--)
	for (int j = n - 1; j >= 0; j--)
	    str[k][t++] = g[i][j] + '0';

    str[k++][t] = '\0';
}

int main() {
    int x, y;
    char c;
    while (scanf("%d", &n), n) {
	memset(g, 0, sizeof(g));
	flag = 0;
	vec.clear();
	for (int i = 1; i <= 2 * n; i++) {
	    scanf("%d%d %c", &x, &y, &c);

	    if (c == '+')
		g[x - 1][y - 1] = 1;
	    else if (c == '-')
		g[x - 1][y - 1] = 0;

	    handle();

	    if (!flag) {
		for (int j = 0; j < 4; j++) {
		    if (vec.find(str[j]) != vec.end())
			flag = i;
		}
	    }

	    if (!flag) {
		for (int j = 0; j < 4; j++)
		    vec.insert(str[j]);
	    }
	}

	if (flag % 2)
	    printf("Player 2 wins on move %d\n", flag);
	else if (flag)
	    printf("Player 1 wins on move %d\n", flag);	    
	else
	    printf("Draw\n");
    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值