Game of Diamond

Problem J: Game of Diamond

Time Limit: 1 Sec   Memory Limit: 32 MB
Submit: 8   Solved: 3
[ Submit][ Status][ Web Board]

Description

Diamond mine is a mini-game which is played on an 8 * 8 board as you can see below.
The board is filled with different colors of diamonds. The player can make one move at a time. A move is
legal if it swaps two adjacent diamonds(not diagonally) and after that, there are three or more adjacent
diamonds in a row or column with the same color. Those diamonds will be taken away and new
diamonds will be put in their positions. The game continues until no legal moves exist.
Given the board description. You are going to determine whether a move is legal.

Input

The input contains several cases. Each case has exactly 9 lines. The first 8 lines each contains a string
of 8 characters. The characters are 'R'(Red), 'O'(Orange), 'G'(Green), 'P'(Purple), 'W'(White), 'Y'(Yellow)
or 'B'(Blue). All characters are uppercase. No 3 diamonds of the same color are initially in adjacent
positions in a row or column. The last line has 4 integers in the form " row1 column1 row2 column2"
describing the postions of the 2 diamonds that the player wants to swap. Rows are marked 1 to 8
increasingly from top to bottom while columns from left to right. Input is terminated by EOF.

Output

For each case, output "Ok!" if the move is legal or "Illegal move!" if it is not.

Sample Input

PBPOWBGW
RRPRYWWP
YGBYYGPP
OWYGGRWB
GBBGBGGR
GBWPPORG
PPGORWOG
WYWGYWBY
4 3 3 3
PBPOWBGW
RRPRYWWP
YGBYYGPP
OWYGGRWB
GBBGBGGR
GBWPPORG
PPGORWOG
WYWGYWBY
5 5 6 5

Sample Output

Ok!
Illegal move!

HINT


#include<cstdio>
#include<cmath>
#include<iostream>
#include <string.h>

using namespace std;
char s[20][20];
int iegal(int x1,int y1,int x2,int y2 )
{
    if(abs(x1-x2)+abs(y1-y2)==1)
   // if(abs(x1+y1-x2-y2)==1)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}
int success(int x,int y)
{
  //  cout<<"char "<<s[x][y]<<" "<<x<<" "<<y<<endl;
    char c;
    c=s[x][y];
    int x1=x-2,i,flag=0,y1=y-2,j;
    if(x1<0)
    {
        x1=0;
    }
    if(y1<0)
    {
        y1=0;
    }
   // cout<<x<<" "<<y<<" "<<x1<<" "<<y1<<endl;
    for(i=x1;i<=x;i++)
    {
       // cout<<"cols  :"<<s[i][y]<<s[i+1][y]<<s[i+2][y]<<endl;
        if(s[i][y]==c&&s[i+1][y]==c&&s[i+2][y]==c)
        {
            flag=1;
            break;
        }
    }
    for(j=y1;j<=y;j++)
    {
      //  cout<<"rows :"<<s[x][j]<<s[x][j+1]<<s[x][j+2]<<endl;
        if(s[x][j]==c&&s[x][j+1]==c&&s[x][j+2]==c)
        {
            flag=1;
            break;
        }
    }
    return flag;
}
int main()
{
    int i,j,sx,sy,ex,ey,flag;
    char c;
    memset(s, 0, sizeof(s));
    while(scanf("%s",s[0])!=EOF)
    {
        flag=0;
        for(i=1;i<8;i++)
        {
            scanf("%s",&s[i]);
        }
      /*  for(i=0;i<8;i++)
        {
            for(j=0;j<8;j++)
            {
                printf("%c",s[i][j]);
            }
            printf("\n");
        }*/
        cin>>sx>>sy>>ex>>ey;
      //  cout<<sx<<" "<<sy<<" "<<s[sx-1][sy-1]<<" "<<ex<<" "<<ey<<" "<<s[ex-1][ey-1]<<endl;
        if(sx<0||sx>=8||sy<0||sy>=8||ex<0||ey>=8||ey<0||ey>=8)
        {
            cout<<"Illegal move!"<<endl;
            continue;
        }
        if(iegal(sx,sy,ex,ey))
        {
            c=s[sx-1][sy-1];
            s[sx-1][sy-1]=s[ex-1][ey-1];
            s[ex-1][ey-1]=c;
          /*  for(i=0;i<8;i++)
            {
                for(j=0;j<8;j++)
                {
                    printf("%c",s[i][j]);
                }
                printf("\n");
            }*/
            if(success(sx-1,sy-1))
            {
                flag=1;
            }
             if(success(ex-1,ey-1))
            {
                flag=1;
            }


        }
        if(flag==0)
        {
            cout<<"Illegal move!"<<endl;
        }
        else
        {
            cout<<"OK!"<<endl;
        }
    }
}
/*
  #E#EE
  #E#E#
  E#E#E
  ##E#E
  */

#E#EE123
#E#E#312
E#E#E456
##E#8789
#E#EE123
9E#E#312
E#E#E456
##E#E789
1 5 2 3


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是Game of Life的C++代码示例: ```cpp #include <iostream> #include <vector> #include <chrono> #include <thread> #include <cstdlib> using namespace std; class GameOfLife { private: vector<vector<int>> grid; int rows, columns; public: GameOfLife(int r, int c) : rows(r), columns(c) { grid.resize(rows, vector<int>(columns, 0)); } void initialize() { srand(time(NULL)); for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { grid[i][j] = rand() % 2; } } } int getNeighbourCount(int row, int col) { int count = 0; for (int i = -1; i <= 1; i++) { for (int j = -1; j <= 1; j++) { int r = row + i; int c = col + j; if (r >= 0 && r < rows && c >= 0 && c < columns && !(i == 0 && j == 0)) { count += grid[r][c]; } } } return count; } void nextGeneration() { vector<vector<int>> newGrid(rows, vector<int>(columns, 0)); for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { int neighbourCount = getNeighbourCount(i, j); if (grid[i][j] == 0 && neighbourCount == 3) { newGrid[i][j] = 1; } else if (grid[i][j] == 1 && (neighbourCount == 2 || neighbourCount == 3)) { newGrid[i][j] = 1; } else { newGrid[i][j] = 0; } } } grid = newGrid; } void print() { for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { if (grid[i][j] == 1) { cout << "* "; } else { cout << ". "; } } cout << endl; } } }; int main() { int rows = 40, columns = 40; GameOfLife gol(rows, columns); gol.initialize(); while (true) { gol.print(); gol.nextGeneration(); this_thread::sleep_for(chrono::milliseconds(100)); system("clear"); } return 0; } ``` 此代码实现了一个Game of Life模拟器,它使用随机值初始化单元格,并在每次迭代中计算每个单元格周围的邻居数量,然后使用规则进行更新。最后,它在控制台中打印游戏板并在每次迭代后清除屏幕。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值