翻转棋

  • Description

翻转棋的棋盘上有64颗棋子,排列成8×8的矩阵。每颗棋子的正反两面分别被涂成白色和黑色。在初始状态下,其中有一些棋子正面朝上(白色),其余的则背面朝上(黑色)。你可以进行如下翻转操作——翻转任意一颗棋子,与此同时与它相邻的上下左右四颗棋子也必须一同翻面。现在给出棋盘的初始状态,请编程计算出至少要进行多少次翻转操作才能让所有的棋子黑色的一面朝上。

  • Input

输入的第一行是一个正整数N,代表有N组输入数据。每组数据包含一个8×8的矩阵表示棋盘的初始状态,‘0’代表白色,‘1’代表黑色。两组数据之间以一个空白行分隔。

  • Output

对于每一组输入数据,输出一行“Case #: n”。其中‘#’为这一组输出的序号,n为让所有棋子反面向上最少所需的翻转次数。

  • Sample Input

2
10111111
00011111
10111111
11111111
11111111
11111111
11111111
11111111

10110001
00011011
10111111
11111111
11111011
11110001
11111111
11110001

  • Sample Output

Case 1: 1
Case 2: 4

#include<iostream>
#include<string>
using namespace std;
#define N 10
#define minn(x,y) ((x)<(y)?(x):(y))
char str[N];
int map[N][N],tmap[N][N],ans[N][N];

void Rev(int i,int j)
{
    tmap[i][j]=(tmap[i][j]+1)%2;
    if(i>0)
        tmap[i-1][j]=(tmap[i-1][j]+1)%2;
    if(i<8)
        tmap[i+1][j]=(tmap[i+1][j]+1)%2;
    if(j>0)
        tmap[i][j-1]=(tmap[i][j-1]+1)%2;
    if(j<8)
        tmap[i][j+1]=(tmap[i][j+1]+1)%2;
}

int main()
{
    //freopen("in.txt","r",stdin);
    int times,i,j,k;
    scanf("%d",&times);
    for(k=1;k<=times;k++)
    {
        getchar();
        int min=256;
        for(i=0;i<8;i++)
        {
            scanf("%s",str);
            for(j=0;j<8;j++)
                map[i][j]=str[j]-'0';
        }
        for(int fl=0;fl<256;fl++)
        {
            int count=0;
            memset(ans,0,sizeof(ans));
            for(i=0;i<8;i++)
            {
                for(j=0;j<8;j++)
                    tmap[i][j]=map[i][j];
            }
            for(j=0;j<8;j++)
            {
                if(fl & (1<<j))
                {
                    Rev(0,j);
                    ans[0][j]=1;
                }
            }
            for(i=1;i<8;i++)
            {
                for(j=0;j<8;j++)
                if(!tmap[i-1][j])
                {
                    Rev(i,j);
                    ans[i][j]=1;
                }
            }
            for(i=0;i<8;i++)
                count+=tmap[7][i];
            if(count!=8)
                continue;
            count=0;
            for(i=0;i<8;i++)
            {
                for(j=0;j<8;j++)
                    count+=ans[i][j];
            }
            min=minn(min,count);
        }
        printf("Case %d: %d\n",k,min);
    }
    return 0;
}

