1206: Diamond (字符配对)

1206: Diamond

时间限制: 1 Sec   内存限制: 32 MB
提交: 76   解决: 14
[ 提交][ 状态][ 讨论版]

题目描述

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<string.h>
#include<math.h>
char a[10][10];
int main(){
	int x1,x2,y1,y2;
	int i=0;
	char c;
	while(scanf("%s",a[++i]+1)!=EOF)
	{
		if(i==8)
		{
			scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
			if((x1==x2&&abs(y1-y2)>1)||(y1==y2&&abs(x1-x2)>1)||x1==x2&&y1==y2)
			{
				printf("Illegal move!\n");
				i=0;
					continue;
			}
			else if(x1==x2||y1==y2)
			{
				c=a[x1][y1];
				a[x1][y1]=a[x2][y2];
				a[x2][y2]=c;
				//这块判断坐标(x1,y1)横向是否有3个相连的 
				if((a[x1][y1]==a[x1][y1-1]&&a[x1][y1]==a[x1][y1-2])||(a[x1][y1]==a[x1][y1+1]&&a[x1][y1]==a[x1][y1+2])||(a[x1][y1]==a[x1][y1+1]&&a[x1][y1]==a[x1][y1-1]))
				{
					printf("Ok!\n");
					i=0;
					continue;
				} 
				//这块判断坐标 (x1,y1)纵向是否有三个相连 
				else if((a[x1][y1]==a[x1-1][y1]&&a[x1][y1]==a[x1-2][y1])||(a[x1][y1]==a[x1+1][y1]&&a[x1][y1]==a[x1+2][y1])||(a[x1][y1]==a[x1+1][y1]&&a[x1][y1]==a[x1-1][y1]))
				{
					printf("Ok!\n");
					i=0;
					continue;
				}
				//这块判断坐标 (x2,y2)横向是否有三个相连
				else if((a[x2][y2]==a[x2][y2-1]&&a[x2][y2]==a[x2][y2-2])||(a[x2][y2]==a[x2][y2+1]&&a[x2][y2]==a[x2][y2+2])||(a[x2][y2]==a[x2][y2-1]&&a[x2][y2]==a[x2][y2+1]))
				{
					printf("Ok!\n");
					i=0;
					continue;
				}
				//这块判断坐标 (x2,y2)纵向是否有三个相连
				else if((a[x2][y2]==a[x2-1][y2]&&a[x2][y2]==a[x2-2][y2])||(a[x2][y2]==a[x2+1][y2]&&a[x2][y2]==a[x2+2][y2])||(a[x2][y2]==a[x2-1][y2]&&a[x2][y2]==a[x2+1][y2]))
				{
					printf("Ok!\n");
					i=0;
					continue;
				}
				else
				{
					printf("Illegal move!\n");
					i=0;
					continue;
				}
			}
			else
				printf("Illegal move!\n");
				i=0;
		}	
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值