若是凉夜已成梦

青春里 总有些事情要努力去做 总有些梦想要拼命去追。


  • 运维

  • 前端

  • 编程

  • 随笔

  • hust-oj

1015: Diamond

发表于 2017-10-06   |   分类于 HUSTOJ   |   阅读次数 2,244

题目描述

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.

输入

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.

输出

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

样例输入

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

样例输出

Ok!
Illegal move!

参考代码

#include <stdio.h>
#include <math.h>
char map[8][9];
int ok(int i,int j,char ch) 
{
    return i>=0&&j>=0&&i<8&&j<8&&ch==map[i][j];
}
int ok2(int i,int j) 
{
    return i>=0&&j>=0&&i<8&&j<8;
}
int can(int si,int sj,int ei,int ej) 
{
    return ok2(si,sj)&&ok2(ei,ej)&&( (si==ei&&fabs(sj-ej)==1) || (sj==ej&&fabs(si-ei)==1));
}
int amount(int si,int sj) 
{
    int i,count=1;
    for (i=1;;i++)
            if( ok(si-i,sj,map[si][sj]) ) count++; else break;
    for (i=1;;i++)
            if( ok(si+i,sj,map[si][sj]) ) count++; else break;
    if(count>2) return 1;
    for (i=count=1;;i++)
            if( ok(si,sj-i,map[si][sj]) ) count++; else break;
    for (i=1;;i++)
            if( ok(si,sj+i,map[si][sj]) ) count++; else break;
    if(count>2) return 1; else return 0;
}
int main() 
{
    int i,si,sj,ei,ej;
    char t;
    while(scanf("%s",map[0])!=EOF) 
    {
        for (i=1;i<8;i++)
                    scanf("%s",map[i]);
        scanf("%d%d%d%d",&si,&sj,&ei,&ej);
        si--;
        sj--;
        ei--;
        ej--;
        t=map[si][sj];
        map[si][sj]=map[ei][ej];
        map[ei][ej]=t;
        if( can(si,sj,ei,ej) && (amount(si,sj) || amount(ei,ej))) puts("Ok!"); else puts("Illegal move!");
    }
    return 0;
}

解析

暂无

hustoj

发表评论 取消回复

要发表评论,您必须先登录。


hoxis wechat
著作权归作者所有
站点更新说明
  • 文章目录
  • 站点概览
若是凉夜已成梦

若是凉夜已成梦

青春里 总有些事情要努力去做 总有些梦想要拼命去追。

1904 日志
6 分类
12 标签
RSS
weibo github twitter facebook

友情链接

原站点 Skip Dreams孤独患者
© 2017 若是凉夜已成梦
Powered by WordPress | 已运行
Theme By NexT.Mist