以下是一个基于Java Swing实现黑白翻转的示例代码: ```java import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Reversi extends JFrame implements ActionListener { private static final long serialVersionUID = 1L; private JButton[][] board; private int[][] status; private int currentPlayer; private JLabel statusLabel; public Reversi() { setTitle("黑白翻转"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(400, 400); setResizable(false); JPanel boardPanel = new JPanel(); boardPanel.setLayout(new GridLayout(8, 8)); board = new JButton[8][8]; status = new int[8][8]; for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { board[i][j] = new JButton(); board[i][j].setPreferredSize(new Dimension(40, 40)); board[i][j].addActionListener(this); boardPanel.add(board[i][j]); status[i][j] = 0; } } board[3][3].setBackground(Color.WHITE); board[3][4].setBackground(Color.BLACK); board[4][3].setBackground(Color.BLACK); board[4][4].setBackground(Color.WHITE); status[3][3] = -1; status[3][4] = 1; status[4][3] = 1; status[4][4] = -1; JPanel statusPanel = new JPanel(); statusPanel.setLayout(new FlowLayout()); statusLabel = new JLabel("黑方先行"); statusPanel.add(statusLabel); Container container = getContentPane(); container.setLayout(new BorderLayout()); container.add(boardPanel, BorderLayout.CENTER); container.add(statusPanel, BorderLayout.SOUTH); } public void actionPerformed(ActionEvent e) { JButton button = (JButton) e.getSource(); int row = -1, col = -1; for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { if (button == board[i][j]) { row = i; col = j; break; } } } if (row == -1 || col == -1) { return; } if (status[row][col] != 0) { return; } int count = 0; for (int i = -1; i <= 1; i++) { for (int j = -1; j <= 1; j++) { if (i == 0 && j == 0) { continue; } int r = row + i, c = col + j; while (r >= 0 && r < 8 && c >= 0 && c < 8 && status[r][c] == -currentPlayer) { r += i; c += j; } if (r >= 0 && r < 8 && c >= 0 && c < 8 && status[r][c] == currentPlayer) { count += Math.max(Math.abs(row - r), Math.abs(col - c)) - 1; } } } if (count == 0) { return; } status[row][col] = currentPlayer; if (currentPlayer == 1) { button.setBackground(Color.BLACK); currentPlayer = -1; statusLabel.setText("白方行动"); } else { button.setBackground(Color.WHITE); currentPlayer = 1; statusLabel.setText("黑方行动"); } for (int i = -1; i <= 1; i++) { for (int j = -1; j <= 1; j++) { if (i == 0 && j == 0) { continue; } int r = row + i, c = col + j; while (r >= 0 && r < 8 && c >= 0 && c < 8 && status[r][c] == -currentPlayer) { r += i; c += j; } if (r >= 0 && r < 8 && c >= 0 && c < 8 && status[r][c] == currentPlayer) { int nr = row + i, nc = col + j; while (nr != r || nc != c) { status[nr][nc] = currentPlayer; if (currentPlayer == 1) { board[nr][nc].setBackground(Color.BLACK); } else { board[nr][nc].setBackground(Color.WHITE); } nr += i; nc += j; } } } } int blackCount = 0, whiteCount = 0; for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { if (status[i][j] == 1) { blackCount++; } else if (status[i][j] == -1) { whiteCount++; } } } if (blackCount + whiteCount == 64) { if (blackCount > whiteCount) { JOptionPane.showMessageDialog(null, "黑方胜利!"); } else if (blackCount < whiteCount) { JOptionPane.showMessageDialog(null, "白方胜利!"); } else { JOptionPane.showMessageDialog(null, "平局!"); } System.exit(0); } if (currentPlayer == 1) { for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { if (status[i][j] == 0) { int cnt = 0; for (int k = -1; k <= 1; k++) { for (int l = -1; l <= 1; l++) { if (k == 0 && l == 0) { continue; } int r = i + k, c = j + l; while (r >= 0 && r < 8 && c >= 0 && c < 8 && status[r][c] == -1) { r += k; c += l; } if (r >= 0 && r < 8 && c >= 0 && c < 8 && status[r][c] == 1) { cnt += Math.max(Math.abs(i - r), Math.abs(j - c)) - 1; } } } if (cnt > 0) { return; } } } } JOptionPane.showMessageDialog(null, "白方无子可下,黑方继续行动"); currentPlayer = -1; statusLabel.setText("黑方行动"); } else { for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { if (status[i][j] == 0) { int cnt = 0; for (int k = -1; k <= 1; k++) { for (int l = -1; l <= 1; l++) { if (k == 0 && l == 0) { continue; } int r = i + k, c = j + l; while (r >= 0 && r < 8 && c >= 0 && c < 8 && status[r][c] == 1) { r += k; c += l; } if (r >= 0 && r < 8 && c >= 0 && c < 8 && status[r][c] == -1) { cnt += Math.max(Math.abs(i - r), Math.abs(j - c)) - 1; } } } if (cnt > 0) { return; } } } } JOptionPane.showMessageDialog(null, "黑方无子可下,白方继续行动"); currentPlayer = 1; statusLabel.setText("白方行动"); } } public static void main(String[] args) { Reversi game = new Reversi(); game.setVisible(true); game.currentPlayer = 1; } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值